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]
15
16/*
17 * The purpose of this module is to store values that are needed by the notification service
18 * extension. Since we would rather get and store values such as age or user mute state
19 * while the app is foregrounded, we should use this module liberally. We should aim to keep
20 * background fetches to a minimum (two or three times per hour) while the app is backgrounded
21 * or killed
22 */
23public class ExpoBackgroundNotificationHandlerModule: Module {
24 let userDefaults = UserDefaults(suiteName: APP_GROUP)
25
26 public func definition() -> ModuleDefinition {
27 Name("ExpoBackgroundNotificationHandler")
28
29 OnCreate {
30 DEFAULTS.forEach { p in
31 if userDefaults?.value(forKey: p.key) == nil {
32 userDefaults?.setValue(p.value, forKey: p.key)
33 }
34 }
35 }
36
37 AsyncFunction("getAllPrefsAsync") { () -> [String:Any]? in
38 var keys: [String] = []
39 DEFAULTS.forEach { p in
40 keys.append(p.key)
41 }
42 return userDefaults?.dictionaryWithValues(forKeys: keys)
43 }
44
45 AsyncFunction("getBoolAsync") { (forKey: String) -> Bool in
46 if let pref = userDefaults?.bool(forKey: forKey) {
47 return pref
48 }
49 return false
50 }
51
52 AsyncFunction("getStringAsync") { (forKey: String) -> String? in
53 if let pref = userDefaults?.string(forKey: forKey) {
54 return pref
55 }
56 return nil
57 }
58
59 AsyncFunction("getStringArrayAsync") { (forKey: String) -> [String]? in
60 if let pref = userDefaults?.stringArray(forKey: forKey) {
61 return pref
62 }
63 return nil
64 }
65
66 AsyncFunction("setBoolAsync") { (forKey: String, value: Bool) -> Void in
67 userDefaults?.setValue(value, forKey: forKey)
68 }
69
70 AsyncFunction("setStringAsync") { (forKey: String, value: String) -> Void in
71 userDefaults?.setValue(value, forKey: forKey)
72 }
73
74 AsyncFunction("setStringArrayAsync") { (forKey: String, value: [String]) -> Void in
75 userDefaults?.setValue(value, forKey: forKey)
76 }
77
78 AsyncFunction("addToStringArrayAsync") { (forKey: String, string: String) in
79 if var curr = userDefaults?.stringArray(forKey: forKey),
80 !curr.contains(string)
81 {
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}