diff options
| author | Fuwn <[email protected]> | 2025-03-01 20:17:15 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-03-01 20:17:15 -0800 |
| commit | 5e1f8ae9e80b0df2fe0407098adacca6f879e8c0 (patch) | |
| tree | b747c328ee784f297eb633c86eae799712a98cbe | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-5e1f8ae9e80b0df2fe0407098adacca6f879e8c0.tar.xz sora-testing-5e1f8ae9e80b0df2fe0407098adacca6f879e8c0.zip | |
feat: Development commit
| -rw-r--r-- | Localizable.xcstrings | 3 | ||||
| -rw-r--r-- | Sora/Data/Settings/SettingsManager.swift | 152 |
2 files changed, 40 insertions, 115 deletions
diff --git a/Localizable.xcstrings b/Localizable.xcstrings index 3e6a136..a3390cc 100644 --- a/Localizable.xcstrings +++ b/Localizable.xcstrings @@ -156,6 +156,9 @@ "Preloaded Images" : { }, + "Preloaded Images: %lld" : { + + }, "Provider" : { "localizations" : { "ja" : { diff --git a/Sora/Data/Settings/SettingsManager.swift b/Sora/Data/Settings/SettingsManager.swift index 50cd6e0..9ffc53b 100644 --- a/Sora/Data/Settings/SettingsManager.swift +++ b/Sora/Data/Settings/SettingsManager.swift @@ -1,7 +1,7 @@ import SwiftUI class SettingsManager: ObservableObject { - // MARK: - + // MARK: - Stored Properties @AppStorage("detailViewType") var detailViewQuality: BooruPostFileType = .original @@ -31,98 +31,56 @@ class SettingsManager: ObservableObject { var saveTagsToFile = false #endif - // MARK: - + // MARK: - Codable Properties @AppStorage("bookmarks") private var bookmarksData = Data() @AppStorage("displayRatings") - private var displayRatingsData = SettingsManager.defaultRatingsData() + private var displayRatingsData = SettingsManager.encode(BooruRating.allCases) ?? Data() @AppStorage("blurRatings") - private var blurRatingsData = SettingsManager.initializeRatingsData( - enabledRatings: [.explicit] - ) + private var blurRatingsData = SettingsManager.encode([.explicit as BooruRating]) ?? Data() @AppStorage("searchHistory") private var searchHistoryData = Data() - // MARK: - + // MARK: - Computed Properties var bookmarks: [SettingsBookmark] { - get { - if let bookmarks = try? JSONDecoder().decode([SettingsBookmark].self, from: bookmarksData) { - return bookmarks - } - - return [] - } + get { Self.decode([SettingsBookmark].self, from: bookmarksData) ?? [] } - set { - if let data = try? JSONEncoder().encode(newValue) { - bookmarksData = data - } - } + set { bookmarksData = Self.encode(newValue) ?? bookmarksData } } var displayRatings: [BooruRating] { - get { Self.decodeRatings(from: displayRatingsData) } - set { if let data = Self.encodeRatings(newValue) { displayRatingsData = data } } - } - - var blurRatings: [BooruRating] { - get { Self.decodeRatings(from: blurRatingsData) } - set { if let data = Self.encodeRatings(newValue) { blurRatingsData = data } } - } - - var searchHistory: [BooruSearchQuery] { get { - if let history = try? JSONDecoder().decode([BooruSearchQuery].self, from: searchHistoryData) { - return history - } - - return [] + Self.decode([BooruRating].self, from: displayRatingsData) ?? BooruRating.allCases } - set { - if let data = try? JSONEncoder().encode(newValue) { - searchHistoryData = data - } - } + set { displayRatingsData = Self.encode(newValue) ?? displayRatingsData } } - // MARK: - - private static func defaultRatingsData() -> Data { - initializeRatingsData(enabledRatings: BooruRating.allCases) - } - - private static func initializeRatingsData(enabledRatings: [BooruRating]) -> Data { - var ratings: [BooruRating] = [] + var blurRatings: [BooruRating] { + get { Self.decode([BooruRating].self, from: blurRatingsData) ?? [.explicit] } - for rating in enabledRatings { - ratings.append(rating) - } + set { blurRatingsData = Self.encode(newValue) ?? blurRatingsData } + } - do { - return try JSONEncoder().encode(ratings) - } catch { - debugPrint("SettingsManager.initializeRatingsData: \(error)") + var searchHistory: [BooruSearchQuery] { + get { Self.decode([BooruSearchQuery].self, from: searchHistoryData) ?? [] } - return Data() - } + set { searchHistoryData = Self.encode(newValue) ?? searchHistoryData } } - private static func decodeRatings(from data: Data) -> [BooruRating] { - if let ratings = try? JSONDecoder().decode([BooruRating].self, from: data) { - return ratings - } - - return BooruRating.allCases + // MARK: - Private Helpers + private static func encode<T: Encodable>(_ value: T) -> Data? { + try? JSONEncoder().encode(value) } - private static func encodeRatings(_ ratings: [BooruRating]) -> Data? { - try? JSONEncoder().encode(ratings) + private static func decode<T: Decodable>(_ type: T.Type, from data: Data) -> T? { + try? JSONDecoder().decode(type, from: data) } - // MARK: - + // MARK: - Public Methods func appendToSearchHistory(_ query: BooruSearchQuery) { self.searchHistory.append(query) } @@ -134,8 +92,8 @@ class SettingsManager: ObservableObject { thumbnailGridColumns = 2 preferredBooru = .safebooru enableShareShortcut = false - displayRatingsData = Self.defaultRatingsData() - blurRatingsData = Self.initializeRatingsData(enabledRatings: [.explicit]) + displayRatings = BooruRating.allCases + blurRatings = [.explicit] displayDetailsInformationBar = true preloadedCarouselImages = 3 @@ -144,69 +102,33 @@ class SettingsManager: ObservableObject { #endif } + // MARK: - Bookmark Management func addBookmark(provider: BooruProvider, tags: [String]) { - var currentBookmarks = bookmarks - - currentBookmarks.append( - SettingsBookmark(provider: provider, tags: tags.map { $0.lowercased() }) - ) - - bookmarks = currentBookmarks + bookmarks.append(SettingsBookmark(provider: provider, tags: tags.map { $0.lowercased() })) } - func removeBookmark(at index: IndexSet) { - var currentBookmarks = bookmarks - - currentBookmarks.remove(atOffsets: index) - - bookmarks = currentBookmarks + func removeBookmark(at offsets: IndexSet) { + bookmarks.remove(atOffsets: offsets) } func removeBookmark(withTags tags: [String]) { - var currentBookmarks = bookmarks - - currentBookmarks.removeAll { bookmark in - bookmark.tags.contains(where: tags.contains) - } - - bookmarks = currentBookmarks + bookmarks.removeAll { $0.tags.contains(where: tags.contains) } } - func removeBookmark(withID: UUID) { - var currentBookmarks = bookmarks - - currentBookmarks.removeAll { bookmark in - bookmark.id == withID - } - - bookmarks = currentBookmarks + func removeBookmark(withID id: UUID) { + bookmarks.removeAll { $0.id == id } } - func removeSearchHistoryEntry(at index: IndexSet) { - var currentSearchHistory = searchHistory - - currentSearchHistory.remove(atOffsets: index) - - searchHistory = currentSearchHistory + // MARK: - Search History Management + func removeSearchHistoryEntry(at offsets: IndexSet) { + searchHistory.remove(atOffsets: offsets) } func removeSearchHistoryEntry(withTags tags: [String]) { - var currentSearchHistory = searchHistory - - currentSearchHistory.removeAll { query in - query.tags.contains(where: tags.contains) - } - - searchHistory = currentSearchHistory + searchHistory.removeAll { $0.tags.contains(where: tags.contains) } } - func removeSearchHistoryEntry(withID: UUID) { - var currentSearchHistory = searchHistory - - currentSearchHistory.removeAll { query in - query.id == withID - } - - searchHistory = currentSearchHistory + func removeSearchHistoryEntry(withID id: UUID) { + searchHistory.removeAll { $0.id == id } } } |