diff options
| author | Fuwn <[email protected]> | 2025-07-24 22:24:24 +0200 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-07-24 22:24:24 +0200 |
| commit | 3ee9aeda00e6f9b5701a70454eabb6e0ea98de50 (patch) | |
| tree | b75c6271d1b7b567e611e174100d3f95c6620d42 /Sora | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-3ee9aeda00e6f9b5701a70454eabb6e0ea98de50.tar.xz sora-testing-3ee9aeda00e6f9b5701a70454eabb6e0ea98de50.zip | |
feat: Development commit
Diffstat (limited to 'Sora')
| -rw-r--r-- | Sora/Data/Settings/SettingsManager.swift | 124 |
1 files changed, 102 insertions, 22 deletions
diff --git a/Sora/Data/Settings/SettingsManager.swift b/Sora/Data/Settings/SettingsManager.swift index 0d20dfa..684cc91 100644 --- a/Sora/Data/Settings/SettingsManager.swift +++ b/Sora/Data/Settings/SettingsManager.swift @@ -85,8 +85,6 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l localData: $bookmarksData, newValue: newValue ) { $0.sorted { $0.date > $1.date } } - loadBookmarksCache() - backupBookmarks() } } @@ -113,8 +111,6 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l set { displayRatingsData = Self.encode(newValue) ?? displayRatingsData - - loadDisplayRatingsCache() } } @@ -123,8 +119,6 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l set { blurRatingsData = Self.encode(newValue) ?? blurRatingsData - - loadBlurRatingsCache() } } @@ -137,7 +131,6 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l localData: $searchHistoryData, newValue: newValue, ) { $0.sorted { $0.date > $1.date } } - loadSearchHistoryCache() } } @@ -269,6 +262,54 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l thumbnailQualityCache = _thumbnailQuality } + func updateBookmarks(_ newValue: [SettingsBookmark]) { + syncableData( + key: "bookmarks", + localData: $bookmarksData, + newValue: newValue + ) { $0.sorted { $0.date > $1.date } } + loadBookmarksCache() + backupBookmarks() + } + + func updateSearchHistory(_ newValue: [BooruSearchQuery]) { + syncableData( + key: "searchHistory", + localData: $searchHistoryData, + newValue: newValue, + ) { $0.sorted { $0.date > $1.date } } + loadSearchHistoryCache() + } + + func updateDisplayRatings(_ newValue: [BooruRating]) { + displayRatingsData = Self.encode(newValue) ?? displayRatingsData + + loadDisplayRatingsCache() + } + + func updateBlurRatings(_ newValue: [BooruRating]) { + blurRatingsData = Self.encode(newValue) ?? blurRatingsData + + loadBlurRatingsCache() + } + + func updateProviderCredentials(_ newValue: [BooruProviderCredentials]) { + let existingCredentials: [BooruProviderCredentials] = + Self.decode([BooruProviderCredentials].self, from: providerCredentialsData) ?? [] + let rawCredentials = newValue.map { credentials in + (provider: credentials.provider, apiKey: credentials.apiKey, userID: credentials.userID) + } + let mergedCredentials = BooruProviderCredentials.from( + rawCredentials, existingCredentials: existingCredentials + ) + + syncableData( + key: "providerAPIKeys", + localData: $providerCredentialsData, + newValue: mergedCredentials, + ) { $0 } + } + // MARK: - Private Helpers private static func encode<T: Encodable>(_ value: T) -> Data? { try? JSONEncoder().encode(value) @@ -417,7 +458,11 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l // MARK: - Public Methods func appendToSearchHistory(_ query: BooruSearchQuery) { - self.searchHistory.append(query) + var updated = searchHistory + + updated.append(query) + + updateSearchHistory(updated) } func resetToDefaults() { @@ -427,8 +472,10 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l thumbnailGridColumns = 2 preferredBooru = .safebooru enableShareShortcut = false - displayRatings = BooruRating.allCases - blurRatings = [.explicit] + + updateDisplayRatings(BooruRating.allCases) + updateBlurRatings([.explicit]) + displayDetailsInformationBar = true preloadedCarouselImages = 3 @@ -523,22 +570,30 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l ) if let data = Self.encode(updatedBookmarks), data.count < 1_000_000 { // 1 MB - bookmarks = updatedBookmarks + updateBookmarks(updatedBookmarks) } else { debugPrint("SettingsManager.addBookmark: iCloud data limit exceeded") } } func removeBookmark(at offsets: IndexSet) { - bookmarks.remove(atOffsets: offsets) + var updated = bookmarks + + updated.remove(atOffsets: offsets) + + updateBookmarks(updated) } func removeBookmark(withTags tags: [String]) { - bookmarks.removeAll { $0.tags.contains(where: tags.contains) } + let updated = bookmarks.filter { !$0.tags.contains(where: tags.contains) } + + updateBookmarks(updated) } func removeBookmark(withID id: UUID) { - bookmarks.removeAll { $0.id == id } + let updated = bookmarks.filter { $0.id != id } + + updateBookmarks(updated) } func exportBookmarks() throws -> Data { @@ -549,14 +604,20 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l let importedBookmarks = try JSONDecoder().decode([SettingsBookmark].self, from: data) let existingBookmarkIDs = Set(bookmarks.map(\.id)) let newBookmarks = importedBookmarks.filter { !existingBookmarkIDs.contains($0.id) } + var updated = bookmarks + + updated.append(contentsOf: newBookmarks) - bookmarks.append(contentsOf: newBookmarks) + updateBookmarks(updated) } func updateBookmarkFolder(withID id: UUID, folder: UUID?) { guard let index = bookmarks.firstIndex(where: { $0.id == id }) else { return } + var updated = bookmarks + + updated[index].folder = folder - bookmarks[index].folder = folder + updateBookmarks(updated) Task { @MainActor in self.syncToCloud() @@ -565,8 +626,11 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l func updateBookmarkLastVisit(withID id: UUID, date: Date = Date()) { guard let index = bookmarks.firstIndex(where: { $0.id == id }) else { return } + var updated = bookmarks - bookmarks[index].lastVisit = date + updated[index].lastVisit = date + + updateBookmarks(updated) Task { @MainActor in self.syncToCloud() @@ -575,8 +639,11 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l func incrementBookmarkVisitCount(withID id: UUID) { guard let index = bookmarks.firstIndex(where: { $0.id == id }) else { return } + var updated = bookmarks + + updated[index].visitedCount += 1 - bookmarks[index].visitedCount += 1 + updateBookmarks(updated) Task { @MainActor in self.syncToCloud() @@ -588,18 +655,31 @@ class SettingsManager: ObservableObject { // swiftlint:disable:this type_body_l } func renameFolder(_ folder: SettingsFolder, to newName: String) { - guard let index = folders.firstIndex(where: { $0.id == folder.id }) else { return } + var updated = folders + guard let index = updated.firstIndex(where: { $0.id == folder.id }) else { return } - folders[index].name = newName + updated[index].name = newName + + syncableData( + key: "folders", + localData: $foldersData, + newValue: updated, + ) { $0 } } // MARK: Search History Management func removeSearchHistoryEntry(at offsets: IndexSet) { - searchHistory.remove(atOffsets: offsets) + var updated = searchHistory + + updated.remove(atOffsets: offsets) + + updateSearchHistory(updated) } func removeSearchHistoryEntry(withID id: UUID) { - searchHistory.removeAll { $0.id == id } + let updated = searchHistory.filter { $0.id != id } + + updateSearchHistory(updated) } #if DEBUG |