[🐴] Decrement app badge when opening unread chat (#4040)

* decrement badge count for chats

* handle decrement in `useMarkAsRead`

* remove async

* oops
zio/stable
Hailey 2024-05-16 12:15:35 -07:00 committed by GitHub
parent 4bceabc21c
commit 5e8650a204
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 6 deletions

View File

@ -169,10 +169,11 @@ export function useNotificationsHandler() {
payload.reason === 'chat-message' && payload.reason === 'chat-message' &&
payload.recipientDid === currentAccount?.did payload.recipientDid === currentAccount?.did
) { ) {
const isCurrentConvo = payload.convoId === currentConvoId
return { return {
shouldShowAlert: payload.convoId !== currentConvoId, shouldShowAlert: !isCurrentConvo,
shouldPlaySound: false, shouldPlaySound: false,
shouldSetBadge: false, shouldSetBadge: !isCurrentConvo,
} }
} }

View File

@ -1,5 +1,6 @@
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 {BskyAgent} from '@atproto/api' import {BskyAgent} from '@atproto/api'
import {logger} from '#/logger' import {logger} from '#/logger'
@ -109,3 +110,14 @@ export function useRequestNotificationsPermission() {
[gate], [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)
}

View File

@ -10,6 +10,7 @@ import {
import {useCurrentConvoId} from '#/state/messages/current-convo-id' import {useCurrentConvoId} from '#/state/messages/current-convo-id'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const' import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
import {useAgent} from '#/state/session' import {useAgent} from '#/state/session'
import {decrementBadgeCount} from 'lib/notifications/notifications'
export const RQKEY = ['convo-list'] export const RQKEY = ['convo-list']
type RQPageParam = string | undefined type RQPageParam = string | undefined
@ -116,10 +117,18 @@ export function useOnMarkAsRead() {
return useCallback( return useCallback(
(chatId: string) => { (chatId: string) => {
queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) => { queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) => {
return optimisticUpdate(chatId, old, convo => ({ return optimisticUpdate(chatId, old, convo => {
...convo, // We only want to decrement the badge by one no matter the unread count, since we only increment once per
unreadCount: 0, // sender regardless of message count
})) if (convo.unreadCount > 0) {
decrementBadgeCount(1)
}
return {
...convo,
unreadCount: 0,
}
})
}) })
}, },
[queryClient], [queryClient],