-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVotdView.swift
More file actions
106 lines (98 loc) · 3.87 KB
/
VotdView.swift
File metadata and controls
106 lines (98 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import SwiftUI
import YouVersionPlatformCore
public struct VotdView: View {
@State private var backgroundUrl: String?
@State private var reference: BibleReference?
@State private var title: String?
@Environment(\.colorScheme) private var colorScheme
@State private var bibleVersionId: Int
public init(bibleVersionId: Int = 3034) {
self.bibleVersionId = bibleVersionId
}
public var body: some View {
Group {
if reference != nil {
textStack
.padding()
.foregroundStyle(backgroundUrl == nil ? Color.primary : Color.white) // background images are dark
.background(
GeometryReader { geo in
if let backgroundUrl, let url = URL(string: backgroundUrl) {
votdBackground(url: url)
.frame(width: geo.size.width, height: geo.size.height)
.clipped()
}
}
)
} else {
ProgressView()
}
}
.task {
guard reference == nil else {
return
}
do {
let dayOfYear = Calendar.current.ordinality(of: .day, in: .year, for: Date())!
let votdData = try await YouVersionAPI.VOTD.verseOfTheDay(dayOfYear: dayOfYear)
if let version = try? await BibleVersionRepository.shared.version(withId: bibleVersionId),
let reference = version.reference(with: votdData.passageId) {
self.reference = reference
self.title = version.displayTitle(for: reference, includesVersionAbbreviation: true)
} else {
print("VotdView: could not load version, or reference is invalid for this version")
}
} catch {
print("VotdView: error loading votd: \(error)")
}
}
}
private func votdBackground(url: URL) -> some View {
AsyncImage(url: url, transaction: Transaction(animation: .easeInOut)) { phase in
switch phase {
case .success(let image):
image
.resizable()
.scaledToFill()
default:
EmptyView()
}
}
}
private var textStack: some View {
VStack(alignment: .leading) {
HStack(alignment: .top) {
Image("votd", bundle: .YouVersionUIBundle)
.renderingMode(.template) // to be right in dark mode
.resizable()
.frame(width: 44, height: 44)
VStack(alignment: .leading, spacing: 4) {
let light = colorScheme == .light
Text(String.localized("tab.votd"))
.font(Font.system(size: 12).bold().smallCaps())
.foregroundStyle(light ? Color(hex: "#636161") : Color(hex: "#bfbdbd"))
if let title {
Text(title)
.font(Font.system(size: 16).bold())
.lineLimit(1)
.padding(.bottom, 16)
} else {
ProgressView()
}
}
}
if let reference {
let textOptions = BibleTextOptions(fontSize: 24, renderVerseNumbers: false)
BibleTextView(reference, textOptions: textOptions)
.minimumScaleFactor(0.5)
.textSelection(.enabled)
.padding(.bottom, 16)
} else {
ProgressView()
}
}
}
}
#Preview {
VotdView(bibleVersionId: 3034)
}