diff options
author | Hailey <me@haileyok.com> | 2024-07-11 18:37:43 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-11 18:37:43 -0700 |
commit | 83e8522e0a89be28b1733f4c50dbd4379d98d03b (patch) | |
tree | c51a1054ffa8f1b226412a77fa7d69f5c891f7ae /modules/expo-bluesky-swiss-army/src/SharedPrefs/index.native.ts | |
parent | 2397104ad6169ced02b1acd9fbbbe426f4cc4da0 (diff) | |
download | voidsky-83e8522e0a89be28b1733f4c50dbd4379d98d03b.tar.zst |
Create shared preferences API (#4654)
Diffstat (limited to 'modules/expo-bluesky-swiss-army/src/SharedPrefs/index.native.ts')
-rw-r--r-- | modules/expo-bluesky-swiss-army/src/SharedPrefs/index.native.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/modules/expo-bluesky-swiss-army/src/SharedPrefs/index.native.ts b/modules/expo-bluesky-swiss-army/src/SharedPrefs/index.native.ts new file mode 100644 index 000000000..0cea7c53b --- /dev/null +++ b/modules/expo-bluesky-swiss-army/src/SharedPrefs/index.native.ts @@ -0,0 +1,51 @@ +import {requireNativeModule} from 'expo-modules-core' + +const NativeModule = requireNativeModule('ExpoBlueskySharedPrefs') + +export function setValue( + key: string, + value: string | number | boolean | null | undefined, +): void { + // A bug on Android causes `JavaScripValue.isString()` to cause a crash on some occasions, seemingly because of a + // memory violation. Instead, we will use a specific function to set strings on this platform. + if (typeof value === 'string') { + return NativeModule.setString(key, value) + } + return NativeModule.setValue(key, value) +} + +export function removeValue(key: string): void { + return NativeModule.removeValue(key) +} + +export function getString(key: string): string | undefined { + return nullToUndefined(NativeModule.getString(key)) +} + +export function getNumber(key: string): number | undefined { + return nullToUndefined(NativeModule.getNumber(key)) +} + +export function getBool(key: string): boolean | undefined { + return nullToUndefined(NativeModule.getBool(key)) +} + +export function addToSet(key: string, value: string): void { + return NativeModule.addToSet(key, value) +} + +export function removeFromSet(key: string, value: string): void { + return NativeModule.removeFromSet(key, value) +} + +export function setContains(key: string, value: string): boolean { + return NativeModule.setContains(key, value) +} + +// iOS returns `null` if a value does not exist, and Android returns `undefined. Normalize these here for JS types +function nullToUndefined(value: any) { + if (value == null) { + return undefined + } + return value +} |