diff options
| author | Fuwn <[email protected]> | 2025-06-18 04:35:13 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-06-18 04:35:13 -0700 |
| commit | ebb7e7d3c44e116b91857f23b2f1bcc477a23ed2 (patch) | |
| tree | 74557ed77534762071cff4c6e194299c98691018 /Sora/Data | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-ebb7e7d3c44e116b91857f23b2f1bcc477a23ed2.tar.xz sora-testing-ebb7e7d3c44e116b91857f23b2f1bcc477a23ed2.zip | |
feat: Development commit
Diffstat (limited to 'Sora/Data')
| -rw-r--r-- | Sora/Data/Booru/BooruManager.swift | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/Sora/Data/Booru/BooruManager.swift b/Sora/Data/Booru/BooruManager.swift index 705d4d8..6361f88 100644 --- a/Sora/Data/Booru/BooruManager.swift +++ b/Sora/Data/Booru/BooruManager.swift @@ -36,8 +36,7 @@ class BooruManager: ObservableObject { // swiftlint:disable:this type_body_leng searchText.isEmpty ? [] : searchText - .split(separator: " ") - .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } + .components(separatedBy: .whitespaces) .filter { !$0.isEmpty } } var canGoBackInHistory: Bool { historyIndex > 0 } @@ -71,9 +70,10 @@ class BooruManager: ObservableObject { // swiftlint:disable:this type_body_leng 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 } + guard !isLoading else { return } + + let pageValue = flavor == .gelbooru ? page - 1 : page + guard let url = urlForPosts(page: pageValue, limit: limit, tags: tags) else { return } let cacheKey = "\(url.absoluteString)_\(replace)" as NSString // swiftlint:disable:this legacy_objc_type @@ -128,7 +128,9 @@ class BooruManager: ObservableObject { // swiftlint:disable:this type_body_leng } func performSearch(settings: SettingsManager? = nil) async { - if searchHistory.last?.tags == tags { return } + let inputTags = tags + + if searchHistory.last?.tags == inputTags { return } if historyIndex < searchHistory.count - 1 { searchHistory = Array(searchHistory[0...historyIndex]) @@ -136,7 +138,7 @@ class BooruManager: ObservableObject { // swiftlint:disable:this type_body_leng let query = BooruSearchQuery( provider: settings?.preferredBooru ?? provider, - tags: tags + tags: inputTags ) searchHistory.append(query) @@ -145,9 +147,9 @@ class BooruManager: ObservableObject { // swiftlint:disable:this type_body_leng settings?.appendToSearchHistory(query) - searchText = tags.joined(separator: " ") + searchText = inputTags.joined(separator: " ") - await fetchPosts(page: 1, tags: tags, replace: true) + await fetchPosts(page: 1, tags: inputTags, replace: true) } func loadNextPage() async { @@ -313,13 +315,14 @@ class BooruManager: ObservableObject { // swiftlint:disable:this type_body_leng withTransaction(Transaction(animation: nil)) { self.posts += chunk - self.postIndexMap.merge( - Dictionary( - uniqueKeysWithValues: chunk.enumerated().map { post in - (post.element.id, post.offset) - } - ) - ) { _, newIndex in newIndex } + let startIndex = self.posts.count + let indexMap = Dictionary( + uniqueKeysWithValues: chunk.enumerated().map { offset, post in + (post.id, startIndex + offset) + } + ) + + self.postIndexMap.merge(indexMap) { _, newIndex in newIndex } } } } @@ -327,9 +330,15 @@ class BooruManager: ObservableObject { // swiftlint:disable:this type_body_leng } private func saveTagsToCache() { - try? tagsCacheFileURL.map { url in - try JSONEncoder().encode(allTags).write(to: url) - updateTagsCacheSize() + if let url = tagsCacheFileURL { + do { + let data = try JSONEncoder().encode(allTags) + + try data.write(to: url) + updateTagsCacheSize() + } catch { + debugPrint("BooruManager.saveTagsToCache:", error) + } } } |