From f7d66a9190526f2c6e715fcddf10ca65c919140c Mon Sep 17 00:00:00 2001 From: Fuwn Date: Fri, 11 Jul 2025 08:24:34 -0700 Subject: feat: Development commit --- Sora/Views/Post/Grid/PostGridView.swift | 498 ++++++++++++++++---------------- 1 file changed, 251 insertions(+), 247 deletions(-) diff --git a/Sora/Views/Post/Grid/PostGridView.swift b/Sora/Views/Post/Grid/PostGridView.swift index c57e296..d0f6473 100644 --- a/Sora/Views/Post/Grid/PostGridView.swift +++ b/Sora/Views/Post/Grid/PostGridView.swift @@ -33,53 +33,26 @@ struct PostGridView: View { // swiftlint:disable:this type_body_length } var body: some View { - ScrollView { - Group { - if let error = manager.error { - ContentUnavailableView( - "Provider Error", - systemImage: "exclamationmark.triangle.fill", - description: Text(error.localizedDescription) - ) - } - - if activePosts.isEmpty, manager.isLoading { - let gridItems = Array( - repeating: GridItem(.flexible()), - count: settings.thumbnailGridColumns - ) - - LazyVGrid(columns: gridItems) { - ForEach(0..<(50 / settings.thumbnailGridColumns), id: \.self) { _ in - PostGridThumbnailPlaceholderView() - } + ScrollViewReader { proxy in + ScrollView { + Group { + if let error = manager.error { + ContentUnavailableView( + "Provider Error", + systemImage: "exclamationmark.triangle.fill", + description: Text(error.localizedDescription) + ) } - #if os(macOS) - .padding(8) - #else - .padding(.horizontal) - #endif - .transition(.opacity) - } else { - let columnCount = settings.thumbnailGridColumns - if settings.alternativeThumbnailGrid { - let columnsData = (0..= 0 && manager.historyIndex < manager.searchHistory.count { - updateViewState(for: queryID, posts: newPosts) + .onChange(of: isSearchablePresented) { _, isPresented in + if !isPresented, manager.searchText.isEmpty, !manager.isNavigatingHistory { + Task(priority: .userInitiated) { await manager.performSearch() } + } } - } - .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) + .onChange(of: manager.posts) { _, newPosts in + if manager.historyIndex >= 0 && manager.historyIndex < manager.searchHistory.count { + updateViewState(for: queryID, posts: newPosts) + } } - } - .onChange(of: manager.selectedPost) { _, newPost in - let queryID = manager.searchHistory.last { $0.tags == manager.tags }?.id ?? UUID() + .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, selectedPost: newPost, resetSelectedPost: newPost == nil) - } - .onChange(of: manager.historyIndex) { _, newIndex in - guard newIndex >= 0 && newIndex < manager.searchHistory.count else { return } + updateViewState(for: queryID, currentPage: newPage) + } + } + .onChange(of: manager.selectedPost) { _, newPost in + let queryID = manager.searchHistory.last { $0.tags == manager.tags }?.id ?? UUID() - let queryID = manager.searchHistory[newIndex].id + updateViewState(for: queryID, selectedPost: newPost, resetSelectedPost: newPost == nil) + } + .onChange(of: manager.historyIndex) { _, newIndex in + guard newIndex >= 0 && newIndex < manager.searchHistory.count else { return } - if let state = viewStates[queryID] { - manager.posts = state.posts - manager.currentPage = state.currentPage - manager.selectedPost = state.selectedPost - } else { - manager.posts = [] - manager.currentPage = 1 + let queryID = manager.searchHistory[newIndex].id - Task(priority: .userInitiated) { - await manager.fetchPosts( - page: 1, - tags: manager.searchHistory[newIndex].tags, - replace: true - ) + if let state = viewStates[queryID] { + manager.posts = state.posts + manager.currentPage = state.currentPage + manager.selectedPost = state.selectedPost + } else { + manager.posts = [] + manager.currentPage = 1 + + Task(priority: .userInitiated) { + await manager.fetchPosts( + page: 1, + tags: manager.searchHistory[newIndex].tags, + replace: true + ) + } } + + proxy.scrollTo(queryID, anchor: .top) } - } - .toolbar { - #if os(macOS) - ToolbarItem { - Button(action: { - Task { - await manager.fetchPosts(page: 1, tags: manager.tags, replace: true) + .toolbar { + #if os(macOS) + ToolbarItem { + Button(action: { + Task { + await manager.fetchPosts(page: 1, tags: manager.tags, replace: true) + } + }) { + Label("Refresh", systemImage: "arrow.clockwise") } - }) { - Label("Refresh", systemImage: "arrow.clockwise") + .disabled(manager.isLoading) } - .disabled(manager.isLoading) - } - #endif + #endif - #if !os(macOS) - PlatformSpecificToolbarItem { - Button(action: { Task { isSearchHistoryPresented.toggle() } }) { - Label("Search History", systemImage: "clock.arrow.circlepath") + #if !os(macOS) + PlatformSpecificToolbarItem { + Button(action: { Task { isSearchHistoryPresented.toggle() } }) { + Label("Search History", systemImage: "clock.arrow.circlepath") + } } - } - if #available(iOS 26, *), manager.isLoading || manager.isNavigatingHistory { - ToolbarItem(placement: .status) { ProgressView() } - } - #endif + if #available(iOS 26, *), manager.isLoading || manager.isNavigatingHistory { + ToolbarItem(placement: .status) { ProgressView() } + } + #endif - PlatformSpecificToolbarItem { - PostGridBookmarkButtonView() - .disabled(manager.tags.isEmpty) - } + PlatformSpecificToolbarItem { + PostGridBookmarkButtonView() + .disabled(manager.tags.isEmpty) + } - PlatformSpecificToolbarItem { - Button( - action: { - Task(priority: .userInitiated) { - await manager.loadNextPage() + PlatformSpecificToolbarItem { + Button( + action: { + Task(priority: .userInitiated) { + await manager.loadNextPage() + } } + ) { + Label( + "Manually Load Next Page", + systemImage: "arrow.down.to.line" + ) } - ) { - Label( - "Manually Load Next Page", - systemImage: "arrow.down.to.line" - ) - } - .disabled(manager.isLoading) - } - - #if !os(macOS) - if #unavailable(iOS 26), manager.isLoading || manager.isNavigatingHistory { - ToolbarItem(placement: .topBarTrailing) { ProgressView() } + .disabled(manager.isLoading) } - #endif - PlatformSpecificToolbarItem(placement: .navigation) { - Menu { - ForEach( - Array(manager.searchHistory.enumerated().filter { $0.offset < manager.historyIndex }), - id: \.offset - ) { offset, query in - Button(action: { - manager.historyIndex = offset - }) { - Text(query.tags.isEmpty ? "No Tags" : query.tags.joined(separator: " ")) - } + #if !os(macOS) + if #unavailable(iOS 26), manager.isLoading || manager.isNavigatingHistory { + ToolbarItem(placement: .topBarTrailing) { ProgressView() } } - } label: { - Label("Previous Search", systemImage: "chevron.left") - } primaryAction: { - withAnimation { - manager.goBackInHistory() + #endif + + PlatformSpecificToolbarItem(placement: .navigation) { + Menu { + ForEach( + Array(manager.searchHistory.enumerated().filter { $0.offset < manager.historyIndex }), + id: \.offset + ) { offset, query in + Button(action: { + manager.historyIndex = offset + }) { + Text(query.tags.isEmpty ? "No Tags" : query.tags.joined(separator: " ")) + } + } + } label: { + Label("Previous Search", systemImage: "chevron.left") + } primaryAction: { + withAnimation { + manager.goBackInHistory() + } } + .disabled(!manager.canGoBackInHistory) } - .disabled(!manager.canGoBackInHistory) - } - PlatformSpecificToolbarItem(placement: .navigation) { - Menu { - ForEach( - Array(manager.searchHistory.enumerated().filter { $0.offset > manager.historyIndex }), - id: \.offset - ) { offset, query in - Button(action: { - manager.historyIndex = offset - }) { - Text(query.tags.isEmpty ? "No Tags" : query.tags.joined(separator: " ")) + PlatformSpecificToolbarItem(placement: .navigation) { + Menu { + ForEach( + Array(manager.searchHistory.enumerated().filter { $0.offset > manager.historyIndex }), + id: \.offset + ) { offset, query in + Button(action: { + manager.historyIndex = offset + }) { + Text(query.tags.isEmpty ? "No Tags" : query.tags.joined(separator: " ")) + } + } + } label: { + Label("Next Search", systemImage: "chevron.right") + } primaryAction: { + withAnimation { + manager.goForwardInHistory() } } - } label: { - Label("Next Search", systemImage: "chevron.right") - } primaryAction: { - withAnimation { - manager.goForwardInHistory() - } + .disabled(!manager.canGoForwardInHistory) } - .disabled(!manager.canGoForwardInHistory) } - } - .navigationTitle("Posts") - .refreshable { - manager.clearCachedPages() + .navigationTitle("Posts") + .refreshable { + manager.clearCachedPages() - Task(priority: .userInitiated) { - await manager.fetchPosts(page: 1, tags: manager.tags, replace: true) + Task(priority: .userInitiated) { + await manager.fetchPosts(page: 1, tags: manager.tags, replace: true) + } } - } - .sheet(isPresented: $isSearchHistoryPresented) { - PostGridSearchHistoryView( - selectedTab: $selectedTab, - isPresented: $isSearchHistoryPresented - ) - } - #if os(iOS) - .gesture( - DragGesture() - .onEnded { value in - if value.startLocation.x < 50 && value.translation.width > 100 { - withAnimation { - manager.goBackInHistory() + .sheet(isPresented: $isSearchHistoryPresented) { + PostGridSearchHistoryView( + selectedTab: $selectedTab, + isPresented: $isSearchHistoryPresented + ) + } + #if os(iOS) + .gesture( + DragGesture() + .onEnded { value in + if value.startLocation.x < 50 && value.translation.width > 100 { + withAnimation { + manager.goBackInHistory() + } + + debugPrint("ContentView: Swipe left, \(manager.searchHistory)") } - debugPrint("ContentView: Swipe left, \(manager.searchHistory)") - } + if value.startLocation.x > (UIScreen.main.bounds.width - 50) + && value.translation.width < -100 + { + withAnimation { + manager.goForwardInHistory() + } - if value.startLocation.x > (UIScreen.main.bounds.width - 50) - && value.translation.width < -100 - { - withAnimation { - manager.goForwardInHistory() + debugPrint("ContentView: Swipe right, \(manager.searchHistory)") } - - debugPrint("ContentView: Swipe right, \(manager.searchHistory)") } - } - ) - #endif + ) + #endif + } } private func waterfallGridContent(post: BooruPost) -> some View { -- cgit v1.2.3