diff options
Diffstat (limited to 'Sora/Views/Post/Details/PostDetailsView.swift')
| -rw-r--r-- | Sora/Views/Post/Details/PostDetailsView.swift | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/Sora/Views/Post/Details/PostDetailsView.swift b/Sora/Views/Post/Details/PostDetailsView.swift index 9c55798..bb58c49 100644 --- a/Sora/Views/Post/Details/PostDetailsView.swift +++ b/Sora/Views/Post/Details/PostDetailsView.swift @@ -7,6 +7,7 @@ struct PostDetailsView: View { @Binding var navigationPath: NavigationPath @State private var loadingStage: BooruPostLoadingState = .loadingPreview @State private var isTagsSheetPresented = false + @State private var activePost: BooruPost // swiftlint:disable:next discouraged_optional_collection let posts: [BooruPost]? let baseSearchText: String? @@ -33,6 +34,7 @@ struct PostDetailsView: View { ) { self.post = post self._navigationPath = navigationPath + self._activePost = State(initialValue: post) self.posts = posts self.baseSearchText = baseSearchText } @@ -44,7 +46,7 @@ struct PostDetailsView: View { } private var currentPost: BooruPost { - manager.selectedPost ?? post + activePost } var body: some View { @@ -67,7 +69,8 @@ struct PostDetailsView: View { PostDetailsCarouselView( posts: filteredPosts, loadingStage: $loadingStage, - focusedPost: currentPost + focusedPost: currentPost, + onFocusedPostChange: updateActivePost ) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center) #endif @@ -149,6 +152,16 @@ struct PostDetailsView: View { tagsSheetContent() } } + .onAppear { + syncActivePost() + } + .onChange(of: post.id) { + activePost = post + syncActivePost() + } + .onChange(of: manager.selectedPost?.id) { + syncActivePost() + } } @ViewBuilder @@ -167,4 +180,26 @@ struct PostDetailsView: View { ) #endif } + + private func updateActivePost(_ post: BooruPost) { + guard activePost.id != post.id else { return } + + activePost = post + } + + private func syncActivePost() { + #if os(macOS) + if let selectedPost = manager.selectedPost { + updateActivePost(selectedPost) + } else { + updateActivePost(post) + } + #else + if let selectedPost = manager.selectedPost, posts == nil { + updateActivePost(selectedPost) + } else { + updateActivePost(post) + } + #endif + } } |