mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import ExpoModulesCore
2
3let APP_GROUP = "group.app.bsky"
4
5let DEFAULTS: [String: Any] = [
6 "playSoundChat": true,
7 "playSoundFollow": false,
8 "playSoundLike": false,
9 "playSoundMention": false,
10 "playSoundQuote": false,
11 "playSoundReply": false,
12 "playSoundRepost": false,
13 "mutedThreads": [:] as! [String: [String]],
14 "badgeCount": 0
15]
16
17/*
18 * The purpose of this module is to store values that are needed by the notification service
19 * extension. Since we would rather get and store values such as age or user mute state
20 * while the app is foregrounded, we should use this module liberally. We should aim to keep
21 * background fetches to a minimum (two or three times per hour) while the app is backgrounded
22 * or killed
23 */
24public class ExpoBackgroundNotificationHandlerModule: Module {
25 let userDefaults = UserDefaults(suiteName: APP_GROUP)
26
27 public func definition() -> ModuleDefinition {
28 Name("ExpoBackgroundNotificationHandler")
29
30 OnCreate {
31 DEFAULTS.forEach { p in
32 if userDefaults?.value(forKey: p.key) == nil {
33 userDefaults?.setValue(p.value, forKey: p.key)
34 }
35 }
36 }
37
38 AsyncFunction("getAllPrefsAsync") { () -> [String: Any]? in
39 var keys: [String] = []
40 DEFAULTS.forEach { p in
41 keys.append(p.key)
42 }
43 return userDefaults?.dictionaryWithValues(forKeys: keys)
44 }
45
46 AsyncFunction("getBoolAsync") { (forKey: String) -> Bool in
47 if let pref = userDefaults?.bool(forKey: forKey) {
48 return pref
49 }
50 return false
51 }
52
53 AsyncFunction("getStringAsync") { (forKey: String) -> String? in
54 if let pref = userDefaults?.string(forKey: forKey) {
55 return pref
56 }
57 return nil
58 }
59
60 AsyncFunction("getStringArrayAsync") { (forKey: String) -> [String]? in
61 if let pref = userDefaults?.stringArray(forKey: forKey) {
62 return pref
63 }
64 return nil
65 }
66
67 AsyncFunction("setBoolAsync") { (forKey: String, value: Bool) in
68 userDefaults?.setValue(value, forKey: forKey)
69 }
70
71 AsyncFunction("setStringAsync") { (forKey: String, value: String) in
72 userDefaults?.setValue(value, forKey: forKey)
73 }
74
75 AsyncFunction("setStringArrayAsync") { (forKey: String, value: [String]) in
76 userDefaults?.setValue(value, forKey: forKey)
77 }
78
79 AsyncFunction("addToStringArrayAsync") { (forKey: String, string: String) in
80 if var curr = userDefaults?.stringArray(forKey: forKey),
81 !curr.contains(string) {
82 curr.append(string)
83 userDefaults?.setValue(curr, forKey: forKey)
84 }
85 }
86
87 AsyncFunction("removeFromStringArrayAsync") { (forKey: String, string: String) in
88 if var curr = userDefaults?.stringArray(forKey: forKey) {
89 curr.removeAll { s in
90 return s == string
91 }
92 userDefaults?.setValue(curr, forKey: forKey)
93 }
94 }
95
96 AsyncFunction("addManyToStringArrayAsync") { (forKey: String, strings: [String]) in
97 if var curr = userDefaults?.stringArray(forKey: forKey) {
98 strings.forEach { s in
99 if !curr.contains(s) {
100 curr.append(s)
101 }
102 }
103 userDefaults?.setValue(curr, forKey: forKey)
104 }
105 }
106
107 AsyncFunction("removeManyFromStringArrayAsync") { (forKey: String, strings: [String]) in
108 if var curr = userDefaults?.stringArray(forKey: forKey) {
109 strings.forEach { s in
110 curr.removeAll(where: { $0 == s })
111 }
112 userDefaults?.setValue(curr, forKey: forKey)
113 }
114 }
115
116 AsyncFunction("setBadgeCountAsync") { (count: Int) in
117 userDefaults?.setValue(count, forKey: "badgeCount")
118 }
119 }
120}