From 15858db21bed2b5da58b8b5a195dc73942db36ad Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Wed, 3 May 2023 10:55:35 +0500 Subject: [PATCH] mobile: sync quick notes only when online --- apps/mobile/app/app.js | 8 -------- apps/mobile/app/services/notifications.ts | 13 +++++++++++-- apps/mobile/native/index.js | 9 +++++++++ apps/mobile/share/share.js | 10 +++++++++- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/apps/mobile/app/app.js b/apps/mobile/app/app.js index ccb5b3440..a9d752706 100644 --- a/apps/mobile/app/app.js +++ b/apps/mobile/app/app.js @@ -31,14 +31,6 @@ import { TipManager } from "./services/tip-manager"; import { useUserStore } from "./stores/use-user-store"; import { View } from "react-native"; import { useState } from "react"; -import NetInfo from "@react-native-community/netinfo"; -NetInfo.configure({ - reachabilityUrl: "https://notesnook.com", - reachabilityTest: (response) => { - if (!response) return false; - return response?.status >= 200 && response?.status < 300; - } -}); SettingsService.init(); SettingsService.checkOrientation(); diff --git a/apps/mobile/app/services/notifications.ts b/apps/mobile/app/services/notifications.ts index a495fe0ff..4d664065b 100644 --- a/apps/mobile/app/services/notifications.ts +++ b/apps/mobile/app/services/notifications.ts @@ -45,6 +45,7 @@ import { sleep } from "../utils/time"; import { useRelationStore } from "../stores/use-relation-store"; import { useReminderStore } from "../stores/use-reminder-store"; import { presentDialog } from "../components/dialog/functions"; +import NetInfo from "@react-native-community/netinfo"; export type Reminder = { id: string; @@ -193,7 +194,7 @@ const onEvent = async ({ type, detail }: Event) => { case "Hide": unpinQuickNote(); break; - case "ReplyInput": + case "ReplyInput": { displayNotification({ title: "Quick note", message: 'Tap on "Take note" to add a note.', @@ -211,9 +212,17 @@ const onEvent = async ({ type, detail }: Event) => { data: `

${input}

` } }); - await db.sync(false, false); + const status = await NetInfo.fetch(); + if (status.isInternetReachable) { + try { + await db.sync(false, false); + } catch (e) { + console.log(e, (e as Error).stack); + } + } useNoteStore.getState().setNotes(); break; + } } } }; diff --git a/apps/mobile/native/index.js b/apps/mobile/native/index.js index 8713a1370..ec68efd09 100644 --- a/apps/mobile/native/index.js +++ b/apps/mobile/native/index.js @@ -7,6 +7,15 @@ import Config from 'react-native-config'; import { SafeAreaProvider } from 'react-native-safe-area-context'; import appJson from './app.json'; import Notifications from "../app/services/notifications"; +import NetInfo from "@react-native-community/netinfo"; +NetInfo.configure({ + reachabilityUrl: "https://notesnook.com", + reachabilityTest: (response) => { + if (!response) return false; + return response?.status >= 200 && response?.status < 300; + } +}); + Notifications.init(); const appName = appJson.name; diff --git a/apps/mobile/share/share.js b/apps/mobile/share/share.js index 8b5cc5743..6979696a6 100644 --- a/apps/mobile/share/share.js +++ b/apps/mobile/share/share.js @@ -48,6 +48,7 @@ import { eOnLoadNote } from "../app/utils/events"; import { Editor } from "./editor"; import { Search } from "./search"; import { initDatabase, useShareStore } from "./store"; +import NetInfo from "@react-native-community/netinfo"; const getLinkPreview = (url) => { return getPreviewData(url, 5000); }; @@ -355,7 +356,14 @@ const ShareView = ({ quicknote = false }) => { } } } - await db.sync(false, false); + const status = await NetInfo.fetch(); + if (status.isInternetReachable) { + try { + await db.sync(false, false); + } catch (e) { + console.log(e, e.stack); + } + } await Storage.write("notesAddedFromIntent", "added"); close(); setLoading(false);