From 5e8650a204cf4b52fa321e672801ce790b3cb554 Mon Sep 17 00:00:00 2001 From: Hailey Date: Thu, 16 May 2024 12:15:35 -0700 Subject: [PATCH] =?UTF-8?q?[=F0=9F=90=B4]=20Decrement=20app=20badge=20when?= =?UTF-8?q?=20opening=20unread=20chat=20(#4040)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * decrement badge count for chats * handle decrement in `useMarkAsRead` * remove async * oops --- src/lib/hooks/useNotificationHandler.ts | 5 +++-- src/lib/notifications/notifications.ts | 12 ++++++++++++ src/state/queries/messages/list-converations.ts | 17 +++++++++++++---- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/lib/hooks/useNotificationHandler.ts b/src/lib/hooks/useNotificationHandler.ts index 6f5fbd66bb..e288ac3ad4 100644 --- a/src/lib/hooks/useNotificationHandler.ts +++ b/src/lib/hooks/useNotificationHandler.ts @@ -169,10 +169,11 @@ export function useNotificationsHandler() { payload.reason === 'chat-message' && payload.recipientDid === currentAccount?.did ) { + const isCurrentConvo = payload.convoId === currentConvoId return { - shouldShowAlert: payload.convoId !== currentConvoId, + shouldShowAlert: !isCurrentConvo, shouldPlaySound: false, - shouldSetBadge: false, + shouldSetBadge: !isCurrentConvo, } } diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index 52f984a599..1182bfcbbe 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -1,5 +1,6 @@ import React from 'react' import * as Notifications from 'expo-notifications' +import {getBadgeCountAsync, setBadgeCountAsync} from 'expo-notifications' import {BskyAgent} from '@atproto/api' import {logger} from '#/logger' @@ -109,3 +110,14 @@ export function useRequestNotificationsPermission() { [gate], ) } + +export async function decrementBadgeCount(by = 1) { + if (!isNative) return + + const currCount = await getBadgeCountAsync() + let newCount = currCount - by + if (newCount < 0) { + newCount = 0 + } + await setBadgeCountAsync(newCount) +} diff --git a/src/state/queries/messages/list-converations.ts b/src/state/queries/messages/list-converations.ts index f2c277068a..4b4d50c493 100644 --- a/src/state/queries/messages/list-converations.ts +++ b/src/state/queries/messages/list-converations.ts @@ -10,6 +10,7 @@ import { import {useCurrentConvoId} from '#/state/messages/current-convo-id' import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const' import {useAgent} from '#/state/session' +import {decrementBadgeCount} from 'lib/notifications/notifications' export const RQKEY = ['convo-list'] type RQPageParam = string | undefined @@ -116,10 +117,18 @@ export function useOnMarkAsRead() { return useCallback( (chatId: string) => { queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) => { - return optimisticUpdate(chatId, old, convo => ({ - ...convo, - unreadCount: 0, - })) + return optimisticUpdate(chatId, old, convo => { + // We only want to decrement the badge by one no matter the unread count, since we only increment once per + // sender regardless of message count + if (convo.unreadCount > 0) { + decrementBadgeCount(1) + } + + return { + ...convo, + unreadCount: 0, + } + }) }) }, [queryClient],