summaryrefslogtreecommitdiff
path: root/Sora/Data/Settings/Settings.swift
blob: 5aac056f6f34ce659cc6d49b863819b8e65b8f82 (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
import SwiftUI

class Settings: ObservableObject {
  #if DEBUG
    @AppStorage("detailViewType")
    var detailViewType: BooruPostFileType = .sample
  #else
    @AppStorage("detailViewType")
    var detailViewType: BooruPostFileType = .original
  #endif

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

  @AppStorage("searchSuggestions")
  var searchSuggestions: Bool = false

  @AppStorage("columns")
  var columns: Int = 2

  @AppStorage("blurNSFWThumbnails")
  var blurNSFWThumbnails: Bool = true

  @AppStorage("showNSFWPosts")
  var showNSFWPosts: Bool = false

  @AppStorage("bookmarks")
  private var bookmarksData: Data = .init()

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

  @AppStorage("enableShareShortcut")
  var enableShareShortcut: Bool = false

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

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