aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Models/Preferences.swift
blob: 2266f835235fa57d42e2067e0847b036516e8716 (plain)
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
import SwiftUI

enum PrefsKeys: String {
  case username = "username"
  case userIconID = "user icon id"
  case refusePrivateMessages = "refuse private messages"
  case refusePrivateChat = "refuse private chat"
  case enableAutomaticMessage = "enable automatic message"
  case automaticMessage = "automatic message"
  case playSounds = "play sounds"
  case playChatSound = "play chat sound"
  case playFileTransferCompleteSound = "play file transfer complete sound"
  case playPrivateMessageSound = "play private message sound"
  case playJoinSound = "play join sound"
  case playLeaveSound = "play leave sound"
  case playLoggedInSound = "play logged in sound"
  case playErrorSound = "play error sound"
  case playChatInvitationSound = "play chat invitation sound"
}

@Observable
class Prefs {
  init() {
    UserDefaults.standard.register(defaults:[
      PrefsKeys.username.rawValue: "guest",
      PrefsKeys.userIconID.rawValue: 191,
      PrefsKeys.refusePrivateMessages.rawValue: false,
      PrefsKeys.refusePrivateChat.rawValue: false,
      PrefsKeys.enableAutomaticMessage.rawValue: false,
      PrefsKeys.automaticMessage.rawValue: "",
      PrefsKeys.playSounds.rawValue: true,
      PrefsKeys.playChatSound.rawValue: true,
      PrefsKeys.playFileTransferCompleteSound.rawValue: true,
      PrefsKeys.playPrivateMessageSound.rawValue: true,
      PrefsKeys.playJoinSound.rawValue: true,
      PrefsKeys.playLeaveSound.rawValue: true,
      PrefsKeys.playLoggedInSound.rawValue: true,
      PrefsKeys.playErrorSound.rawValue: true,
      PrefsKeys.playChatInvitationSound.rawValue: true,
    ])
    
    self.username = UserDefaults.standard.string(forKey: PrefsKeys.username.rawValue)!
    self.userIconID = UserDefaults.standard.integer(forKey: PrefsKeys.userIconID.rawValue)
    self.refusePrivateMessages = UserDefaults.standard.bool(forKey: PrefsKeys.refusePrivateMessages.rawValue)
    self.refusePrivateChat = UserDefaults.standard.bool(forKey: PrefsKeys.refusePrivateChat.rawValue)
    self.enableAutomaticMessage = UserDefaults.standard.bool(forKey: PrefsKeys.enableAutomaticMessage.rawValue)
    self.automaticMessage = UserDefaults.standard.string(forKey: PrefsKeys.automaticMessage.rawValue)!
    self.playSounds = UserDefaults.standard.bool(forKey: PrefsKeys.playSounds.rawValue)
    self.playChatSound = UserDefaults.standard.bool(forKey: PrefsKeys.playChatSound.rawValue)
    self.playFileTransferCompleteSound = UserDefaults.standard.bool(forKey: PrefsKeys.playFileTransferCompleteSound.rawValue)
    self.playPrivateMessageSound = UserDefaults.standard.bool(forKey: PrefsKeys.playPrivateMessageSound.rawValue)
    self.playJoinSound = UserDefaults.standard.bool(forKey: PrefsKeys.playJoinSound.rawValue)
    self.playLeaveSound = UserDefaults.standard.bool(forKey: PrefsKeys.playLeaveSound.rawValue)
    self.playLoggedInSound = UserDefaults.standard.bool(forKey: PrefsKeys.playLoggedInSound.rawValue)
    self.playErrorSound = UserDefaults.standard.bool(forKey: PrefsKeys.playErrorSound.rawValue)
    self.playChatInvitationSound = UserDefaults.standard.bool(forKey: PrefsKeys.playChatInvitationSound.rawValue)
  }
  
  public static let shared = Prefs()

  var username: String {
    didSet { UserDefaults.standard.set(self.username, forKey: PrefsKeys.username.rawValue) }
  }
  
  var userIconID: Int {
    didSet { UserDefaults.standard.set(self.userIconID, forKey: PrefsKeys.userIconID.rawValue) }
  }
  
  var refusePrivateMessages: Bool {
    didSet { UserDefaults.standard.set(self.refusePrivateMessages, forKey: PrefsKeys.refusePrivateMessages.rawValue) }
  }
  
  var playSounds: Bool {
    didSet { UserDefaults.standard.set(self.playSounds, forKey: PrefsKeys.playSounds.rawValue) }
  }
  
  var playChatSound: Bool {
    didSet { UserDefaults.standard.set(self.playChatSound, forKey: PrefsKeys.playChatSound.rawValue) }
  }
  
  var playFileTransferCompleteSound: Bool {
    didSet { UserDefaults.standard.set(self.playFileTransferCompleteSound, forKey: PrefsKeys.playFileTransferCompleteSound.rawValue) }
  }
  
  var playPrivateMessageSound: Bool {
    didSet { UserDefaults.standard.set(self.playPrivateMessageSound, forKey: PrefsKeys.playPrivateMessageSound.rawValue) }
  }
  
  var playJoinSound: Bool {
    didSet { UserDefaults.standard.set(self.playJoinSound, forKey: PrefsKeys.playJoinSound.rawValue) }
  }
  
  var playLeaveSound: Bool {
    didSet { UserDefaults.standard.set(self.playLeaveSound, forKey: PrefsKeys.playLeaveSound.rawValue) }
  }
  
  var playLoggedInSound: Bool {
    didSet { UserDefaults.standard.set(self.playLoggedInSound, forKey: PrefsKeys.playLoggedInSound.rawValue) }
  }
  
  var playErrorSound: Bool {
    didSet { UserDefaults.standard.set(self.playErrorSound, forKey: PrefsKeys.playErrorSound.rawValue) }
  }
  
  var playChatInvitationSound: Bool {
    didSet { UserDefaults.standard.set(self.playChatInvitationSound, forKey: PrefsKeys.playChatInvitationSound.rawValue) }
  }
  
  var refusePrivateChat: Bool {
    didSet { UserDefaults.standard.set(self.refusePrivateChat, forKey: PrefsKeys.refusePrivateChat.rawValue) }
  }
  
  var enableAutomaticMessage: Bool {
    didSet { UserDefaults.standard.set(self.enableAutomaticMessage, forKey: PrefsKeys.enableAutomaticMessage.rawValue) }
  }
  
  var automaticMessage: String {
    didSet { UserDefaults.standard.set(self.automaticMessage, forKey: PrefsKeys.automaticMessage.rawValue) }
  }
}

//@Observable
//final class Preferences {
//  
//  var username: String {
//    get {
//      access(keyPath: \.username)
//      return UserDefaults.standard.object(forKey: "username") as? String ?? "guest"
//    }
//    set {
//      withMutation(keyPath: \.username) {
//        UserDefaults.standard.set(newValue, forKey: "username")
//      }
//    }
//  }
//  
//  var refusePrivateMessages: Bool {
//    get { return UserDefaults.standard.object(forKey: "refuse private messages") as? Bool ?? false }
//    set { UserDefaults.standard.set(newValue, forKey: "refuse private messages") }
//  }
//  
//  var refusePrivateChat: Bool {
//    get { return UserDefaults.standard.object(forKey: "refuse private chat") as? Bool ?? false }
//    set { UserDefaults.standard.set(newValue, forKey: "refuse private chat") }
//  }
//  
//  var automaticResponseEnabled: Bool {
//    get { return UserDefaults.standard.object(forKey: "enable automatic response") as? Bool ?? false }
//    set { UserDefaults.standard.set(newValue, forKey: "enable automatic response") }
//  }
//  
//  var automaticResponse: String {
//    get { return UserDefaults.standard.object(forKey: "automatic response") as? String ?? "" }
//    set { UserDefaults.standard.set(newValue, forKey: "automatic response") }
//  }
//  
//  var userIconID: Int {
//    get { return UserDefaults.standard.object(forKey: "user icon") as? Int ?? 404 }
//    set { UserDefaults.standard.set(newValue, forKey: "user icon") }
//  }
//  
////  @AppStorage("username") public var username: String = "guest"
////  @AppStorage("refuse private messages") public var refusePrivateMessages: Bool = false
////  @AppStorage("refuse private chat") public var refusePrivateChat: Bool = false
////  @AppStorage("enable automatic response") public var enableAutomaticResponse: Bool = false
////  @AppStorage("automatic response") public var automaticResponse: String = ""
////  
////  // Icon
////  @AppStorage("user icon id") public var iconID: Int = 404
//  
//  public static let shared = Preferences()
//}