A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
mobile: keep shortcuts synced with the app
Ammar Ahmed465e1af838f6
diff --git a/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/RCTNNativeModule.java b/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/RCTNNativeModule.javaindex 9cd572e67..a32e85852 100644--- a/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/RCTNNativeModule.java+++ b/apps/mobile/android/app/src/main/java/com/streetwriters/notesnook/RCTNNativeModule.java@@ -36,6 +36,7 @@ import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.Objects;+import java.util.Set;public class RCTNNativeModule extends ReactContextBaseJavaModule {@@ -263,11 +264,11 @@ public class RCTNNativeModule extends ReactContextBaseJavaModule {intent.setPackage(mContext.getPackageName());Icon icon = createLetterIcon(type, title);-ShortcutInfo shortcut = new ShortcutInfo.Builder(mContext, id).setShortLabel(title).setLongLabel(description != null && !description.isEmpty() ? description : title).setIcon(icon)+ .setCategories(Set.of(type)).setIntent(intent).build();@@ -324,7 +325,6 @@ public class RCTNNativeModule extends ReactContextBaseJavaModule {}if (existingShortcut == null) {- promise.reject("NOT_FOUND", "Shortcut not found");return;}@@ -369,6 +369,41 @@ public class RCTNNativeModule extends ReactContextBaseJavaModule {}}+ @ReactMethod+ public void getAllShortcuts(Promise promise) {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {+ return;+ }+ ShortcutManager shortcutManager = mContext.getSystemService(ShortcutManager.class);+ WritableArray shortcuts = Arguments.createArray();+ List<ShortcutInfo> infos = shortcutManager.getPinnedShortcuts();++ for (ShortcutInfo info: infos) {+ WritableMap data = Arguments.createMap();+ data.putString("id", info.getId());+ if (info.getShortLabel() != null) {+ data.putString("title", info.getShortLabel().toString());+ }++ if (info.getLongLabel() != null) {+ data.putString("description", info.getLongLabel().toString());+ }+++ if (!Objects.requireNonNull(info.getCategories()).isEmpty()) {+ if (info.getCategories().contains("note")) {+ data.putString("type", "note");+ } else if (info.getCategories().contains("notebook")) {+ data.putString("type", "notebook");+ } else if (info.getCategories().contains("tag")) {+ data.putString("type", "tag");+ }+ }+ shortcuts.pushMap(data);+ }+ promise.resolve(shortcuts);+ }+private Icon createLetterIcon(String type, String title) {String letter = type.contains("tag") ? "#" : title != null && !title.isEmpty()diff --git a/apps/mobile/app/hooks/use-actions.tsx b/apps/mobile/app/hooks/use-actions.tsxindex f52ad205e..d5962b9c8 100644--- a/apps/mobile/app/hooks/use-actions.tsx+++ b/apps/mobile/app/hooks/use-actions.tsx@@ -300,7 +300,6 @@ export const useActions = ({id: item.id,title: value});-eSendEvent(Navigation.routeNames.TaggedNotes);InteractionManager.runAfterInteractions(() => {useTagStore.getState().refresh();@@ -1199,13 +1198,14 @@ export const useActions = ({title: strings.addToHome(),icon: "cellphone-arrow-down",onPress: async () => {- const added = await NotesnookModule.addShortcut(- item.id,- item.type,- item.title,- (item as Note).headline || (item as Notebook).description || ""- );- console.log(added);+ try {+ await NotesnookModule.addShortcut(+ item.id,+ item.type,+ item.title,+ (item as Note).headline || (item as Notebook).description || ""+ );+ } catch (e) {}}});}diff --git a/apps/mobile/app/stores/index.ts b/apps/mobile/app/stores/index.tsindex 9171be95d..a1280e8c3 100644--- a/apps/mobile/app/stores/index.ts+++ b/apps/mobile/app/stores/index.ts@@ -17,12 +17,13 @@ You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/-import { db } from "../common/database";+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 Notifications from "../services/notifications";import { eAfterSync } from "../utils/events";+import { NotesnookModule, ShortcutInfo } from "../utils/notesnook-module";import { useFavoriteStore } from "./use-favorite-store";import { useMenuStore } from "./use-menu-store";import { useMonographStore } from "./use-monograph-store";@@ -34,6 +35,58 @@ import { useTagStore } from "./use-tag-store";import { useTrashStore } from "./use-trash-store";import { useUserStore } from "./use-user-store";+async function syncShortcuts(result: ShortcutInfo[]) {+ try {+ for (let shortcut of result) {+ switch (shortcut.type) {+ case "note":+ {+ const note = await db.notes.note(shortcut.id);+ if (!note) {+ NotesnookModule.removeShortcut(shortcut.id);+ } else if (note.title !== shortcut.title) {+ NotesnookModule.updateShortcut(+ shortcut.id,+ note.title,+ note.headline+ );+ }+ }+ break;+ case "notebook":+ {+ const notebook = await db.notebooks.notebook(shortcut.id);+ if (!notebook) {+ NotesnookModule.removeShortcut(shortcut.id);+ } else if (notebook.title !== shortcut.title) {+ NotesnookModule.updateShortcut(+ shortcut.id,+ notebook.title,+ notebook.description+ );+ }+ }+ break;+ case "tag":+ {+ const tag = await db.tags.tag(shortcut.id);+ if (!tag) {+ NotesnookModule.removeShortcut(shortcut.id);+ } else if (tag.title !== shortcut.title) {+ NotesnookModule.updateShortcut(shortcut.id, tag.title, tag.title);+ }+ }+ break;+ }+ }+ } catch (e) {+ DatabaseLogger.error(+ e as Error,+ "Error while syncing homescreen shortcuts"+ );+ }+}+export function initAfterSync(type: "full" | "send" = "send") {if (type === "full") {Navigation.queueRoutesForUpdate();@@ -46,9 +99,11 @@ export function initAfterSync(type: "full" | "send" = "send") {profile: db.settings.getProfile()});}+Notifications.setupReminders(true);NotePreviewWidget.updateNotes();eSendEvent(eAfterSync);+ NotesnookModule.getAllShortcuts().then(syncShortcuts);}export async function initialize() {}diff --git a/apps/mobile/app/utils/notesnook-module.ts b/apps/mobile/app/utils/notesnook-module.tsindex aa337f35e..9aac19f41 100644--- a/apps/mobile/app/utils/notesnook-module.ts+++ b/apps/mobile/app/utils/notesnook-module.ts@@ -19,6 +19,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.import { NativeModules, Platform } from "react-native";+export type ShortcutInfo = {+ id: string;+ title: string;+ description: string;+ type: "note" | "notebook" | "tag";+};+interface NotesnookModuleInterface {getActivityName: () => Promise<string>;setBackgroundColor: (color: string) => void;@@ -54,6 +61,7 @@ interface NotesnookModuleInterface {description?: string) => Promise<boolean>;removeAllShortcuts: () => Promise<boolean>;+ getAllShortcuts: () => Promise<ShortcutInfo[]>;}export const NotesnookModule: NotesnookModuleInterface = Platform.select({@@ -78,7 +86,8 @@ export const NotesnookModule: NotesnookModuleInterface = Platform.select({addShortcut: () => Promise.resolve(false),removeShortcut: () => Promise.resolve(false),updateShortcut: () => Promise.resolve(false),- removeAllShortcuts: () => Promise.resolve(false)+ removeAllShortcuts: () => Promise.resolve(false),+ getAllShortcuts: () => Promise.resolve([])},android: NativeModules.NNativeModule});