Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: sync pinned notes to widget on note changes

kashaf-ansari-devdf802884254f
diff --git a/apps/mobile/app/hooks/use-actions.tsx b/apps/mobile/app/hooks/use-actions.tsx
index 906d0e73c..77a8c780f 100644
--- a/apps/mobile/app/hooks/use-actions.tsx
+++ b/apps/mobile/app/hooks/use-actions.tsx
@@ -60,6 +60,7 @@ import {
} from "../services/event-manager";
import Navigation from "../services/navigation";
import Notifications from "../services/notifications";
+import { PinnedNotesWidget } from "../services/pinned-notes-widget";
import SettingsService from "../services/settings";
import { useArchivedStore } from "../stores/use-archived-store";
import { useMenuStore } from "../stores/use-menu-store";
@@ -293,6 +294,7 @@ export const useActions = ({
if (item.type === "note") {
await db.notes.pin(!item?.pinned, item.id);
setItem((await db.notes.note(item.id)) as Item);
+ PinnedNotesWidget.updateNotes();
} else if (item.type === "notebook") {
await db.notebooks.pin(!item?.pinned, item.id);
setItem((await db.notebooks.notebook(item.id)) as Item);
diff --git a/apps/mobile/app/hooks/use-app-events.tsx b/apps/mobile/app/hooks/use-app-events.tsx
index 8b15aa470..8676484f5 100644
--- a/apps/mobile/app/hooks/use-app-events.tsx
+++ b/apps/mobile/app/hooks/use-app-events.tsx
@@ -82,6 +82,7 @@ import {
} from "../services/message";
import Navigation from "../services/navigation";
import { NotePreviewWidget } from "../services/note-preview-widget";
+import { PinnedNotesWidget } from "../services/pinned-notes-widget";
import Notifications from "../services/notifications";
import PremiumService from "../services/premium";
import SettingsService from "../services/settings";
@@ -580,6 +581,7 @@ export const useAppEvents = () => {
// (most obviously after the user clears app data). Nothing can run at that moment, so the
// first launch afterwards is the earliest chance to put them right.
NotePreviewWidget.updateNotes();
+ PinnedNotesWidget.updateNotes();
let subscriptions: EventManagerSubscription[] = [];
const eventManager = db.eventManager;
diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor.ts b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
index e278870c9..8c1d13b26 100644
--- a/apps/mobile/app/screens/editor/tiptap/use-editor.ts
+++ b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
@@ -49,6 +49,7 @@ import {
} from "../../../services/event-manager";
import Navigation from "../../../services/navigation";
import { NotePreviewWidget } from "../../../services/note-preview-widget";
+import { PinnedNotesWidget } from "../../../services/pinned-notes-widget";
import Notifications from "../../../services/notifications";
import SettingsService from "../../../services/settings";
import { useSettingStore } from "../../../stores/use-setting-store";
@@ -502,6 +503,9 @@ export const useEditor = (
timers.current.onsave = setTimeout(async () => {
if (!id || !note) return;
NotePreviewWidget.updateNote(id, note);
+ if (note.pinned) {
+ PinnedNotesWidget.updateNotes();
+ }
}, 500);
return id;
diff --git a/apps/mobile/app/services/background-sync.ts b/apps/mobile/app/services/background-sync.ts
index a747d31ea..4067d442e 100644
--- a/apps/mobile/app/services/background-sync.ts
+++ b/apps/mobile/app/services/background-sync.ts
@@ -27,6 +27,7 @@ import { DatabaseLogger, db, setupDatabase } from "../common/database";
import { deleteDCacheFiles } from "../common/filesystem/io";
import { useUserStore } from "../stores/use-user-store";
import { NotePreviewWidget } from "./note-preview-widget";
+import { PinnedNotesWidget } from "./pinned-notes-widget";
import Notifications from "./notifications";
import SettingsService from "./settings";
@@ -118,6 +119,7 @@ async function onBackgroundSyncStarted() {
}
Notifications.restorePinnedNotes();
NotePreviewWidget.updateNotes();
+ PinnedNotesWidget.updateNotes();
deleteDCacheFiles();
DatabaseLogger.info("BACKGROUND SYNC COMPLETE");
} catch (e) {
@@ -146,6 +148,7 @@ const onBoot = async () => {
}
Notifications.restorePinnedNotes();
NotePreviewWidget.updateNotes();
+ PinnedNotesWidget.updateNotes();
DatabaseLogger.info("BOOT TASK COMPLETE");
} catch (e) {
DatabaseLogger.error(e as Error);
diff --git a/apps/mobile/app/services/pinned-notes-widget.ts b/apps/mobile/app/services/pinned-notes-widget.ts
new file mode 100644
index 000000000..6b5fa3992
--- /dev/null
+++ b/apps/mobile/app/services/pinned-notes-widget.ts
@@ -0,0 +1,61 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+import { strings } from "@notesnook/intl";
+import { db } from "../common/database";
+import { NotesnookModule } from "../utils/notesnook-module";
+import { Platform } from "react-native";
+
+let timer: NodeJS.Timeout;
+
+/**
+ * Service to synchronize pinned notes from the Notesnook database to the native
+ * Android Pinned Notes home screen widget via SharedPreferences.
+ */
+export const PinnedNotesWidget = {
+ /**
+ * Queries all currently pinned notes, sanitizes them into a lightweight payload,
+ * stores them in SharedPreferences, and requests a widget redraw.
+ * Debounced to prevent redundant native updates during rapid consecutive edits.
+ */
+ updateNotes: () => {
+ if (Platform.OS !== "android") return;
+ clearTimeout(timer);
+ timer = setTimeout(async () => {
+ try {
+ const pinnedNotes = await db.notes.pinned.items();
+ const payload = (pinnedNotes || []).map((note) => ({
+ id: note.id,
+ title: note.title || strings.untitledNote(),
+ headline: note.headline || "",
+ dateEdited: note.dateEdited,
+ pinned: true
+ }));
+
+ NotesnookModule.setString(
+ "appPreview",
+ "pinnedNotesList",
+ JSON.stringify(payload)
+ );
+ NotesnookModule.updatePinnedNotesWidget();
+ } catch (err) {
+ console.error("Failed to update pinned notes widget:", err);
+ }
+ }, 500);
+ }
+};
diff --git a/apps/mobile/app/stores/index.ts b/apps/mobile/app/stores/index.ts
index 82831963a..01b5eeb8c 100644
--- a/apps/mobile/app/stores/index.ts
+++ b/apps/mobile/app/stores/index.ts
@@ -21,6 +21,7 @@ import { DatabaseLogger, db } from "../common/database";
import { eSendEvent } from "../services/event-manager";
import Navigation from "../services/navigation";
import { NotePreviewWidget } from "../services/note-preview-widget";
+import { PinnedNotesWidget } from "../services/pinned-notes-widget";
import Notifications from "../services/notifications";
import { eAfterSync } from "../utils/events";
import { NotesnookModule, ShortcutInfo } from "../utils/notesnook-module";
@@ -127,6 +128,7 @@ export function initAfterSync(type: "full" | "send" = "send") {
Notifications.setupReminders(true);
NotePreviewWidget.updateNotes();
+ PinnedNotesWidget.updateNotes();
eSendEvent(eAfterSync);
NotesnookModule.getAllShortcuts()