diff options
| author | Fuwn <[email protected]> | 2026-03-22 13:33:46 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-03-22 13:33:51 +0000 |
| commit | 54a5c28943f2f41d07e017dce488695768e7907e (patch) | |
| tree | 7aae408a7894848141bac644e6e3dcd9d4a67b28 /Sora/Views/Post/Details/Carousel/PostDetailsCarouselView.swift | |
| parent | fix: send referer header for booru requests (diff) | |
| download | sora-testing-54a5c28943f2f41d07e017dce488695768e7907e.tar.xz sora-testing-54a5c28943f2f41d07e017dce488695768e7907e.zip | |
Fix tag sheet when swiping post details
Diffstat (limited to 'Sora/Views/Post/Details/Carousel/PostDetailsCarouselView.swift')
| -rw-r--r-- | Sora/Views/Post/Details/Carousel/PostDetailsCarouselView.swift | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/Sora/Views/Post/Details/Carousel/PostDetailsCarouselView.swift b/Sora/Views/Post/Details/Carousel/PostDetailsCarouselView.swift index 94d258e..f733a89 100644 --- a/Sora/Views/Post/Details/Carousel/PostDetailsCarouselView.swift +++ b/Sora/Views/Post/Details/Carousel/PostDetailsCarouselView.swift @@ -5,6 +5,7 @@ struct PostDetailsCarouselView: View { @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 @@ -12,10 +13,12 @@ struct PostDetailsCarouselView: View { init( posts: [BooruPost], loadingStage: Binding<BooruPostLoadingState>, - focusedPost: BooruPost? = nil + focusedPost: BooruPost? = nil, + onFocusedPostChange: @escaping (BooruPost) -> Void = { _ in } ) { self.posts = posts self.focusedPost = focusedPost + self.onFocusedPostChange = onFocusedPostChange _loadingStage = loadingStage if let focused = focusedPost, @@ -61,7 +64,9 @@ struct PostDetailsCarouselView: View { .scrollIndicators(.hidden) .scrollTargetBehavior(.paging) .onChange(of: currentIndex) { - guard let currentIndex else { return } + guard let currentIndex, posts.indices.contains(currentIndex) else { return } + + onFocusedPostChange(posts[currentIndex]) Task(priority: .utility) { if currentIndex == posts.count - 1 { await manager.loadNextPage() } @@ -69,7 +74,13 @@ struct PostDetailsCarouselView: View { preloadNearbyImages() } - .onAppear(perform: preloadNearbyImages) + .onChange(of: focusedPost?.id) { + syncCurrentIndexWithFocus() + } + .onAppear { + syncCurrentIndexWithFocus() + preloadNearbyImages() + } } private func preloadNearbyImages() { @@ -92,4 +103,16 @@ struct PostDetailsCarouselView: View { cacheManager.preloadImages(urlsToPreload) } + + 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]) + } + } } |