1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
import SwiftUI
class SettingsManager: ObservableObject {
// MARK: - Stored Properties
@AppStorage("detailViewType")
var detailViewQuality: BooruPostFileType = .original
@AppStorage("thumbnailQuality")
var thumbnailQuality: BooruPostFileType = .preview
@AppStorage("searchSuggestionsMode")
var searchSuggestionsMode: SettingsSearchSuggestionsMode = .disabled
@AppStorage("thumbnailGridColumns")
var thumbnailGridColumns = 2
@AppStorage("preferredBooru")
var preferredBooru: BooruProvider = .safebooru
@AppStorage("enableShareShortcut")
var enableShareShortcut = false
@AppStorage("displayDetailsInformationBar")
var displayDetailsInformationBar = true
@AppStorage("preloadedCarouselImages")
var preloadedCarouselImages = 3
#if os(macOS)
@AppStorage("saveTagsToFile")
var saveTagsToFile = false
#endif
// MARK: - Codable Properties
@AppStorage("bookmarks")
private var bookmarksData = Data()
@AppStorage("displayRatings")
private var displayRatingsData = SettingsManager.encode(BooruRating.allCases) ?? Data()
@AppStorage("blurRatings")
private var blurRatingsData = SettingsManager.encode([.explicit as BooruRating]) ?? Data()
@AppStorage("searchHistory")
private var searchHistoryData = Data()
// MARK: - Computed Properties
var bookmarks: [SettingsBookmark] {
get {
(Self.decode([SettingsBookmark].self, from: bookmarksData) ?? [])
.sorted { $0.date > $1.date }
}
set { bookmarksData = Self.encode(newValue) ?? bookmarksData }
}
var displayRatings: [BooruRating] {
get {
Self.decode([BooruRating].self, from: displayRatingsData) ?? BooruRating.allCases
}
set { displayRatingsData = Self.encode(newValue) ?? displayRatingsData }
}
var blurRatings: [BooruRating] {
get { Self.decode([BooruRating].self, from: blurRatingsData) ?? [.explicit] }
set { blurRatingsData = Self.encode(newValue) ?? blurRatingsData }
}
var searchHistory: [BooruSearchQuery] {
get {
(Self.decode([BooruSearchQuery].self, from: searchHistoryData) ?? [])
.sorted { $0.date > $1.date }
}
set { searchHistoryData = Self.encode(newValue) ?? searchHistoryData }
}
// MARK: - Private Helpers
private static func encode<T: Encodable>(_ value: T) -> Data? {
try? JSONEncoder().encode(value)
}
private static func decode<T: Decodable>(_ type: T.Type, from data: Data) -> T? {
try? JSONDecoder().decode(type, from: data)
}
// MARK: - Public Methods
func appendToSearchHistory(_ query: BooruSearchQuery) {
self.searchHistory.append(query)
}
func resetToDefaults() {
detailViewQuality = .original
thumbnailQuality = .preview
searchSuggestionsMode = .disabled
thumbnailGridColumns = 2
preferredBooru = .safebooru
enableShareShortcut = false
displayRatings = BooruRating.allCases
blurRatings = [.explicit]
displayDetailsInformationBar = true
preloadedCarouselImages = 3
#if os(macOS)
saveTagsToFile = false
#endif
}
// MARK: - Bookmark Management
func addBookmark(provider: BooruProvider, tags: [String]) {
bookmarks.append(SettingsBookmark(provider: provider, tags: tags.map { $0.lowercased() }))
}
func removeBookmark(at offsets: IndexSet) {
bookmarks.remove(atOffsets: offsets)
}
func removeBookmark(withTags tags: [String]) {
bookmarks.removeAll { $0.tags.contains(where: tags.contains) }
}
func removeBookmark(withID id: UUID) {
bookmarks.removeAll { $0.id == id }
}
func exportBookmarks() throws -> Data {
try JSONEncoder().encode(bookmarks)
}
func importBookmarks(from data: Data) throws {
let importedBookmarks = try JSONDecoder().decode([SettingsBookmark].self, from: data)
let existingIDs = Set(bookmarks.map(\.id))
let newBookmarks = importedBookmarks.filter { !existingIDs.contains($0.id) }
bookmarks.append(contentsOf: newBookmarks)
}
// MARK: - Search History Management
func removeSearchHistoryEntry(at offsets: IndexSet) {
searchHistory.remove(atOffsets: offsets)
}
func removeSearchHistoryEntry(withID id: UUID) {
searchHistory.removeAll { $0.id == id }
}
#if DEBUG
// https://stackoverflow.com/a/68926484/14452787
private func randomWord() -> String {
var word = ""
for _ in 0..<5 {
word += String(format: "%c", Int.random(in: 97..<123)) as String
}
return word
}
func addDummyBookmarks() {
for _ in 0..<10 {
let randomTags: [String] = Array(repeating: randomWord(), count: Int.random(in: 1...5))
addBookmark(provider: .safebooru, tags: randomTags)
}
}
func addDummySearchHistory() {
for _ in 0..<10 {
let randomTags: [String] = Array(repeating: randomWord(), count: Int.random(in: 1...5))
appendToSearchHistory(
BooruSearchQuery(provider: .safebooru, tags: randomTags, searchedAt: Date())
)
}
}
#endif
}
|