-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPageView.swift
77 lines (72 loc) · 2.66 KB
/
PageView.swift
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
import BibleCore
import ComposableArchitecture
import SwiftUI
struct PageView: View {
let store: StoreOf<Page>
func swipe(store: ViewStoreOf<Page>) -> some Gesture {
DragGesture()
.onEnded { value in
if value.translation.width < 0 {
store.send(.paginateChapter(forward: true))
} else {
store.send(.paginateChapter(forward: false))
}
}
}
var body: some View {
WithViewStore(store, observe: { $0 }) { viewStore in
ScrollView {
ScrollViewReader { reader in
LazyVStack(alignment: .leading) {
ForEach(viewStore.isRedacted ? [.mock] : viewStore.verses!) { (content: Verse) in
HStack(alignment: .top, spacing: 8) {
Text(content.verseId.description)
.bold()
Text(content.verse)
}
.frame(alignment: .top)
.onTapGesture {
viewStore.send(.select(content))
}
.id(content.id)
.transition(
AnyTransition.asymmetric(
insertion: .opacity,
removal: .identity
)
)
}
.onChange(of: viewStore.verse) { newValue in
withAnimation(.easeOut(duration: 0.3)) {
reader.scrollTo(newValue?.id, anchor: .top)
}
}
.redacted(reason: viewStore.isRedacted ? .placeholder : [])
}
.padding(.horizontal)
}
}
.task {
viewStore.send(.loadLastSave)
}
.gesture(swipe(store: viewStore))
}
}
}
struct PageView_Previews: PreviewProvider {
static var previews: some View {
Group {
PageView(store: Store(initialState: Page.State()) {
Page()
})
.previewDevice("iPhone 14")
.previewDisplayName("Mobile")
PageView(store: Store(initialState: Page.State()) {
Page()
})
.previewLayout(.sizeThatFits)
.previewDevice("Mac")
.previewDisplayName("Desktop")
}
}
}