diff options
| author | Fuwn <[email protected]> | 2025-03-23 03:19:25 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-03-23 03:19:25 -0700 |
| commit | 6a0996d857786405b304cb71a50076e2251c57d3 (patch) | |
| tree | 5bca8b13dc7945d4f465ab6bc2b85b8de3ad18f1 /Sora/Views/Post/Grid/PostGridView.swift | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-6a0996d857786405b304cb71a50076e2251c57d3.tar.xz sora-testing-6a0996d857786405b304cb71a50076e2251c57d3.zip | |
feat: Development commit
Diffstat (limited to 'Sora/Views/Post/Grid/PostGridView.swift')
| -rw-r--r-- | Sora/Views/Post/Grid/PostGridView.swift | 84 |
1 files changed, 66 insertions, 18 deletions
diff --git a/Sora/Views/Post/Grid/PostGridView.swift b/Sora/Views/Post/Grid/PostGridView.swift index 78d90b3..0125b60 100644 --- a/Sora/Views/Post/Grid/PostGridView.swift +++ b/Sora/Views/Post/Grid/PostGridView.swift @@ -6,6 +6,7 @@ struct PostGridView: View { @EnvironmentObject var manager: BooruManager @State private var isSearchHistoryPresented = false @Binding var selectedTab: Int + @State private var viewStates: [UUID: PostGridViewState] = [:] @Environment(\.isSearching) private var isSearching @@ -17,18 +18,40 @@ struct PostGridView: View { var body: some View { ScrollViewReader { proxy in - ScrollView { - if filteredPosts.isEmpty { - ProgressView() - .padding() - } + ZStack { + ForEach(Array(manager.searchHistory.enumerated()), id: \.element.id) { index, query in + let isActive = index == manager.historyIndex + let filteredPosts = + viewStates[query.id]?.posts + .filter { settings.displayRatings.contains($0.rating) } ?? [] + + ScrollView { + if filteredPosts.isEmpty && isActive { + ProgressView() + .padding() + } - WaterfallGrid(filteredPosts, id: \.id) { post in - waterfallGridContent(post: post) - .id(post.id) + WaterfallGrid(filteredPosts, id: \.id) { post in + waterfallGridContent(post: post) + .id(post.id) + } + .gridStyle(columns: settings.thumbnailGridColumns) + .padding(8) + .onAppear { + if isActive, let scrollPosition = viewStates[query.id]?.scrollPosition { + proxy.scrollTo(scrollPosition) + } + } + .onChange(of: filteredPosts.first?.id) { _, newID in + if isActive, let newID { + updateViewState(for: query.id, scrollPosition: newID) + } + } + } + .opacity(isActive ? 1 : 0) + .frame(height: isActive ? nil : 0) + .clipped() } - .gridStyle(columns: settings.thumbnailGridColumns) - .padding(8) } .searchable(text: $manager.searchText, prompt: "Tags") .searchSuggestions { @@ -50,6 +73,20 @@ struct PostGridView: View { Task { manager.performSearch() } } } + .onChange(of: manager.posts) { _, newPosts in + if manager.historyIndex >= 0 && manager.historyIndex < manager.searchHistory.count { + let queryID = manager.searchHistory[manager.historyIndex].id + + updateViewState(for: queryID, posts: newPosts) + } + } + .onChange(of: manager.currentPage) { _, newPage in + if manager.historyIndex >= 0 && manager.historyIndex < manager.searchHistory.count { + let queryID = manager.searchHistory[manager.historyIndex].id + + updateViewState(for: queryID, currentPage: newPage) + } + } .toolbar { #if os(macOS) ToolbarItem { @@ -90,20 +127,14 @@ struct PostGridView: View { } PlatformSpecificToolbarItem { - Button(action: { - manager.goBackInHistory() - withAnimation { proxy.scrollTo(filteredPosts.first?.id) } - }) { + Button(action: { manager.goBackInHistory() }) { Label("Previous Search", systemImage: "chevron.left") } .disabled(!manager.canGoBackInHistory) } PlatformSpecificToolbarItem { - Button(action: { - manager.goForwardInHistory() - withAnimation { proxy.scrollTo(filteredPosts.first?.id) } - }) { + Button(action: { manager.goForwardInHistory() }) { Label("Next Search", systemImage: "chevron.right") } .disabled(!manager.canGoForwardInHistory) @@ -175,4 +206,21 @@ struct PostGridView: View { return items } + + private func updateViewState( + for queryID: UUID, + posts: [BooruPost] = [], + currentPage: Int? = nil, + scrollPosition: String? = nil + ) { + var state = viewStates[queryID] ?? PostGridViewState() + + if !posts.isEmpty { state.posts = posts } + + if let currentPage { state.currentPage = currentPage } + + if let scrollPosition { state.scrollPosition = scrollPosition } + + viewStates[queryID] = state + } } |