summaryrefslogtreecommitdiff
path: root/Sora/Data/Settings/SettingsManager.swift
blob: 9ffc53b576083b24dc47047ea48fad7c23dd1b16 (plain) (blame)
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
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) ?? [] }

    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) ?? [] }

    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 }
  }

  // MARK: - Search History Management
  func removeSearchHistoryEntry(at offsets: IndexSet) {
    searchHistory.remove(atOffsets: offsets)
  }

  func removeSearchHistoryEntry(withTags tags: [String]) {
    searchHistory.removeAll { $0.tags.contains(where: tags.contains) }
  }

  func removeSearchHistoryEntry(withID id: UUID) {
    searchHistory.removeAll { $0.id == id }
  }
}