From d6a2ee4e9e385e298a8749b9d3d8cdf17f891c78 Mon Sep 17 00:00:00 2001 From: Hailey Date: Wed, 26 Jun 2024 11:16:32 -0700 Subject: [PATCH] JS changes --- .../index.ts | 6 ++- ...oBackgroundNotificationHandlerModule.swift | 6 +-- .../BackgroundNotificationHandlerProvider.tsx | 22 +++------- ...ExpoBackgroundNotificationHandler.types.ts | 44 ------------------- ...ExpoBackgroundNotificationHandlerModule.ts | 8 ---- ...BackgroundNotificationHandlerModule.web.ts | 28 ------------ .../src/NotImplemented.ts | 16 +++++++ .../src/index.native.ts | 27 ++++++++++++ .../src/index.ts | 24 ++++++++++ .../src/types.ts | 3 ++ 10 files changed, 82 insertions(+), 102 deletions(-) delete mode 100644 modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandler.types.ts delete mode 100644 modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandlerModule.ts delete mode 100644 modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandlerModule.web.ts create mode 100644 modules/expo-background-notification-handler/src/NotImplemented.ts create mode 100644 modules/expo-background-notification-handler/src/index.native.ts create mode 100644 modules/expo-background-notification-handler/src/index.ts create mode 100644 modules/expo-background-notification-handler/src/types.ts diff --git a/modules/expo-background-notification-handler/index.ts b/modules/expo-background-notification-handler/index.ts index 680c6c13f5..31419756e9 100644 --- a/modules/expo-background-notification-handler/index.ts +++ b/modules/expo-background-notification-handler/index.ts @@ -1,2 +1,4 @@ -import {BackgroundNotificationHandler} from './src/ExpoBackgroundNotificationHandlerModule' -export default BackgroundNotificationHandler +import {BackgroundNotificationPreferencesProvider} from './src/BackgroundNotificationHandlerProvider' +import * as BackgroundNotifications from './src/index' + +export {BackgroundNotificationPreferencesProvider, BackgroundNotifications} diff --git a/modules/expo-background-notification-handler/ios/ExpoBackgroundNotificationHandlerModule.swift b/modules/expo-background-notification-handler/ios/ExpoBackgroundNotificationHandlerModule.swift index 3ba34384a4..66fe6b6783 100644 --- a/modules/expo-background-notification-handler/ios/ExpoBackgroundNotificationHandlerModule.swift +++ b/modules/expo-background-notification-handler/ios/ExpoBackgroundNotificationHandlerModule.swift @@ -37,11 +37,11 @@ public class ExpoBackgroundNotificationHandlerModule: Module { } } - AsyncFunction("resetGenericCount") { + AsyncFunction("resetGenericCountAsync") { SharedPrefs.shared.setValue(BadgeType.generic.toKeyName(), 0) } - AsyncFunction("incrementMessagesCount") { (convoId: String) in + AsyncFunction("incrementMessagesCountAsync") { (convoId: String) in guard !SharedPrefs.shared.setContains(INCREMENTED_FOR_KEY, convoId) else { return } @@ -53,7 +53,7 @@ public class ExpoBackgroundNotificationHandlerModule: Module { SharedPrefs.shared.setValue(BadgeType.messages.toKeyName(), count) } - AsyncFunction("decrementMessagesCount") { (convoId: String) in + AsyncFunction("decrementMessagesCountAsync") { (convoId: String) in guard SharedPrefs.shared.setContains(INCREMENTED_FOR_KEY, convoId) else { return } diff --git a/modules/expo-background-notification-handler/src/BackgroundNotificationHandlerProvider.tsx b/modules/expo-background-notification-handler/src/BackgroundNotificationHandlerProvider.tsx index 6ecdd1d476..4307a61099 100644 --- a/modules/expo-background-notification-handler/src/BackgroundNotificationHandlerProvider.tsx +++ b/modules/expo-background-notification-handler/src/BackgroundNotificationHandlerProvider.tsx @@ -1,7 +1,8 @@ import React from 'react' -import {BackgroundNotificationHandlerPreferences} from './ExpoBackgroundNotificationHandler.types' -import {BackgroundNotificationHandler} from './ExpoBackgroundNotificationHandlerModule' +import {SharedPrefs} from '../../expo-bluesky-swiss-army' +import {BackgroundNotifications} from '../index' +import {BackgroundNotificationHandlerPreferences} from './types' interface BackgroundNotificationPreferencesContext { preferences: BackgroundNotificationHandlerPreferences @@ -29,7 +30,7 @@ export function BackgroundNotificationPreferencesProvider({ React.useEffect(() => { ;(async () => { - const prefs = await BackgroundNotificationHandler.getAllPrefsAsync() + const prefs = await BackgroundNotifications.getPrefsAsync() setPreferences(prefs) })() }, []) @@ -43,20 +44,7 @@ export function BackgroundNotificationPreferencesProvider({ k: Key, v: BackgroundNotificationHandlerPreferences[Key], ) => { - switch (typeof v) { - case 'boolean': { - await BackgroundNotificationHandler.setBoolAsync(k, v) - break - } - case 'string': { - await BackgroundNotificationHandler.setStringAsync(k, v) - break - } - default: { - throw new Error(`Invalid type for value: ${typeof v}`) - } - } - + SharedPrefs.setValueAsync(k, v) setPreferences(prev => ({ ...prev, [k]: v, diff --git a/modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandler.types.ts b/modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandler.types.ts deleted file mode 100644 index 29c6c9a176..0000000000 --- a/modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandler.types.ts +++ /dev/null @@ -1,44 +0,0 @@ -export type ExpoBackgroundNotificationHandlerModule = { - getAllPrefsAsync: () => Promise - getBoolAsync: (forKey: string) => Promise - getStringAsync: (forKey: string) => Promise - getStringArrayAsync: (forKey: string) => Promise - setBoolAsync: ( - forKey: keyof BackgroundNotificationHandlerPreferences, - value: boolean, - ) => Promise - setStringAsync: ( - forKey: keyof BackgroundNotificationHandlerPreferences, - value: string, - ) => Promise - setStringArrayAsync: ( - forKey: keyof BackgroundNotificationHandlerPreferences, - value: string[], - ) => Promise - addToStringArrayAsync: ( - forKey: keyof BackgroundNotificationHandlerPreferences, - value: string, - ) => Promise - removeFromStringArrayAsync: ( - forKey: keyof BackgroundNotificationHandlerPreferences, - value: string, - ) => Promise - addManyToStringArrayAsync: ( - forKey: keyof BackgroundNotificationHandlerPreferences, - value: string[], - ) => Promise - removeManyFromStringArrayAsync: ( - forKey: keyof BackgroundNotificationHandlerPreferences, - value: string[], - ) => Promise - setBadgeCountAsync: ( - type: 'generic' | 'messages', - count: number, - ) => Promise -} - -// TODO there are more preferences in the native code, however they have not been added here yet. -// Don't add them until the native logic also handles the notifications for those preference types. -export type BackgroundNotificationHandlerPreferences = { - playSoundChat: boolean -} diff --git a/modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandlerModule.ts b/modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandlerModule.ts deleted file mode 100644 index d6517893ad..0000000000 --- a/modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandlerModule.ts +++ /dev/null @@ -1,8 +0,0 @@ -import {requireNativeModule} from 'expo-modules-core' - -import {ExpoBackgroundNotificationHandlerModule} from './ExpoBackgroundNotificationHandler.types' - -export const BackgroundNotificationHandler = - requireNativeModule( - 'ExpoBackgroundNotificationHandler', - ) diff --git a/modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandlerModule.web.ts b/modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandlerModule.web.ts deleted file mode 100644 index 7d2f51c856..0000000000 --- a/modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandlerModule.web.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { - BackgroundNotificationHandlerPreferences, - ExpoBackgroundNotificationHandlerModule, -} from './ExpoBackgroundNotificationHandler.types' - -// Stub for web -export const BackgroundNotificationHandler = { - getAllPrefsAsync: async () => { - return {} as BackgroundNotificationHandlerPreferences - }, - getBoolAsync: async (_: string) => { - return false - }, - getStringAsync: async (_: string) => { - return '' - }, - getStringArrayAsync: async (_: string) => { - return [] - }, - setBoolAsync: async (_: string, __: boolean) => {}, - setStringAsync: async (_: string, __: string) => {}, - setStringArrayAsync: async (_: string, __: string[]) => {}, - addToStringArrayAsync: async (_: string, __: string) => {}, - removeFromStringArrayAsync: async (_: string, __: string) => {}, - addManyToStringArrayAsync: async (_: string, __: string[]) => {}, - removeManyFromStringArrayAsync: async (_: string, __: string[]) => {}, - setBadgeCountAsync: async (_: 'generic' | 'messages', __: number) => {}, -} as ExpoBackgroundNotificationHandlerModule diff --git a/modules/expo-background-notification-handler/src/NotImplemented.ts b/modules/expo-background-notification-handler/src/NotImplemented.ts new file mode 100644 index 0000000000..876cd7b328 --- /dev/null +++ b/modules/expo-background-notification-handler/src/NotImplemented.ts @@ -0,0 +1,16 @@ +import {Platform} from 'react-native' + +export class NotImplementedError extends Error { + constructor(params = {}) { + if (__DEV__) { + const caller = new Error().stack?.split('\n')[2] + super( + `Not implemented on ${Platform.OS}. Given params: ${JSON.stringify( + params, + )} ${caller}`, + ) + } else { + super('Not implemented') + } + } +} diff --git a/modules/expo-background-notification-handler/src/index.native.ts b/modules/expo-background-notification-handler/src/index.native.ts new file mode 100644 index 0000000000..402603818d --- /dev/null +++ b/modules/expo-background-notification-handler/src/index.native.ts @@ -0,0 +1,27 @@ +import {requireNativeModule} from 'expo' + +import {BackgroundNotificationHandlerPreferences} from './ExpoBackgroundNotificationHandler.types' + +const NativeModule = requireNativeModule('ExpoBackgroundNotificationHandler') + +export function resetGenericCountAsync(count: number): Promise { + NativeModule.resetGenericCountAsync(count) +} + +export function incrementMessagesCountAsync( + count: number, + convoId: string, +): Promise { + NativeModule.incrementMessagesCountAsync(count, convoId) +} + +export function decrementMessagesCountAsync( + count: number, + convoId: string, +): Promise { + NativeModule.decrementMessagesCountAsync(count, convoId) +} + +export function getPrefsAsync(): Promise { + return NativeModule.getPrefsAsync() +} diff --git a/modules/expo-background-notification-handler/src/index.ts b/modules/expo-background-notification-handler/src/index.ts new file mode 100644 index 0000000000..a859c7ecea --- /dev/null +++ b/modules/expo-background-notification-handler/src/index.ts @@ -0,0 +1,24 @@ +import {NotImplementedError} from './NotImplemented' +import {BackgroundNotificationHandlerPreferences} from './types' + +export function resetGenericCountAsync(count: number): Promise { + throw new NotImplementedError({count}) +} + +export function incrementMessagesCountAsync( + count: number, + convoId: string, +): Promise { + throw new NotImplementedError({count, convoId}) +} + +export function decrementMessagesCountAsync( + count: number, + convoId: string, +): Promise { + throw new NotImplementedError({count, convoId}) +} + +export function getPrefsAsync(): Promise { + throw new NotImplementedError() +} diff --git a/modules/expo-background-notification-handler/src/types.ts b/modules/expo-background-notification-handler/src/types.ts new file mode 100644 index 0000000000..5d6cb6b5dc --- /dev/null +++ b/modules/expo-background-notification-handler/src/types.ts @@ -0,0 +1,3 @@ +export type BackgroundNotificationHandlerPreferences = { + playSoundChat: boolean +}