diff options
Diffstat (limited to 'Sora/Views/Post/Details/PostDetailsCarouselView.swift')
| -rw-r--r-- | Sora/Views/Post/Details/PostDetailsCarouselView.swift | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/Sora/Views/Post/Details/PostDetailsCarouselView.swift b/Sora/Views/Post/Details/PostDetailsCarouselView.swift new file mode 100644 index 0000000..378b9cc --- /dev/null +++ b/Sora/Views/Post/Details/PostDetailsCarouselView.swift @@ -0,0 +1,69 @@ +import SwiftUI + +struct PostDetailsCarouselView: View { + @EnvironmentObject var manager: BooruManager + @EnvironmentObject var settings: SettingsManager + let posts: [BooruPost] + let focusedPost: BooruPost? + @Binding var loadingStage: BooruPostLoadingState + @State private var currentIndex: Int + + init( + posts: [BooruPost], + loadingStage: Binding<BooruPostLoadingState>, + focusedPost: BooruPost? = nil + ) { + self.posts = posts + self.focusedPost = focusedPost + _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 { + TabView(selection: $currentIndex) { + ForEach(posts.indices, id: \.self) { index in + ZStack { + PostDetailsImageView( + url: posts[index].previewURL, + loadingStage: $loadingStage + ) + .opacity(loadingStage == .loaded ? 0 : 1) + + PostDetailsImageView( + url: imageURL(post: posts[index]), + loadingStage: $loadingStage, + finalLoadingState: .loaded, + post: posts[index] + ) + } + .tag(index) + } + } + .onChange(of: currentIndex) { + if currentIndex == posts.count - 1 { manager.loadNextPage() } + } + #if !os(macOS) + .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never)) + #endif + } +} |