summaryrefslogtreecommitdiff
path: root/Sora/Data/Settings/Settings.swift
blob: 1cd19de6a0ceb7cf97e58dccda0e2fbed5c69ba6 (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
135
136
137
138
139
import SwiftUI

class Settings: ObservableObject {
  @AppStorage("detailViewType")
  var detailViewType: BooruPostFileType = .original

  @AppStorage("thumbnailType")
  var thumbnailType: BooruPostFileType = .preview

  @AppStorage("searchSuggestions")
  var searchSuggestions = false

  @AppStorage("columns")
  var columns = 2

  @AppStorage("blurNSFWThumbnails")
  var blurNSFWThumbnails = true

  @AppStorage("showNSFWPosts")
  var showNSFWPosts = false

  @AppStorage("bookmarks")
  private var bookmarksData = Data()

  @AppStorage("preferredBooru")
  var preferredBooru: BooruProvider = .yandere

  @AppStorage("enableShareShortcut")
  var enableShareShortcut = false

  @AppStorage("displayRatings")
  private var displayRatingsData = Settings.defaultRatingsData()

  @AppStorage("blurRatings")
  private var blurRatingsData = Settings.defaultRatingsData()

  @AppStorage("displayTags")
  var displayTags = true

  #if os(macOS)
    @AppStorage("saveTagsToFile")
    var saveTagsToFile = true
  #endif

  var bookmarks: [Bookmark] {
    get {
      if let bookmarks = try? JSONDecoder().decode([Bookmark].self, from: bookmarksData) {
        return bookmarks
      }

      return []
    }

    set {
      if let data = try? JSONEncoder().encode(newValue) {
        bookmarksData = data
      }
    }
  }

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

  private static func defaultRatingsData() -> Data {
    do {
      return try JSONEncoder().encode(BooruRating.allCases)
    } catch {
      return Data()
    }
  }

  private static func decodeRatings(from data: Data) -> [BooruRating] {
    if let ratings = try? JSONDecoder().decode([BooruRating].self, from: data) {
      return ratings
    }

    return BooruRating.allCases
  }

  private static func encodeRatings(_ ratings: [BooruRating]) -> Data? {
    try? JSONEncoder().encode(ratings)
  }

  func resetToDefaults() {
    #if DEBUG
      detailViewType = .preview
    #else
      detailViewType = .original
    #endif
    thumbnailType = .preview
    searchSuggestions = false
    columns = 2
    blurNSFWThumbnails = true
    showNSFWPosts = false
  }

  func addBookmark(provider: BooruProvider, tags: [String]) {
    var currentBookmarks = bookmarks

    currentBookmarks.append(Bookmark(provider: provider, tags: tags.map { $0.lowercased() }))

    bookmarks = currentBookmarks
  }

  func removeBookmark(at index: IndexSet) {
    var currentBookmarks = bookmarks

    currentBookmarks.remove(atOffsets: index)

    bookmarks = currentBookmarks
  }

  func removeBookmark(withTags tags: [String]) {
    var currentBookmarks = bookmarks

    currentBookmarks.removeAll { bookmark in
      bookmark.tags.contains(where: tags.contains)
    }

    bookmarks = currentBookmarks
  }

  func removeBookmark(withID: UUID) {
    var currentBookmarks = bookmarks

    currentBookmarks.removeAll { bookmark in
      bookmark.id == withID
    }

    bookmarks = currentBookmarks
  }
}