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
import SwiftUI
struct ThreadScrollAnchor: View {
let id: String
@State private var offset: CGFloat = 0
var body: some View {
Color.clear
.frame(height: 0)
.onGeometryChange(for: CGFloat.self) { geometry in
geometry.frame(in: .named("threadScrollContent")).minY
} action: { offset = $0 }
.preference(key: ThreadAnchorOffsets.self, value: [id: offset])
}
}
private struct ThreadAnchorOffsets: PreferenceKey {
static let defaultValue: [String: CGFloat] = [:]
static func reduce(value: inout [String: CGFloat], nextValue: () -> [String: CGFloat]) {
value.merge(nextValue(), uniquingKeysWith: { _, new in new })
}
}
// Store scroll samples without updating the thread on each frame.
private final class ThreadScrollTracking {
var offset: CGFloat = 0
var anchorID: String?
var anchorOffset: CGFloat = 0
var isScrolling = false
var pendingCorrection: CGFloat = 0
}
struct ThreadScrollView<Content: View>: View {
let anchorID: String
let content: (CGFloat) -> Content
@State private var position = ScrollPosition(idType: String.self, y: 0)
@State private var tracking = ThreadScrollTracking()
init(anchorID: String, @ViewBuilder content: @escaping (CGFloat) -> Content) {
self.anchorID = anchorID
self.content = content
}
var body: some View {
GeometryReader { geometry in
ScrollView {
content(geometry.size.height)
.frame(maxWidth: .infinity, alignment: .leading)
.coordinateSpace(name: "threadScrollContent")
}
.scrollPosition($position)
.onScrollGeometryChange(for: CGFloat.self) { geometry in
geometry.contentOffset.y + geometry.contentInsets.top
} action: { _, offset in tracking.offset = offset }
.onScrollPhaseChange { _, phase in
tracking.isScrolling = phase == .tracking || phase == .interacting || phase == .decelerating
if phase == .idle, tracking.pendingCorrection != 0 {
position.scrollTo(y: tracking.offset + tracking.pendingCorrection)
tracking.pendingCorrection = 0
}
}
.onPreferenceChange(ThreadAnchorOffsets.self) { offsets in
guard let offset = offsets[anchorID] else { return }
let correction = tracking.anchorID == anchorID ? offset - tracking.anchorOffset : offset - tracking.offset
tracking.anchorID = anchorID
tracking.anchorOffset = offset
guard abs(correction) > 0.5 else { return }
if tracking.isScrolling {
tracking.pendingCorrection += correction
} else {
position.scrollTo(y: tracking.offset + correction)
}
}
}
.accessibilityIdentifier("threadScrollView")
}
}
#if DEBUG
#Preview("Thread scroll position") {
ThreadScrollView(anchorID: "selected") { height in
VStack(alignment: .leading, spacing: 24) {
Text("Earlier post")
VStack(alignment: .leading, spacing: 24) {
Text("Selected post")
Text("A reply to the selected post")
}
.frame(minHeight: height, alignment: .topLeading)
.background(alignment: .top) { ThreadScrollAnchor(id: "selected") }
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
}
}
#endif