From b07d7e4f3fb0fd142ce53f0841a52fdec7fe4957 Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Mon, 24 Mar 2025 13:58:02 +0500 Subject: [PATCH] mobile: restore pinned notes on reboot --- apps/mobile/app/hooks/use-actions.tsx | 13 +---- apps/mobile/app/hooks/use-app-events.tsx | 1 + apps/mobile/app/services/background-sync.ts | 1 + apps/mobile/app/services/notifications.ts | 59 +++++++++++++++++---- apps/mobile/app/services/settings.ts | 2 +- apps/mobile/app/stores/index.ts | 5 +- 6 files changed, 57 insertions(+), 24 deletions(-) diff --git a/apps/mobile/app/hooks/use-actions.tsx b/apps/mobile/app/hooks/use-actions.tsx index 2557b9bc2..b6dce79cb 100644 --- a/apps/mobile/app/hooks/use-actions.tsx +++ b/apps/mobile/app/hooks/use-actions.tsx @@ -651,17 +651,8 @@ export const useActions = ({ }); return; } - const text = await convertNoteToText(item as Note, true); - const html = (text || "").replace(/\n/g, "
"); - await Notifications.displayNotification({ - title: (item as Note).title, - message: (item as Note).headline || text || "", - subtitle: "", - bigText: html, - ongoing: true, - actions: ["UNPIN"], - id: item.id - }); + + Notifications.pinNote(item.id); await Notifications.get(); setNotifPinned(isNotePinnedInNotifications(item)); } diff --git a/apps/mobile/app/hooks/use-app-events.tsx b/apps/mobile/app/hooks/use-app-events.tsx index 4a064263e..e1760a074 100644 --- a/apps/mobile/app/hooks/use-app-events.tsx +++ b/apps/mobile/app/hooks/use-app-events.tsx @@ -410,6 +410,7 @@ const initializeDatabase = async (password?: string) => { Notifications.pinQuickNote(false); } DatabaseLogger.info("Database initialized"); + Notifications.restorePinnedNotes(); } Walkthrough.init(); }; diff --git a/apps/mobile/app/services/background-sync.ts b/apps/mobile/app/services/background-sync.ts index 25945148d..cc9703177 100644 --- a/apps/mobile/app/services/background-sync.ts +++ b/apps/mobile/app/services/background-sync.ts @@ -141,6 +141,7 @@ const onBoot = async () => { if (SettingsService.get().notifNotes) { Notifications.pinQuickNote(false); } + Notifications.restorePinnedNotes(); DatabaseLogger.info("BOOT TASK COMPLETE"); } catch (e) { DatabaseLogger.error(e as Error); diff --git a/apps/mobile/app/services/notifications.ts b/apps/mobile/app/services/notifications.ts index cc57a2fa0..ef48a9b46 100644 --- a/apps/mobile/app/services/notifications.ts +++ b/apps/mobile/app/services/notifications.ts @@ -53,6 +53,7 @@ import { eSendEvent } from "./event-manager"; import Navigation from "./navigation"; import SettingsService from "./settings"; import { getFormattedReminderTime } from "@notesnook/common"; +import { MMKV } from "../common/database/mmkv"; let pinned: DisplayedNotification[] = []; @@ -883,13 +884,6 @@ function init() { notifee.onForegroundEvent(onEvent); } -async function remove(id: string) { - await notifee.cancelNotification(id); - get().then(() => { - eSendEvent("onUpdate", "unpin"); - }); -} - async function pinQuickNote(launch: boolean) { useUserStore.setState({ disableAppLockRequests: true @@ -969,11 +963,49 @@ async function setupReminders(checkNeedsScheduling = false) { updateRemindersForWidget(); } +class PinnedNotesStorage { + static storageKey = "nn::pinnedNoteIds"; + static get() { + const items = MMKV.getArray(PinnedNotesStorage.storageKey); + if (!items) return []; + return items; + } + + static add(id: string) { + const items = PinnedNotesStorage.get(); + if (items.indexOf(id) === -1) { + items.push(id); + MMKV.setArray(PinnedNotesStorage.storageKey, items); + } + } + + static remove(id: string) { + const items = PinnedNotesStorage.get(); + const index = items.indexOf(id); + if (index > -1) { + items.splice(index, 1); + MMKV.setArray(PinnedNotesStorage.storageKey, items); + } + } + + static clear() { + MMKV.removeItem(PinnedNotesStorage.storageKey); + } +} + +async function remove(id: string) { + await notifee.cancelNotification(id); + PinnedNotesStorage.remove(id); + get().then(() => { + eSendEvent("onUpdate", "unpin"); + }); +} + async function pinNote(id: string) { try { const note = await db.notes.note(id as string); if (!note) return; - + PinnedNotesStorage.add(id); let text = await convertNoteToText(note, true); if (!text) text = ""; const html = text.replace(/\n/g, "
"); @@ -990,6 +1022,14 @@ async function pinNote(id: string) { /* empty */ } } + +async function restorePinnedNotes() { + const pinnedNotes = PinnedNotesStorage.get(); + for (const id of pinnedNotes) { + pinNote(id); + } +} + const Events = { onUpdate: "onUpdate" }; @@ -1013,7 +1053,8 @@ const Notifications = { isNotePinned, pinNote, Events, - updateRemindersForWidget + updateRemindersForWidget, + restorePinnedNotes }; export default Notifications; diff --git a/apps/mobile/app/services/settings.ts b/apps/mobile/app/services/settings.ts index 997c9bc27..2d242bdb4 100644 --- a/apps/mobile/app/services/settings.ts +++ b/apps/mobile/app/services/settings.ts @@ -123,10 +123,10 @@ function init() { scale.fontScale = settings.fontScale; } - setTimeout(() => setPrivacyScreen(settings), 1); updateSize(); useSettingStore.getState().setSettings({ ...settings }); migrateAppLock(); + setPrivacyScreen(settings); } function setPrivacyScreen(settings: SettingStore["settings"]) { diff --git a/apps/mobile/app/stores/index.ts b/apps/mobile/app/stores/index.ts index 7681a3ac4..098a666b9 100644 --- a/apps/mobile/app/stores/index.ts +++ b/apps/mobile/app/stores/index.ts @@ -39,17 +39,16 @@ export function initAfterSync(type: "full" | "send" = "send") { Navigation.queueRoutesForUpdate(); // Whenever sync completes, try to reschedule // any new/updated reminders. - Notifications.setupReminders(true); useRelationStore.getState().update(); useMenuStore.getState().setColorNotes(); useMenuStore.getState().setMenuPins(); useUserStore.setState({ profile: db.settings.getProfile() }); - - NotePreviewWidget.updateNotes(); eSendEvent(eAfterSync); } + Notifications.setupReminders(true); + NotePreviewWidget.updateNotes(); } export async function initialize() {}