Add naive badge count implementation

This commit is contained in:
Matthew Remmel
2025-09-23 11:34:53 -04:00
committed by hodlbod
parent 5cb22d0bed
commit d1e5aee84e
3 changed files with 37 additions and 2 deletions
+5
View File
@@ -20,6 +20,7 @@
playAlertSound,
} from "@app/core/state"
import {deleteAlert, createDmAlert} from "@app/core/commands"
import {clearBadges} from "../util/notifications"
type Props = {
url?: string
@@ -74,6 +75,10 @@
const onShowBadgeOnUnreadToggle = async () => {
$showUnreadBadge = !$showUnreadBadge
if (!$showUnreadBadge) {
clearBadges()
}
}
const onDirectMessagesNotificationSoundToggle = async () => {
+28 -2
View File
@@ -1,4 +1,4 @@
import {derived} from "svelte/store"
import {derived, get} from "svelte/store"
import {synced, throttled} from "@welshman/store"
import {pubkey, relaysByUrl} from "@welshman/app"
import {prop, spec, identity, now, groupBy} from "@welshman/lib"
@@ -12,8 +12,16 @@ import {
makeSpaceChatPath,
makeRoomPath,
} from "@app/util/routes"
import {chats, hasNip29, getUrlsForEvent, userRoomsByUrl, repositoryStore} from "@app/core/state"
import {
chats,
hasNip29,
getUrlsForEvent,
userRoomsByUrl,
repositoryStore,
showUnreadBadge,
} from "@app/core/state"
import {preferencesStorageProvider} from "@src/lib/storage"
import {Badge} from "@capawesome/capacitor-badge"
// Checked state
@@ -57,6 +65,7 @@ export const notifications = derived(
return true
}
// can update to tuple of (path, event_type) if later filtering is desired
const paths = new Set<string>()
for (const {pubkeys, messages} of $chats) {
@@ -150,3 +159,20 @@ export const notifications = derived(
return paths
},
)
export const badgeCount = derived(notifications, notifications => {
// do filtering as desired?
return notifications.size
})
export const handleBadgeCountChanges = (count: number) => {
if (get(showUnreadBadge)) {
Badge.set({count})
} else {
clearBadges()
}
}
export const clearBadges = () => {
Badge.clear()
}
+4
View File
@@ -109,6 +109,7 @@
import * as requests from "@app/core/requests"
import * as notifications from "@app/util/notifications"
import * as appState from "@app/core/state"
import {badgeCount, handleBadgeCountChanges} from "@app/util/notifications"
// Migration: old nostrtalk instance used different sessions
if ($session && !$signer) {
@@ -465,6 +466,9 @@
},
)
// subscribe to badge count for changes
badgeCount.subscribe(handleBadgeCountChanges)
// Listen for signer errors, report to user via toast
signerLog.subscribe(
throttle(10_000, $log => {