JS changes

This commit is contained in:
Hailey
2024-06-26 11:16:32 -07:00
parent 83056bbf2b
commit d6a2ee4e9e
10 changed files with 82 additions and 102 deletions
@@ -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}
@@ -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
}
@@ -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,
@@ -1,44 +0,0 @@
export type ExpoBackgroundNotificationHandlerModule = {
getAllPrefsAsync: () => Promise<BackgroundNotificationHandlerPreferences>
getBoolAsync: (forKey: string) => Promise<boolean>
getStringAsync: (forKey: string) => Promise<string>
getStringArrayAsync: (forKey: string) => Promise<string[]>
setBoolAsync: (
forKey: keyof BackgroundNotificationHandlerPreferences,
value: boolean,
) => Promise<void>
setStringAsync: (
forKey: keyof BackgroundNotificationHandlerPreferences,
value: string,
) => Promise<void>
setStringArrayAsync: (
forKey: keyof BackgroundNotificationHandlerPreferences,
value: string[],
) => Promise<void>
addToStringArrayAsync: (
forKey: keyof BackgroundNotificationHandlerPreferences,
value: string,
) => Promise<void>
removeFromStringArrayAsync: (
forKey: keyof BackgroundNotificationHandlerPreferences,
value: string,
) => Promise<void>
addManyToStringArrayAsync: (
forKey: keyof BackgroundNotificationHandlerPreferences,
value: string[],
) => Promise<void>
removeManyFromStringArrayAsync: (
forKey: keyof BackgroundNotificationHandlerPreferences,
value: string[],
) => Promise<void>
setBadgeCountAsync: (
type: 'generic' | 'messages',
count: number,
) => Promise<void>
}
// 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
}
@@ -1,8 +0,0 @@
import {requireNativeModule} from 'expo-modules-core'
import {ExpoBackgroundNotificationHandlerModule} from './ExpoBackgroundNotificationHandler.types'
export const BackgroundNotificationHandler =
requireNativeModule<ExpoBackgroundNotificationHandlerModule>(
'ExpoBackgroundNotificationHandler',
)
@@ -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
@@ -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')
}
}
}
@@ -0,0 +1,27 @@
import {requireNativeModule} from 'expo'
import {BackgroundNotificationHandlerPreferences} from './ExpoBackgroundNotificationHandler.types'
const NativeModule = requireNativeModule('ExpoBackgroundNotificationHandler')
export function resetGenericCountAsync(count: number): Promise<void> {
NativeModule.resetGenericCountAsync(count)
}
export function incrementMessagesCountAsync(
count: number,
convoId: string,
): Promise<void> {
NativeModule.incrementMessagesCountAsync(count, convoId)
}
export function decrementMessagesCountAsync(
count: number,
convoId: string,
): Promise<void> {
NativeModule.decrementMessagesCountAsync(count, convoId)
}
export function getPrefsAsync(): Promise<BackgroundNotificationHandlerPreferences> {
return NativeModule.getPrefsAsync()
}
@@ -0,0 +1,24 @@
import {NotImplementedError} from './NotImplemented'
import {BackgroundNotificationHandlerPreferences} from './types'
export function resetGenericCountAsync(count: number): Promise<void> {
throw new NotImplementedError({count})
}
export function incrementMessagesCountAsync(
count: number,
convoId: string,
): Promise<void> {
throw new NotImplementedError({count, convoId})
}
export function decrementMessagesCountAsync(
count: number,
convoId: string,
): Promise<void> {
throw new NotImplementedError({count, convoId})
}
export function getPrefsAsync(): Promise<BackgroundNotificationHandlerPreferences> {
throw new NotImplementedError()
}
@@ -0,0 +1,3 @@
export type BackgroundNotificationHandlerPreferences = {
playSoundChat: boolean
}