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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
import SwiftUI
struct PostDetailsCarouselView: View {
@EnvironmentObject var manager: BooruManager
@EnvironmentObject var settings: SettingsManager
let posts: [BooruPost]
let focusedPost: BooruPost?
let onFocusedPostChange: (BooruPost) -> Void
@Binding var loadingStage: BooruPostLoadingState
@State private var currentIndex: Int?
private let cacheManager = ImageCacheManager.shared
init(
posts: [BooruPost],
loadingStage: Binding<BooruPostLoadingState>,
focusedPost: BooruPost? = nil,
onFocusedPostChange: @escaping (BooruPost) -> Void = { _ in
// Default no-op callback for previews and callers that don't need focus updates.
}
) {
self.posts = posts
self.focusedPost = focusedPost
self.onFocusedPostChange = onFocusedPostChange
_loadingStage = loadingStage
if let focused = focusedPost,
let index = posts.firstIndex(where: { $0.id == focused.id })
{
self._currentIndex = State(initialValue: index)
} else {
self._currentIndex = State(initialValue: 0)
}
}
func imageURL(post: BooruPost) -> URL? {
switch settings.detailViewQuality {
case .preview:
post.previewURL
case .sample:
post.sampleURL
case .original:
post.fileURL
}
}
var body: some View {
ScrollView(.horizontal) {
LazyHStack(spacing: 0) {
ForEach(Array(posts.enumerated()), id: \.offset) { index, post in
PostDetailsCarouselItemView(
post: post,
index: index,
loadingStage: $loadingStage,
imageURL: imageURL
)
#if os(iOS)
.frame(width: UIScreen.main.bounds.width)
#endif
}
}
.scrollTargetLayout()
}
.scrollPosition(id: $currentIndex)
.scrollIndicators(.hidden)
.scrollTargetBehavior(.paging)
.onChange(of: currentIndex) {
guard let currentIndex, posts.indices.contains(currentIndex) else { return }
onFocusedPostChange(posts[currentIndex])
Task(priority: .utility) {
if currentIndex == posts.count - 1 { await manager.loadNextPage() }
}
preloadNearbyImages()
}
.onChange(of: focusedPost?.id) {
syncCurrentIndexWithFocus()
}
.onAppear {
syncCurrentIndexWithFocus()
preloadNearbyImages()
}
}
private func preloadNearbyImages() {
let preloadRange = settings.preloadedCarouselImages
guard preloadRange > 0 else { return }
guard let currentIndex else { return }
let startIndex = max(0, currentIndex - preloadRange)
let endIndex = min(posts.count - 1, currentIndex + preloadRange)
var urlsToPreload: [URL] = []
for index in startIndex...endIndex {
if let url = imageURL(post: posts[index]) {
urlsToPreload.append(url)
}
urlsToPreload.append(posts[index].previewURL)
}
cacheManager.preloadImages(
urlsToPreload,
domain: manager.domain,
sendUserAgent: settings.sendBooruUserAgent,
customUserAgent: settings.customBooruUserAgent
)
}
private func syncCurrentIndexWithFocus() {
if let focusedPost,
let index = posts.firstIndex(where: { $0.id == focusedPost.id })
{
currentIndex = index
onFocusedPostChange(posts[index])
} else if !posts.isEmpty {
currentIndex = 0
onFocusedPostChange(posts[0])
}
}
}
|