mobile: restore pinned notes on reboot

This commit is contained in:
Ammar Ahmed
2025-03-24 13:58:02 +05:00
committed by Abdullah Atta
parent b141907066
commit b07d7e4f3f
6 changed files with 57 additions and 24 deletions

View File

@@ -651,17 +651,8 @@ export const useActions = ({
});
return;
}
const text = await convertNoteToText(item as Note, true);
const html = (text || "").replace(/\n/g, "<br />");
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));
}

View File

@@ -410,6 +410,7 @@ const initializeDatabase = async (password?: string) => {
Notifications.pinQuickNote(false);
}
DatabaseLogger.info("Database initialized");
Notifications.restorePinnedNotes();
}
Walkthrough.init();
};

View File

@@ -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);

View File

@@ -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<string>(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, "<br />");
@@ -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;

View File

@@ -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"]) {

View File

@@ -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() {}