decrement badge count for chats
This commit is contained in:
@@ -6,6 +6,7 @@ import {useQueryClient} from '@tanstack/react-query'
|
||||
import {logger} from '#/logger'
|
||||
import {track} from 'lib/analytics/analytics'
|
||||
import {useAccountSwitcher} from 'lib/hooks/useAccountSwitcher'
|
||||
import {decrementBadgeCount} from 'lib/notifications/notifications'
|
||||
import {NavigationProp} from 'lib/routes/types'
|
||||
import {logEvent} from 'lib/statsig/statsig'
|
||||
import {isAndroid} from 'platform/detection'
|
||||
@@ -88,6 +89,7 @@ export function useNotificationsHandler() {
|
||||
if (!payload) return
|
||||
|
||||
if (payload.reason === 'chat-message') {
|
||||
decrementBadgeCount(1)
|
||||
if (payload.recipientDid !== currentAccount?.did && !storedPayload) {
|
||||
storedPayload = payload
|
||||
closeAllActiveElements()
|
||||
@@ -169,10 +171,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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import {isNative} from '#/platform/detection'
|
||||
import {useProfileShadow} from '#/state/cache/profile-shadow'
|
||||
import {useModerationOpts} from '#/state/preferences/moderation-opts'
|
||||
import {useSession} from '#/state/session'
|
||||
import {decrementBadgeCount} from 'lib/notifications/notifications'
|
||||
import {sanitizeDisplayName} from 'lib/strings/display-names'
|
||||
import {TimeElapsed} from '#/view/com/util/TimeElapsed'
|
||||
import {UserAvatar} from '#/view/com/util/UserAvatar'
|
||||
@@ -103,11 +104,16 @@ function ChatListItemReady({
|
||||
setShowActions(true)
|
||||
}, [])
|
||||
|
||||
const onPress = useCallback(() => {
|
||||
const onPress = useCallback(async () => {
|
||||
if (convo.unreadCount > 0) {
|
||||
// We only increment the badge once per conversation, so even if there are multiple unreads
|
||||
// we only want to decrement by one.
|
||||
decrementBadgeCount(1)
|
||||
}
|
||||
navigation.push('MessagesConversation', {
|
||||
conversation: convo.id,
|
||||
})
|
||||
}, [convo.id, navigation])
|
||||
}, [convo.id, convo.unreadCount, navigation])
|
||||
|
||||
return (
|
||||
<View
|
||||
|
||||
Reference in New Issue
Block a user