bsky-app/modules/expo-background-notificatio.../ios/ExpoBackgroundNotificationH...

121 lines
3.6 KiB
Swift
Raw Normal View History

import ExpoModulesCore
let APP_GROUP = "group.app.bsky"
2024-07-12 03:15:35 +02:00
let DEFAULTS: [String: Any] = [
"playSoundChat": true,
"playSoundFollow": false,
"playSoundLike": false,
"playSoundMention": false,
"playSoundQuote": false,
"playSoundReply": false,
"playSoundRepost": false,
2024-07-12 03:15:35 +02:00
"mutedThreads": [:] as! [String: [String]],
"badgeCount": 0
]
/*
* The purpose of this module is to store values that are needed by the notification service
* extension. Since we would rather get and store values such as age or user mute state
* while the app is foregrounded, we should use this module liberally. We should aim to keep
* background fetches to a minimum (two or three times per hour) while the app is backgrounded
* or killed
*/
public class ExpoBackgroundNotificationHandlerModule: Module {
let userDefaults = UserDefaults(suiteName: APP_GROUP)
2024-07-12 03:15:35 +02:00
public func definition() -> ModuleDefinition {
Name("ExpoBackgroundNotificationHandler")
2024-07-12 03:15:35 +02:00
OnCreate {
DEFAULTS.forEach { p in
if userDefaults?.value(forKey: p.key) == nil {
userDefaults?.setValue(p.value, forKey: p.key)
}
}
}
2024-07-12 03:15:35 +02:00
AsyncFunction("getAllPrefsAsync") { () -> [String: Any]? in
var keys: [String] = []
DEFAULTS.forEach { p in
keys.append(p.key)
}
return userDefaults?.dictionaryWithValues(forKeys: keys)
}
2024-07-12 03:15:35 +02:00
AsyncFunction("getBoolAsync") { (forKey: String) -> Bool in
if let pref = userDefaults?.bool(forKey: forKey) {
return pref
}
return false
}
2024-07-12 03:15:35 +02:00
AsyncFunction("getStringAsync") { (forKey: String) -> String? in
if let pref = userDefaults?.string(forKey: forKey) {
return pref
}
return nil
}
2024-07-12 03:15:35 +02:00
AsyncFunction("getStringArrayAsync") { (forKey: String) -> [String]? in
if let pref = userDefaults?.stringArray(forKey: forKey) {
return pref
}
return nil
}
2024-07-12 03:15:35 +02:00
AsyncFunction("setBoolAsync") { (forKey: String, value: Bool) in
userDefaults?.setValue(value, forKey: forKey)
}
2024-07-12 03:15:35 +02:00
AsyncFunction("setStringAsync") { (forKey: String, value: String) in
userDefaults?.setValue(value, forKey: forKey)
}
2024-07-12 03:15:35 +02:00
AsyncFunction("setStringArrayAsync") { (forKey: String, value: [String]) in
userDefaults?.setValue(value, forKey: forKey)
}
2024-07-12 03:15:35 +02:00
AsyncFunction("addToStringArrayAsync") { (forKey: String, string: String) in
if var curr = userDefaults?.stringArray(forKey: forKey),
2024-07-12 03:15:35 +02:00
!curr.contains(string) {
curr.append(string)
userDefaults?.setValue(curr, forKey: forKey)
}
}
2024-07-12 03:15:35 +02:00
AsyncFunction("removeFromStringArrayAsync") { (forKey: String, string: String) in
if var curr = userDefaults?.stringArray(forKey: forKey) {
curr.removeAll { s in
return s == string
}
userDefaults?.setValue(curr, forKey: forKey)
}
}
2024-07-12 03:15:35 +02:00
AsyncFunction("addManyToStringArrayAsync") { (forKey: String, strings: [String]) in
if var curr = userDefaults?.stringArray(forKey: forKey) {
strings.forEach { s in
if !curr.contains(s) {
curr.append(s)
}
}
userDefaults?.setValue(curr, forKey: forKey)
}
}
2024-07-12 03:15:35 +02:00
AsyncFunction("removeManyFromStringArrayAsync") { (forKey: String, strings: [String]) in
if var curr = userDefaults?.stringArray(forKey: forKey) {
strings.forEach { s in
curr.removeAll(where: { $0 == s })
}
userDefaults?.setValue(curr, forKey: forKey)
}
}
2024-07-12 03:15:35 +02:00
AsyncFunction("setBadgeCountAsync") { (count: Int) in
userDefaults?.setValue(count, forKey: "badgeCount")
}
}
}