diff --git a/src/app/components/Alerts.svelte b/src/app/components/Alerts.svelte index fa84626d..279a6eb5 100644 --- a/src/app/components/Alerts.svelte +++ b/src/app/components/Alerts.svelte @@ -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 () => { diff --git a/src/app/util/notifications.ts b/src/app/util/notifications.ts index f0ab4bd4..7910f012 100644 --- a/src/app/util/notifications.ts +++ b/src/app/util/notifications.ts @@ -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() 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() +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 230aa990..2c0baa0e 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -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 => {