Create shared preferences API (#4654)
This commit is contained in:
parent
2397104ad6
commit
83e8522e0a
19 changed files with 722 additions and 81 deletions
|
@ -1,23 +0,0 @@
|
|||
import ExpoModulesCore
|
||||
|
||||
public class ExpoBlueskyDevicePrefsModule: Module {
|
||||
func getDefaults(_ useAppGroup: Bool) -> UserDefaults? {
|
||||
if useAppGroup {
|
||||
return UserDefaults(suiteName: "group.app.bsky")
|
||||
} else {
|
||||
return UserDefaults.standard
|
||||
}
|
||||
}
|
||||
|
||||
public func definition() -> ModuleDefinition {
|
||||
Name("ExpoBlueskyDevicePrefs")
|
||||
|
||||
AsyncFunction("getStringValueAsync") { (key: String, useAppGroup: Bool) in
|
||||
return self.getDefaults(useAppGroup)?.string(forKey: key)
|
||||
}
|
||||
|
||||
AsyncFunction("setStringValueAsync") { (key: String, value: String?, useAppGroup: Bool) in
|
||||
self.getDefaults(useAppGroup)?.setValue(value, forKey: key)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
import Foundation
|
||||
import ExpoModulesCore
|
||||
|
||||
public class ExpoBlueskySharedPrefsModule: Module {
|
||||
let defaults = UserDefaults(suiteName: "group.app.bsky")
|
||||
|
||||
func getDefaults(_ info: String = "(no info)") -> UserDefaults? {
|
||||
guard let defaults = self.defaults else {
|
||||
NSLog("Failed to get defaults for app group: \(info)")
|
||||
return nil
|
||||
}
|
||||
return defaults
|
||||
}
|
||||
|
||||
public func definition() -> ModuleDefinition {
|
||||
Name("ExpoBlueskySharedPrefs")
|
||||
|
||||
// JavaScripValue causes a crash when trying to check `isString()`. Let's
|
||||
// explicitly define setString instead.
|
||||
Function("setString") { (key: String, value: String?) in
|
||||
SharedPrefs.shared.setValue(key, value)
|
||||
}
|
||||
|
||||
Function("setValue") { (key: String, value: JavaScriptValue) in
|
||||
if value.isNumber() {
|
||||
SharedPrefs.shared.setValue(key, value.getDouble())
|
||||
} else if value.isBool() {
|
||||
SharedPrefs.shared.setValue(key, value.getBool())
|
||||
} else if value.isNull() || value.isUndefined() {
|
||||
SharedPrefs.shared.removeValue(key)
|
||||
}
|
||||
}
|
||||
|
||||
Function("removeValue") { (key: String) in
|
||||
SharedPrefs.shared.removeValue(key)
|
||||
}
|
||||
|
||||
Function("getString") { (key: String) in
|
||||
return SharedPrefs.shared.getString(key)
|
||||
}
|
||||
|
||||
Function("getBool") { (key: String) in
|
||||
return SharedPrefs.shared.getBool(key)
|
||||
}
|
||||
|
||||
Function("getNumber") { (key: String) in
|
||||
return SharedPrefs.shared.getNumber(key)
|
||||
}
|
||||
|
||||
Function("addToSet") { (key: String, value: String) in
|
||||
SharedPrefs.shared.addToSet(key, value)
|
||||
}
|
||||
|
||||
Function("removeFromSet") { (key: String, value: String) in
|
||||
SharedPrefs.shared.removeFromSet(key, value)
|
||||
}
|
||||
|
||||
Function("setContains") { (key: String, value: String) in
|
||||
return SharedPrefs.shared.setContains(key, value)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
import Foundation
|
||||
|
||||
public class SharedPrefs {
|
||||
public static let shared = SharedPrefs()
|
||||
|
||||
private let defaults = UserDefaults(suiteName: "group.app.bsky")
|
||||
|
||||
init() {
|
||||
if defaults == nil {
|
||||
NSLog("Failed to get user defaults for app group.")
|
||||
}
|
||||
}
|
||||
|
||||
private func getDefaults(_ info: String = "(no info)") -> UserDefaults? {
|
||||
guard let defaults = self.defaults else {
|
||||
NSLog("Failed to get defaults for app group: \(info)")
|
||||
return nil
|
||||
}
|
||||
return defaults
|
||||
}
|
||||
|
||||
public func setValue(_ key: String, _ value: String?) {
|
||||
getDefaults(key)?.setValue(value, forKey: key)
|
||||
}
|
||||
|
||||
public func setValue(_ key: String, _ value: Double?) {
|
||||
getDefaults(key)?.setValue(value, forKey: key)
|
||||
}
|
||||
|
||||
public func setValue(_ key: String, _ value: Bool?) {
|
||||
getDefaults(key)?.setValue(value, forKey: key)
|
||||
}
|
||||
|
||||
public func _setAnyValue(_ key: String, _ value: Any?) {
|
||||
getDefaults(key)?.setValue(value, forKey: key)
|
||||
}
|
||||
|
||||
public func removeValue(_ key: String) {
|
||||
getDefaults(key)?.removeObject(forKey: key)
|
||||
}
|
||||
|
||||
public func getString(_ key: String) -> String? {
|
||||
return getDefaults(key)?.string(forKey: key)
|
||||
}
|
||||
|
||||
public func getNumber(_ key: String) -> Double? {
|
||||
return getDefaults(key)?.double(forKey: key)
|
||||
}
|
||||
|
||||
public func getBool(_ key: String) -> Bool? {
|
||||
return getDefaults(key)?.bool(forKey: key)
|
||||
}
|
||||
|
||||
public func addToSet(_ key: String, _ value: String) {
|
||||
var dict: [String: Bool]?
|
||||
if var currDict = getDefaults(key)?.dictionary(forKey: key) as? [String: Bool] {
|
||||
currDict[value] = true
|
||||
dict = currDict
|
||||
} else {
|
||||
dict = [
|
||||
value: true
|
||||
]
|
||||
}
|
||||
getDefaults(key)?.setValue(dict, forKey: key)
|
||||
}
|
||||
|
||||
public func removeFromSet(_ key: String, _ value: String) {
|
||||
guard var dict = getDefaults(key)?.dictionary(forKey: key) as? [String: Bool] else {
|
||||
return
|
||||
}
|
||||
dict.removeValue(forKey: value)
|
||||
getDefaults(key)?.setValue(dict, forKey: key)
|
||||
}
|
||||
|
||||
public func setContains(_ key: String, _ value: String) -> Bool {
|
||||
guard let dict = getDefaults(key)?.dictionary(forKey: key) as? [String: Bool] else {
|
||||
return false
|
||||
}
|
||||
return dict[value] == true
|
||||
}
|
||||
|
||||
public func hasValue(_ key: String) -> Bool {
|
||||
return getDefaults(key)?.value(forKey: key) != nil
|
||||
}
|
||||
|
||||
public func getValues(_ keys: [String]) -> [String: Any?]? {
|
||||
return getDefaults("keys:\(keys)")?.dictionaryWithValues(forKeys: keys)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue