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 {logger} from '#/logger'
|
||||||
import {track} from 'lib/analytics/analytics'
|
import {track} from 'lib/analytics/analytics'
|
||||||
import {useAccountSwitcher} from 'lib/hooks/useAccountSwitcher'
|
import {useAccountSwitcher} from 'lib/hooks/useAccountSwitcher'
|
||||||
|
import {decrementBadgeCount} from 'lib/notifications/notifications'
|
||||||
import {NavigationProp} from 'lib/routes/types'
|
import {NavigationProp} from 'lib/routes/types'
|
||||||
import {logEvent} from 'lib/statsig/statsig'
|
import {logEvent} from 'lib/statsig/statsig'
|
||||||
import {isAndroid} from 'platform/detection'
|
import {isAndroid} from 'platform/detection'
|
||||||
@@ -88,6 +89,7 @@ export function useNotificationsHandler() {
|
|||||||
if (!payload) return
|
if (!payload) return
|
||||||
|
|
||||||
if (payload.reason === 'chat-message') {
|
if (payload.reason === 'chat-message') {
|
||||||
|
decrementBadgeCount(1)
|
||||||
if (payload.recipientDid !== currentAccount?.did && !storedPayload) {
|
if (payload.recipientDid !== currentAccount?.did && !storedPayload) {
|
||||||
storedPayload = payload
|
storedPayload = payload
|
||||||
closeAllActiveElements()
|
closeAllActiveElements()
|
||||||
@@ -169,10 +171,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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {isNative} from '#/platform/detection'
|
|||||||
import {useProfileShadow} from '#/state/cache/profile-shadow'
|
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 {decrementBadgeCount} from 'lib/notifications/notifications'
|
||||||
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'
|
||||||
import {UserAvatar} from '#/view/com/util/UserAvatar'
|
import {UserAvatar} from '#/view/com/util/UserAvatar'
|
||||||
@@ -103,11 +104,16 @@ function ChatListItemReady({
|
|||||||
setShowActions(true)
|
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', {
|
navigation.push('MessagesConversation', {
|
||||||
conversation: convo.id,
|
conversation: convo.id,
|
||||||
})
|
})
|
||||||
}, [convo.id, navigation])
|
}, [convo.id, convo.unreadCount, navigation])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View
|
<View
|
||||||
|
|||||||
Reference in New Issue
Block a user