diff options
| author | Fuwn <[email protected]> | 2025-03-15 16:47:05 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-03-15 16:47:05 -0700 |
| commit | cb33fc700fffffd2c1a2f507145fc919e7dac3dd (patch) | |
| tree | 1a75c848ee4ac99356e0722b657d03f0c55de0e7 | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-cb33fc700fffffd2c1a2f507145fc919e7dac3dd.tar.xz sora-testing-cb33fc700fffffd2c1a2f507145fc919e7dac3dd.zip | |
feat: Development commit
| -rw-r--r-- | Sora/Data/Booru/BooruManager.swift | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/Sora/Data/Booru/BooruManager.swift b/Sora/Data/Booru/BooruManager.swift index 6cd3b3b..31a25c9 100644 --- a/Sora/Data/Booru/BooruManager.swift +++ b/Sora/Data/Booru/BooruManager.swift @@ -36,14 +36,24 @@ class BooruManager: ObservableObject { .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } .filter { !$0.isEmpty } } + var canGoBackInHistory: Bool { historyIndex > 0 } + var canGoForwardInHistory: Bool { historyIndex < searchHistory.count - 1 } - // MARK: - Initialization + // MARK: - Initialisation init(_ provider: BooruProvider) { self.provider = provider self.flavor = BooruProviderFlavor(provider: provider) self.domain = provider.domain pageCache.countLimit = 50 pageCache.totalCostLimit = 50 * 1_024 * 1_024 + + let rootQuery = BooruSearchQuery( + provider: provider, + tags: [] + ) + + searchHistory.append(rootQuery) + historyIndex = 0 } // MARK: - Public Methods @@ -77,6 +87,7 @@ class BooruManager: ObservableObject { currentTask?.cancel() + currentTask = nil currentTask = Task { defer { isLoading = false } @@ -101,18 +112,29 @@ class BooruManager: ObservableObject { } func performSearch(settings: SettingsManager? = nil) { + if searchHistory.last?.tags == tags { return } + + if historyIndex < searchHistory.count - 1 { + searchHistory = Array(searchHistory[0...historyIndex]) + } + let query = BooruSearchQuery( provider: settings?.preferredBooru ?? provider, tags: tags ) - settings?.appendToSearchHistory(query) searchHistory.append(query) historyIndex = searchHistory.count - 1 + settings?.appendToSearchHistory(query) + + searchText = tags.joined(separator: " ") + currentTask?.cancel() + currentTask = nil + Task { await fetchPosts(page: 1, tags: tags, replace: true) } } @@ -134,7 +156,7 @@ class BooruManager: ObservableObject { } func goBackInHistory() { - guard historyIndex > 0 else { return } + guard canGoBackInHistory else { return } historyIndex -= 1 @@ -142,18 +164,21 @@ class BooruManager: ObservableObject { searchText = previousQuery.tags.joined(separator: " ") currentPage = previousQuery.page + posts = [] + + if tags.isEmpty { clearPageCache() } + + currentTask?.cancel() + + currentTask = nil Task { - await fetchPosts( - page: currentPage, - tags: previousQuery.tags, - replace: true - ) + await fetchPosts(page: currentPage, tags: previousQuery.tags, replace: true) } } func goForwardInHistory() { - guard historyIndex < searchHistory.count - 1 else { return } + guard canGoForwardInHistory else { return } historyIndex += 1 @@ -161,6 +186,12 @@ class BooruManager: ObservableObject { searchText = nextQuery.tags.joined(separator: " ") currentPage = nextQuery.page + posts = [] + + clearPageCache() + currentTask?.cancel() + + currentTask = nil Task { await fetchPosts(page: currentPage, tags: nextQuery.tags, replace: true) |