summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-07-08 04:08:00 -0700
committerFuwn <[email protected]>2025-07-08 04:08:00 -0700
commit29285f2ff0e154ea16d14c74611e2afa4f657a15 (patch)
tree458217256b47a4f5a35d9c788f2599175dd98c68
parentfeat: Development commit (diff)
downloadsora-testing-29285f2ff0e154ea16d14c74611e2afa4f657a15.tar.xz
sora-testing-29285f2ff0e154ea16d14c74611e2afa4f657a15.zip
feat: Development commit
-rw-r--r--Sora/Data/Booru/BooruManager.swift68
1 files changed, 43 insertions, 25 deletions
diff --git a/Sora/Data/Booru/BooruManager.swift b/Sora/Data/Booru/BooruManager.swift
index 9ed01fa..284ad25 100644
--- a/Sora/Data/Booru/BooruManager.swift
+++ b/Sora/Data/Booru/BooruManager.swift
@@ -2,7 +2,7 @@ import Alamofire
import SwiftUI
@MainActor
-class BooruManager: ObservableObject {
+class BooruManager: ObservableObject { // swiftlint:disable:this type_body_length
// MARK: - Published Properties
@Published var posts: [BooruPost] = []
@Published var isLoading = false
@@ -85,36 +85,54 @@ class BooruManager: ObservableObject {
isLoading = true
- do {
- let data = try await requestURL(url)
- let flavor = self.flavor
- let provider = self.provider
- let newPosts = await withCheckedContinuation { continuation in
- DispatchQueue.global(qos: .userInitiated).async {
- let parsedPosts = self.parsePosts(
- from: data,
- flavor: flavor,
- provider: provider
- )
- .sorted { $0.id > $1.id }
-
- continuation.resume(returning: parsedPosts)
+ defer { isLoading = false }
+
+ var finalPosts: [BooruPost] = []
+ let maxAttempts = 4
+
+ for attempt in 1...maxAttempts {
+ do {
+ let data = try await requestURL(url)
+ let flavor = self.flavor
+ let provider = self.provider
+ let newPosts = await withCheckedContinuation { continuation in
+ DispatchQueue.global(qos: .userInitiated).async {
+ let parsedPosts = self.parsePosts(
+ from: data,
+ flavor: flavor,
+ provider: provider
+ )
+ .sorted { $0.id > $1.id }
+
+ continuation.resume(returning: parsedPosts)
+ }
}
- }
- let cacheEntry = BooruPageCacheEntry(posts: newPosts, timestamp: Date())
- pageCache.setObject(cacheEntry, forKey: cacheKey)
+ if !newPosts.isEmpty {
+ finalPosts = newPosts
- withAnimation(nil) {
- updatePosts(newPosts, replace: replace)
- }
- } catch {
- self.error = error
+ break
+ }
- debugPrint("BooruManager.fetchPosts: \(error)")
+ if attempt < maxAttempts {
+ try await Task.sleep(for: .seconds(0.5 * Double(attempt)))
+ }
+ } catch {
+ self.error = error
+
+ debugPrint("BooruManager.fetchPosts(\(attempt)): \(error)")
+
+ break
+ }
}
- isLoading = false
+ let cacheEntry = BooruPageCacheEntry(posts: finalPosts, timestamp: Date())
+
+ pageCache.setObject(cacheEntry, forKey: cacheKey)
+
+ withAnimation(nil) {
+ updatePosts(finalPosts, replace: replace)
+ }
}
func clearCachedPages() {