summaryrefslogtreecommitdiff
path: root/Sora/Data
diff options
context:
space:
mode:
authorFuwn <[email protected]>2025-06-15 05:22:14 -0700
committerFuwn <[email protected]>2025-06-15 05:22:14 -0700
commita70349e971e1b6e31642cd3b2e9b15881395e2f9 (patch)
tree1a16ad8b517ab325e74bbd4ce811fea8ca32fac5 /Sora/Data
parentfeat: Development commit (diff)
downloadsora-testing-a70349e971e1b6e31642cd3b2e9b15881395e2f9.tar.xz
sora-testing-a70349e971e1b6e31642cd3b2e9b15881395e2f9.zip
feat: Development commit
Diffstat (limited to 'Sora/Data')
-rw-r--r--Sora/Data/Booru/BooruManager.swift52
1 files changed, 22 insertions, 30 deletions
diff --git a/Sora/Data/Booru/BooruManager.swift b/Sora/Data/Booru/BooruManager.swift
index 1abc7b6..12d96f0 100644
--- a/Sora/Data/Booru/BooruManager.swift
+++ b/Sora/Data/Booru/BooruManager.swift
@@ -66,7 +66,8 @@ class BooruManager: ObservableObject {
updateTagsCacheSize()
}
- func fetchPosts(page: Int = 1, limit: Int = 100, tags: [String] = [], replace: Bool = false) {
+ func fetchPosts(page: Int = 1, limit: Int = 100, tags: [String] = [], replace: Bool = false) async
+ {
guard !isLoading,
let url = urlForPosts(page: flavor == .gelbooru ? page - 1 : page, limit: limit, tags: tags)
else { return }
@@ -87,32 +88,25 @@ class BooruManager: ObservableObject {
isLoading = true
- cancelCurrentTask()
-
- currentTask = Task {
- defer { isLoading = false }
-
- do {
- let data = try await requestURL(url)
-
- guard !Task.isCancelled else { return }
+ do {
+ let data = try await requestURL(url)
+ let newPosts = parsePosts(from: data).sorted { $0.id > $1.id }
+ let cacheEntry = BooruPageCacheEntry(posts: newPosts, timestamp: Date())
- let newPosts = parsePosts(from: data).sorted { $0.id > $1.id }
- let cacheEntry = BooruPageCacheEntry(posts: newPosts, timestamp: Date())
-
- pageCache.setObject(cacheEntry, forKey: cacheKey)
- updatePosts(newPosts, replace: replace)
- } catch {
- debugPrint("BooruManager.fetchPosts: \(error)")
- }
+ pageCache.setObject(cacheEntry, forKey: cacheKey)
+ updatePosts(newPosts, replace: replace)
+ } catch {
+ debugPrint("BooruManager.fetchPosts: \(error)")
}
+
+ isLoading = false
}
func clearCachedPages() {
pageCache.removeAllObjects()
}
- func performSearch(settings: SettingsManager? = nil) {
+ func performSearch(settings: SettingsManager? = nil) async {
if searchHistory.last?.tags == tags { return }
if historyIndex < searchHistory.count - 1 {
@@ -132,24 +126,22 @@ class BooruManager: ObservableObject {
searchText = tags.joined(separator: " ")
- cancelCurrentTask()
- fetchPosts(page: 1, tags: tags, replace: true)
+ await fetchPosts(page: 1, tags: tags, replace: true)
}
- func loadNextPage() {
+ func loadNextPage() async {
guard !isLoading else { return }
- Task {
- currentPage += 1
+ currentPage += 1
- fetchPosts(page: currentPage, tags: tags)
+ await fetchPosts(page: currentPage, tags: tags)
- if historyIndex >= 0 && historyIndex < searchHistory.count {
- var currentQuery = searchHistory[historyIndex]
+ if historyIndex >= 0 && historyIndex < searchHistory.count {
+ var currentQuery = searchHistory[historyIndex]
- currentQuery.page = currentPage
- searchHistory[historyIndex] = currentQuery
- }
+ currentQuery.page = currentPage
+
+ searchHistory[historyIndex] = currentQuery
}
}