diff options
| author | Fuwn <[email protected]> | 2025-09-10 03:52:04 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-09-10 03:52:04 -0700 |
| commit | 9803d532bad9ff26dfb4dfdd1a37392ccb8ae1dd (patch) | |
| tree | c0663ec34fe57aa7a8c168bd63d08c905d4bf6d6 /Sora | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-9803d532bad9ff26dfb4dfdd1a37392ccb8ae1dd.tar.xz sora-testing-9803d532bad9ff26dfb4dfdd1a37392ccb8ae1dd.zip | |
feat: Development commit
Diffstat (limited to 'Sora')
| -rw-r--r-- | Sora/Views/Post/Grid/PostGridView.swift | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/Sora/Views/Post/Grid/PostGridView.swift b/Sora/Views/Post/Grid/PostGridView.swift index 6d96bdc..518b305 100644 --- a/Sora/Views/Post/Grid/PostGridView.swift +++ b/Sora/Views/Post/Grid/PostGridView.swift @@ -23,6 +23,7 @@ struct PostGridView: View { // swiftlint:disable:this type_body_length @State private var localEndOfData = false @State private var localError: Error? @State private var hasAppearedBefore = false + @State private var currentLocalTask: Task<Void, Never>? init( selectedTab: Binding<Int>, navigationPath: Binding<NavigationPath>, initialTag: String? = nil @@ -252,7 +253,7 @@ struct PostGridView: View { // swiftlint:disable:this type_body_length await performLocalSearch() } } else { - Task(priority: .userInitiated) { + currentLocalTask = Task(priority: .userInitiated) { await fetchLocalPosts( page: 1, tags: localSearchText.components(separatedBy: .whitespaces).filter { component in @@ -268,8 +269,8 @@ struct PostGridView: View { // swiftlint:disable:this type_body_length #if os(macOS) ToolbarItem { Button(action: { - Task { - if initialTag != nil { + if initialTag != nil { + currentLocalTask = Task(priority: .userInitiated) { await fetchLocalPosts( page: 1, tags: localSearchText.components(separatedBy: .whitespaces).filter { component in @@ -277,7 +278,9 @@ struct PostGridView: View { // swiftlint:disable:this type_body_length }, replace: true ) - } else { + } + } else { + Task(priority: .userInitiated) { await manager.fetchPosts(page: 1, tags: manager.tags, replace: true) } } @@ -395,13 +398,16 @@ struct PostGridView: View { // swiftlint:disable:this type_body_length .navigationTitle(initialTag != nil ? initialTag! : "Posts") .refreshable { if initialTag != nil { - await fetchLocalPosts( - page: 1, - tags: localSearchText.components(separatedBy: .whitespaces).filter { component in - !component.isEmpty - }, - replace: true - ) + currentLocalTask = Task(priority: .userInitiated) { + await fetchLocalPosts( + page: 1, + tags: localSearchText.components(separatedBy: .whitespaces).filter { component in + !component.isEmpty + }, + replace: true + ) + } + await currentLocalTask?.value } else { manager.clearCachedPages() Task(priority: .userInitiated) { @@ -516,9 +522,14 @@ struct PostGridView: View { // swiftlint:disable:this type_body_length ) async { guard !localIsLoading else { return } + currentLocalTask?.cancel() + localIsLoading = true - defer { localIsLoading = false } + defer { + localIsLoading = false + currentLocalTask = nil + } let flavor = manager.flavor let provider = manager.provider @@ -528,6 +539,9 @@ struct PostGridView: View { // swiftlint:disable:this type_body_length do { let data = try await manager.requestURL(url) + + guard !Task.isCancelled else { return } + let newPosts = await withCheckedContinuation { continuation in DispatchQueue.global(qos: .userInitiated).async { let parsedPosts = BooruManager.parsePosts( @@ -541,6 +555,8 @@ struct PostGridView: View { // swiftlint:disable:this type_body_length } } + guard !Task.isCancelled else { return } + withAnimation(nil) { if replace { localPosts = newPosts @@ -552,9 +568,11 @@ struct PostGridView: View { // swiftlint:disable:this type_body_length localEndOfData = newPosts.isEmpty } } catch { - localError = error + if (error as? URLError)?.code != .cancelled { + localError = error - debugPrint("PostGridView.fetchLocalPosts: \(error)") + debugPrint("PostGridView.fetchLocalPosts: \(error)") + } } } } |