implement js changes

This commit is contained in:
Hailey
2024-06-26 11:31:27 -07:00
parent f9c5c42290
commit 035fad8bc6
6 changed files with 31 additions and 49 deletions
@@ -41,9 +41,9 @@ public class ExpoBackgroundNotificationHandlerModule: Module {
SharedPrefs.shared.setValue(BadgeType.generic.toKeyName(), 0) SharedPrefs.shared.setValue(BadgeType.generic.toKeyName(), 0)
} }
AsyncFunction("incrementMessagesCountAsync") { (convoId: String) in AsyncFunction("maybeIncrementMessagesCountAsync") { (convoId: String) in
guard !SharedPrefs.shared.setContains(INCREMENTED_FOR_KEY, convoId) else { guard !SharedPrefs.shared.setContains(INCREMENTED_FOR_KEY, convoId) else {
return return false
} }
var count = SharedPrefs.shared.getNumber(BadgeType.messages.toKeyName()) ?? 0 var count = SharedPrefs.shared.getNumber(BadgeType.messages.toKeyName()) ?? 0
@@ -51,11 +51,12 @@ public class ExpoBackgroundNotificationHandlerModule: Module {
SharedPrefs.shared.addToSet(INCREMENTED_FOR_KEY, convoId) SharedPrefs.shared.addToSet(INCREMENTED_FOR_KEY, convoId)
SharedPrefs.shared.setValue(BadgeType.messages.toKeyName(), count) SharedPrefs.shared.setValue(BadgeType.messages.toKeyName(), count)
return true
} }
AsyncFunction("decrementMessagesCountAsync") { (convoId: String) in AsyncFunction("maybeDecrementMessagesCountAsync") { (convoId: String) in
guard SharedPrefs.shared.setContains(INCREMENTED_FOR_KEY, convoId) else { guard SharedPrefs.shared.setContains(INCREMENTED_FOR_KEY, convoId) else {
return return false
} }
var count = SharedPrefs.shared.getNumber(BadgeType.messages.toKeyName()) ?? 0 var count = SharedPrefs.shared.getNumber(BadgeType.messages.toKeyName()) ?? 0
@@ -63,6 +64,7 @@ public class ExpoBackgroundNotificationHandlerModule: Module {
SharedPrefs.shared.removeFromSet(INCREMENTED_FOR_KEY, convoId) SharedPrefs.shared.removeFromSet(INCREMENTED_FOR_KEY, convoId)
SharedPrefs.shared.setValue(BadgeType.messages.toKeyName(), count) SharedPrefs.shared.setValue(BadgeType.messages.toKeyName(), count)
return true
} }
AsyncFunction("getPrefsAsync") { AsyncFunction("getPrefsAsync") {
@@ -4,22 +4,20 @@ import {BackgroundNotificationHandlerPreferences} from './ExpoBackgroundNotifica
const NativeModule = requireNativeModule('ExpoBackgroundNotificationHandler') const NativeModule = requireNativeModule('ExpoBackgroundNotificationHandler')
export function resetGenericCountAsync(count: number): Promise<void> { export function resetGenericCountAsync(): Promise<void> {
NativeModule.resetGenericCountAsync(count) NativeModule.resetGenericCountAsync()
} }
export function incrementMessagesCountAsync( export function maybeIncrementMessagesCountAsync(
count: number,
convoId: string, convoId: string,
): Promise<void> { ): Promise<boolean> {
NativeModule.incrementMessagesCountAsync(count, convoId) NativeModule.incrementMessagesCountAsync(convoId)
} }
export function decrementMessagesCountAsync( export function maybeDecrementMessagesCountAsync(
count: number,
convoId: string, convoId: string,
): Promise<void> { ): Promise<boolean> {
NativeModule.decrementMessagesCountAsync(count, convoId) NativeModule.decrementMessagesCountAsync(convoId)
} }
export function getPrefsAsync(): Promise<BackgroundNotificationHandlerPreferences> { export function getPrefsAsync(): Promise<BackgroundNotificationHandlerPreferences> {
@@ -1,22 +1,20 @@
import {NotImplementedError} from './NotImplemented' import {NotImplementedError} from './NotImplemented'
import {BackgroundNotificationHandlerPreferences} from './types' import {BackgroundNotificationHandlerPreferences} from './types'
export function resetGenericCountAsync(count: number): Promise<void> { export function resetGenericCountAsync(): Promise<void> {
throw new NotImplementedError({count}) throw new NotImplementedError()
} }
export function incrementMessagesCountAsync( export function maybeIncrementMessagesCountAsync(
count: number,
convoId: string, convoId: string,
): Promise<void> { ): Promise<boolean> {
throw new NotImplementedError({count, convoId}) throw new NotImplementedError({convoId})
} }
export function decrementMessagesCountAsync( export function maybeDecrementMessagesCountAsync(
count: number,
convoId: string, convoId: string,
): Promise<void> { ): Promise<boolean> {
throw new NotImplementedError({count, convoId}) throw new NotImplementedError({convoId})
} }
export function getPrefsAsync(): Promise<BackgroundNotificationHandlerPreferences | null> { export function getPrefsAsync(): Promise<BackgroundNotificationHandlerPreferences | null> {
+4 -20
View File
@@ -1,13 +1,13 @@
import React from 'react' import React from 'react'
import * as Notifications from 'expo-notifications' import * as Notifications from 'expo-notifications'
import {getBadgeCountAsync, setBadgeCountAsync} from 'expo-notifications' import {setBadgeCountAsync} from 'expo-notifications'
import {BskyAgent} from '@atproto/api' import {BskyAgent} from '@atproto/api'
import {logger} from '#/logger' import {logger} from '#/logger'
import {SessionAccount, useAgent, useSession} from '#/state/session' import {SessionAccount, useAgent, useSession} from '#/state/session'
import {logEvent, useGate} from 'lib/statsig/statsig' import {logEvent, useGate} from 'lib/statsig/statsig'
import {devicePlatform, isAndroid, isNative} from 'platform/detection' import {devicePlatform, isAndroid, isNative} from 'platform/detection'
import BackgroundNotificationHandler from '../../../modules/expo-background-notification-handler' import {BackgroundNotifications} from '../../../modules/expo-background-notification-handler'
const SERVICE_DID = (serviceUrl?: string) => const SERVICE_DID = (serviceUrl?: string) =>
serviceUrl?.includes('staging') serviceUrl?.includes('staging')
@@ -144,23 +144,7 @@ export function useRequestNotificationsPermission() {
} }
} }
export async function decrementBadgeCount( export async function resetGenericBadgeCount() {
type: 'generic' | 'messages', await BackgroundNotifications.resetGenericCountAsync()
by: number,
) {
if (!isNative) return
let count = await getBadgeCountAsync()
count -= by
if (count < 0) {
count = 0
}
await BackgroundNotificationHandler.setBadgeCountAsync(type, count)
await setBadgeCountAsync(count)
}
export async function resetBadgeCount(type: 'generic' | 'messages') {
await BackgroundNotificationHandler.setBadgeCountAsync(type, 0)
await setBadgeCountAsync(0) await setBadgeCountAsync(0)
} }
+3 -3
View File
@@ -20,7 +20,6 @@ import {useProfileShadow} from '#/state/cache/profile-shadow'
import {useModerationOpts} from '#/state/preferences/moderation-opts' import {useModerationOpts} from '#/state/preferences/moderation-opts'
import {useSession} from '#/state/session' import {useSession} from '#/state/session'
import {useHaptics} from 'lib/haptics' import {useHaptics} from 'lib/haptics'
import {decrementBadgeCount} from 'lib/notifications/notifications'
import {logEvent} from 'lib/statsig/statsig' import {logEvent} from 'lib/statsig/statsig'
import {sanitizeDisplayName} from 'lib/strings/display-names' import {sanitizeDisplayName} from 'lib/strings/display-names'
import {TimeElapsed} from '#/view/com/util/TimeElapsed' import {TimeElapsed} from '#/view/com/util/TimeElapsed'
@@ -32,6 +31,7 @@ import {Link} from '#/components/Link'
import {useMenuControl} from '#/components/Menu' import {useMenuControl} from '#/components/Menu'
import {PostAlerts} from '#/components/moderation/PostAlerts' import {PostAlerts} from '#/components/moderation/PostAlerts'
import {Text} from '#/components/Typography' import {Text} from '#/components/Typography'
import {BackgroundNotifications} from '../../../../modules/expo-background-notification-handler'
export let ChatListItem = ({ export let ChatListItem = ({
convo, convo,
@@ -178,7 +178,7 @@ function ChatListItemReady({
const onPress = useCallback( const onPress = useCallback(
(e: GestureResponderEvent) => { (e: GestureResponderEvent) => {
decrementBadgeCount(convo.unreadCount) BackgroundNotifications.maybeDecrementMessagesCountAsync(convo.id)
if (isDeletedAccount) { if (isDeletedAccount) {
e.preventDefault() e.preventDefault()
menuControl.open() menuControl.open()
@@ -187,7 +187,7 @@ function ChatListItemReady({
logEvent('chat:open', {logContext: 'ChatsList'}) logEvent('chat:open', {logContext: 'ChatsList'})
} }
}, },
[convo.unreadCount, isDeletedAccount, menuControl], [convo.id, isDeletedAccount, menuControl],
) )
const onLongPress = useCallback(() => { const onLongPress = useCallback(() => {
+2 -2
View File
@@ -10,7 +10,7 @@ import EventEmitter from 'eventemitter3'
import BroadcastChannel from '#/lib/broadcast' import BroadcastChannel from '#/lib/broadcast'
import {logger} from '#/logger' import {logger} from '#/logger'
import {useAgent, useSession} from '#/state/session' import {useAgent, useSession} from '#/state/session'
import {resetBadgeCount} from 'lib/notifications/notifications' import {resetGenericBadgeCount} from 'lib/notifications/notifications'
import {useModerationOpts} from '../../preferences/moderation-opts' import {useModerationOpts} from '../../preferences/moderation-opts'
import {truncateAndInvalidate} from '../util' import {truncateAndInvalidate} from '../util'
import {RQKEY as RQKEY_NOTIFS} from './feed' import {RQKEY as RQKEY_NOTIFS} from './feed'
@@ -117,7 +117,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
// update & broadcast // update & broadcast
setNumUnread('') setNumUnread('')
broadcast.postMessage({event: ''}) broadcast.postMessage({event: ''})
resetBadgeCount('generic') resetGenericBadgeCount()
}, },
async checkUnread({ async checkUnread({