Skip to content

Commit 82d25ad

Browse files
authored
refactor: cut apart BibleVersionRendering into 5 files
refactor: cut apart BibleVersionRendering into 5 files. No actual code was changed, just moved.
1 parent 2e893e8 commit 82d25ad

File tree

5 files changed

+387
-381
lines changed

5 files changed

+387
-381
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import SwiftUI
2+
import YouVersionPlatformCore
3+
4+
public final class BibleAttributedString: Equatable, Hashable {
5+
private var two: AttributedString
6+
7+
public init() {
8+
two = AttributedString()
9+
}
10+
11+
public init(_ string: String) {
12+
two = AttributedString(string)
13+
}
14+
15+
static func +(lhs: BibleAttributedString, rhs: BibleAttributedString) -> BibleAttributedString { //swiftlint:disable:this function_name_whitespace
16+
let result = BibleAttributedString()
17+
result.two = lhs.two + rhs.two
18+
return result
19+
}
20+
21+
static func += (lhs: inout BibleAttributedString, rhs: BibleAttributedString) {
22+
lhs = lhs + rhs
23+
}
24+
25+
public static func == (lhs: BibleAttributedString, rhs: BibleAttributedString) -> Bool {
26+
lhs.two == rhs.two
27+
}
28+
29+
public func hash(into hasher: inout Hasher) {
30+
two.hash(into: &hasher)
31+
}
32+
33+
public var asAttributedString: AttributedString {
34+
two
35+
}
36+
37+
var characters: String {
38+
String(two.characters)
39+
}
40+
41+
var isEmpty: Bool {
42+
two.characters.isEmpty
43+
}
44+
45+
@discardableResult
46+
public func setFont(_ option: BibleTextFontOption, from fonts: BibleTextFonts) -> BibleAttributedString {
47+
two.font = fonts.font(for: option)
48+
return self
49+
}
50+
51+
@discardableResult
52+
public func setColor(_ color: Color) -> BibleAttributedString {
53+
var ac = AttributeContainer()
54+
ac.foregroundColor = color
55+
two.mergeAttributes(ac)
56+
return self
57+
}
58+
59+
@discardableResult
60+
public func setBaselineOffset(_ offset: CGFloat) -> BibleAttributedString {
61+
two.baselineOffset = offset
62+
return self
63+
}
64+
65+
func trimTrailingWhitespaceAndNewlines() {
66+
var trimmed = two
67+
while let last = trimmed.characters.last, last.isWhitespace {
68+
trimmed = AttributedString(trimmed.characters.dropLast())
69+
}
70+
two = trimmed
71+
}
72+
73+
func markWithTextCategory(_ category: BibleTextCategory) {
74+
two.bibleTextCategory = category
75+
}
76+
77+
func markWithReference(_ reference: BibleReference, scheme: String, id: String?) {
78+
two.bibleReference = reference
79+
let idString = id == nil ? "" : "#\(id!)"
80+
two.link = URL(string: "\(scheme)://\(reference.versionId)/\(reference.asUSFM)\(idString)")
81+
}
82+
83+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import YouVersionPlatformCore
2+
3+
// Represents a footnote and its reference location.
4+
public struct BibleFootnote: Hashable, Identifiable {
5+
public let id: String
6+
public let text: BibleAttributedString
7+
public let reference: BibleReference
8+
9+
public init(text: BibleAttributedString, reference: BibleReference, id: String) {
10+
self.text = text
11+
self.reference = reference
12+
self.id = id
13+
}
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Foundation
2+
import SwiftUI
3+
4+
public struct BibleTextBlock: Identifiable {
5+
public let id = UUID()
6+
public let text: BibleAttributedString
7+
public let chapter: Int
8+
public let rows: [[BibleAttributedString]] // If it's a table, these are present instead of "text".
9+
public let firstLineHeadIndent: Int // The indentation of the first line of the paragraph. Always >= 0.
10+
public let headIndent: Int // The indentation of the paragraph’s lines other than the first. Always >= 0.
11+
//let tailIndent: Int // If positive, this value is the distance from the leading margin (for example,
12+
//the left margin in left-to-right text). If 0 or negative, it’s the distance from the trailing margin.
13+
public let marginTop: CGFloat
14+
public let alignment: TextAlignment
15+
public let footnotes: [BibleFootnote]
16+
17+
public init(
18+
text: BibleAttributedString,
19+
chapter: Int,
20+
firstLineHeadIndent: Int,
21+
headIndent: Int,
22+
marginTop: CGFloat,
23+
alignment: TextAlignment,
24+
footnotes: [BibleFootnote],
25+
rows: [[BibleAttributedString]] = []
26+
) {
27+
self.text = text
28+
self.chapter = chapter
29+
self.firstLineHeadIndent = firstLineHeadIndent
30+
self.headIndent = headIndent
31+
self.marginTop = marginTop
32+
self.alignment = alignment
33+
self.footnotes = footnotes
34+
self.rows = rows
35+
}
36+
}

0 commit comments

Comments
 (0)