diff --git a/modules/expo-background-notification-handler/ios/ExpoBackgroundNotificationHandlerModule.swift b/modules/expo-background-notification-handler/ios/ExpoBackgroundNotificationHandlerModule.swift index a19bc05a3e..2131ada491 100644 --- a/modules/expo-background-notification-handler/ios/ExpoBackgroundNotificationHandlerModule.swift +++ b/modules/expo-background-notification-handler/ios/ExpoBackgroundNotificationHandlerModule.swift @@ -41,9 +41,9 @@ public class ExpoBackgroundNotificationHandlerModule: Module { 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 { - return + return false } 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.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 { - return + return false } 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.setValue(BadgeType.messages.toKeyName(), count) + return true } AsyncFunction("getPrefsAsync") { diff --git a/modules/expo-background-notification-handler/src/index.native.ts b/modules/expo-background-notification-handler/src/index.native.ts index 402603818d..f93b9462ce 100644 --- a/modules/expo-background-notification-handler/src/index.native.ts +++ b/modules/expo-background-notification-handler/src/index.native.ts @@ -4,22 +4,20 @@ import {BackgroundNotificationHandlerPreferences} from './ExpoBackgroundNotifica const NativeModule = requireNativeModule('ExpoBackgroundNotificationHandler') -export function resetGenericCountAsync(count: number): Promise { - NativeModule.resetGenericCountAsync(count) +export function resetGenericCountAsync(): Promise { + NativeModule.resetGenericCountAsync() } -export function incrementMessagesCountAsync( - count: number, +export function maybeIncrementMessagesCountAsync( convoId: string, -): Promise { - NativeModule.incrementMessagesCountAsync(count, convoId) +): Promise { + NativeModule.incrementMessagesCountAsync(convoId) } -export function decrementMessagesCountAsync( - count: number, +export function maybeDecrementMessagesCountAsync( convoId: string, -): Promise { - NativeModule.decrementMessagesCountAsync(count, convoId) +): Promise { + NativeModule.decrementMessagesCountAsync(convoId) } export function getPrefsAsync(): Promise { diff --git a/modules/expo-background-notification-handler/src/index.ts b/modules/expo-background-notification-handler/src/index.ts index 918dc95f34..97d52b89a4 100644 --- a/modules/expo-background-notification-handler/src/index.ts +++ b/modules/expo-background-notification-handler/src/index.ts @@ -1,22 +1,20 @@ import {NotImplementedError} from './NotImplemented' import {BackgroundNotificationHandlerPreferences} from './types' -export function resetGenericCountAsync(count: number): Promise { - throw new NotImplementedError({count}) +export function resetGenericCountAsync(): Promise { + throw new NotImplementedError() } -export function incrementMessagesCountAsync( - count: number, +export function maybeIncrementMessagesCountAsync( convoId: string, -): Promise { - throw new NotImplementedError({count, convoId}) +): Promise { + throw new NotImplementedError({convoId}) } -export function decrementMessagesCountAsync( - count: number, +export function maybeDecrementMessagesCountAsync( convoId: string, -): Promise { - throw new NotImplementedError({count, convoId}) +): Promise { + throw new NotImplementedError({convoId}) } export function getPrefsAsync(): Promise { diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index e0d447e6a9..ed6ae1ecce 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -1,13 +1,13 @@ import React from 'react' import * as Notifications from 'expo-notifications' -import {getBadgeCountAsync, setBadgeCountAsync} from 'expo-notifications' +import {setBadgeCountAsync} from 'expo-notifications' import {BskyAgent} from '@atproto/api' import {logger} from '#/logger' import {SessionAccount, useAgent, useSession} from '#/state/session' import {logEvent, useGate} from 'lib/statsig/statsig' 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) => serviceUrl?.includes('staging') @@ -144,23 +144,7 @@ export function useRequestNotificationsPermission() { } } -export async function decrementBadgeCount( - type: 'generic' | 'messages', - 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) +export async function resetGenericBadgeCount() { + await BackgroundNotifications.resetGenericCountAsync() await setBadgeCountAsync(0) } diff --git a/src/screens/Messages/List/ChatListItem.tsx b/src/screens/Messages/List/ChatListItem.tsx index 8ebf8b00b5..0065a8ad8f 100644 --- a/src/screens/Messages/List/ChatListItem.tsx +++ b/src/screens/Messages/List/ChatListItem.tsx @@ -20,7 +20,6 @@ import {useProfileShadow} from '#/state/cache/profile-shadow' import {useModerationOpts} from '#/state/preferences/moderation-opts' import {useSession} from '#/state/session' import {useHaptics} from 'lib/haptics' -import {decrementBadgeCount} from 'lib/notifications/notifications' import {logEvent} from 'lib/statsig/statsig' import {sanitizeDisplayName} from 'lib/strings/display-names' import {TimeElapsed} from '#/view/com/util/TimeElapsed' @@ -32,6 +31,7 @@ import {Link} from '#/components/Link' import {useMenuControl} from '#/components/Menu' import {PostAlerts} from '#/components/moderation/PostAlerts' import {Text} from '#/components/Typography' +import {BackgroundNotifications} from '../../../../modules/expo-background-notification-handler' export let ChatListItem = ({ convo, @@ -178,7 +178,7 @@ function ChatListItemReady({ const onPress = useCallback( (e: GestureResponderEvent) => { - decrementBadgeCount(convo.unreadCount) + BackgroundNotifications.maybeDecrementMessagesCountAsync(convo.id) if (isDeletedAccount) { e.preventDefault() menuControl.open() @@ -187,7 +187,7 @@ function ChatListItemReady({ logEvent('chat:open', {logContext: 'ChatsList'}) } }, - [convo.unreadCount, isDeletedAccount, menuControl], + [convo.id, isDeletedAccount, menuControl], ) const onLongPress = useCallback(() => { diff --git a/src/state/queries/notifications/unread.tsx b/src/state/queries/notifications/unread.tsx index f4278ec2aa..aa234d449d 100644 --- a/src/state/queries/notifications/unread.tsx +++ b/src/state/queries/notifications/unread.tsx @@ -10,7 +10,7 @@ import EventEmitter from 'eventemitter3' import BroadcastChannel from '#/lib/broadcast' import {logger} from '#/logger' 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 {truncateAndInvalidate} from '../util' import {RQKEY as RQKEY_NOTIFS} from './feed' @@ -117,7 +117,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { // update & broadcast setNumUnread('') broadcast.postMessage({event: ''}) - resetBadgeCount('generic') + resetGenericBadgeCount() }, async checkUnread({