Skip to content

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

mobile: fix-backups-bug

ammarahm-edc9450ea141ec
diff --git a/apps/mobile/app/app.js b/apps/mobile/app/app.js
index c9df8ee91..69264a0f7 100644
--- a/apps/mobile/app/app.js
+++ b/apps/mobile/app/app.js
@@ -30,16 +30,19 @@ import SettingsService from "./services/settings";
import { TipManager } from "./services/tip-manager";
import { useUserStore } from "./stores/use-user-store";
import { View } from "react-native";
+import { useState } from "react";
SettingsService.init();
SettingsService.checkOrientation();
const App = () => {
useAppEvents();
+ const [init, setInit] = useState(false);
useEffect(() => {
let { appLockMode } = SettingsService.get();
if (appLockMode && appLockMode !== "none") {
useUserStore.getState().setVerifyUser(true);
}
+ setInit(true);
setTimeout(async () => {
SettingsService.onFirstLaunch();
await Notifications.get();
@@ -77,7 +80,7 @@ const App = () => {
}}
>
<ApplicationHolder />
- <Launcher />
+ {init && <Launcher />}
</GestureHandlerRootView>
</View>
);
diff --git a/apps/mobile/app/components/launcher/index.js b/apps/mobile/app/components/launcher/index.js
index 81aa2b548..1818c785a 100644
--- a/apps/mobile/app/components/launcher/index.js
+++ b/apps/mobile/app/components/launcher/index.js
@@ -24,7 +24,6 @@ import { checkVersion } from "react-native-check-version";
import { enabled } from "react-native-privacy-snapshot";
import { DatabaseLogger, db, loadDatabase } from "../../common/database";
import { useAppState } from "../../hooks/use-app-state";
-import BackupService from "../../services/backup";
import BiometricService from "../../services/biometrics";
import {
eSendEvent,
@@ -133,20 +132,7 @@ const Launcher = React.memo(
eSendEvent("session_expired");
return;
}
- const user = await db.user.getUser();
await useMessageStore.getState().setAnnouncement();
- if (PremiumService.get() && user) {
- if (SettingsService.get().reminder === "off") {
- SettingsService.set({ reminder: "daily" });
- }
- if (
- await BackupService.checkBackupRequired(
- SettingsService.get().reminder
- )
- ) {
- sleep(2000).then(() => BackupService.run());
- }
- }
if (NewFeature.present()) return;
if (await checkAppUpdateAvailable()) return;
if (await checkForRateAppRequest()) return;
diff --git a/apps/mobile/app/hooks/use-app-events.js b/apps/mobile/app/hooks/use-app-events.js
index 879077815..6e518cbe5 100644
--- a/apps/mobile/app/hooks/use-app-events.js
+++ b/apps/mobile/app/hooks/use-app-events.js
@@ -67,6 +67,8 @@ import { useDragState } from "../screens/settings/editor/state";
import { useCallback } from "react";
import { clearAppState } from "../screens/editor/tiptap/utils";
import { tabBarRef } from "../utils/global-refs";
+import BackupService from "../services/backup";
+import { sleep } from "../utils/time";
const SodiumEventEmitter = new NativeEventEmitter(NativeModules.Sodium);
export const useAppEvents = () => {
@@ -75,6 +77,7 @@ export const useAppEvents = () => {
const setUser = useUserStore((state) => state.setUser);
const syncedOnLaunch = useRef(false);
const verify = useUserStore((state) => state.verifyUser);
+ const syncing = useUserStore((state) => state.syncing);
const refValues = useRef({
subsriptionSuccessListener: null,
subsriptionErrorListener: null,
@@ -83,7 +86,8 @@ export const useAppEvents = () => {
showingDialog: false,
removeInternetStateListener: null,
isReconnecting: false,
- initialUrl: null
+ initialUrl: null,
+ backupDidWait: false
});
const onLoadingAttachmentProgress = (data) => {
@@ -213,7 +217,7 @@ export const useAppEvents = () => {
}, []);
const onSyncComplete = useCallback(async () => {
- console.log('Sync complete');
+ console.log("Sync complete");
initAfterSync();
setLastSynced(await db.lastSynced());
eSendEvent(eCloseProgressDialog, "sync_progress");
@@ -349,10 +353,47 @@ export const useAppEvents = () => {
refValues.current.isUserReady = true;
syncedOnLaunch.current = true;
+ if (!login) {
+ checkAutoBackup();
+ }
},
- [attachIAPListeners, onEmailVerified, setLastSynced, setUser]
+ [
+ attachIAPListeners,
+ onEmailVerified,
+ setLastSynced,
+ setUser,
+ checkAutoBackup
+ ]
);
+ const checkAutoBackup = useCallback(async () => {
+ if (verify || syncing) {
+ console.log("backup is waiting");
+ refValues.current.backupDidWait = true;
+ return;
+ }
+ console.log("backup running immediate");
+ const user = await db.user.getUser();
+ if (PremiumService.get() && user) {
+ if (SettingsService.get().reminder === "off") {
+ SettingsService.set({ reminder: "daily" });
+ }
+ if (
+ await BackupService.checkBackupRequired(SettingsService.get().reminder)
+ ) {
+ sleep(2000).then(() => BackupService.run());
+ }
+ }
+ }, [verify, syncing]);
+
+ useEffect(() => {
+ if (!verify && !syncing && refValues.current.backupDidWait) {
+ console.log("backup run after wait");
+ refValues.current.backupDidWait = false;
+ checkAutoBackup();
+ }
+ }, [verify, syncing, checkAutoBackup]);
+
const onSuccessfulSubscription = async (subscription) => {
await PremiumService.subscriptions.set(subscription);
await PremiumService.subscriptions.verify(subscription);
diff --git a/apps/mobile/app/services/message.js b/apps/mobile/app/services/message.js
index 80b824017..db1ce1fa8 100644
--- a/apps/mobile/app/services/message.js
+++ b/apps/mobile/app/services/message.js
@@ -122,3 +122,19 @@ const noMessage = {
export function clearMessage() {
useMessageStore.getState().setMessage(noMessage);
}
+
+const autoBackupsOff = {
+ visible: true,
+ message: "Automatic backups turned off",
+ actionText: "Get Notesnook Pro to enable automatic backups",
+ onPress: () => {
+ clearMessage();
+ },
+ data: {},
+ icon: "backup-restore",
+ type: "error"
+};
+
+export function setAutobackOffMessage() {
+ useMessageStore.getState().setMessage(autoBackupsOff);
+}
diff --git a/apps/mobile/app/services/premium.js b/apps/mobile/app/services/premium.js
index bdedcb685..dd463fbdb 100644
--- a/apps/mobile/app/services/premium.js
+++ b/apps/mobile/app/services/premium.js
@@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { CHECK_IDS } from "@notesnook/core/common";
import React from "react";
import { Platform } from "react-native";
+import Config from "react-native-config";
import * as RNIap from "react-native-iap";
import { db } from "../common/database";
import { MMKV } from "../common/database/mmkv";
@@ -35,10 +36,8 @@ import {
eShowGetPremium
} from "../utils/events";
import { eSendEvent, presentSheet, ToastEvent } from "./event-manager";
-import Config from "react-native-config";
import SettingsService from "./settings";
-import { sleep } from "../utils/time";
let premiumStatus = 0;
let products = [];
let user = null;
@@ -72,6 +71,9 @@ async function setPremiumStatus() {
console.log("subscriptions: ", e);
}
}
+ if (premiumStatus === 0 && !__DEV__) {
+ SettingsService.reset();
+ }
}
function getMontlySub() {
diff --git a/apps/mobile/app/services/settings.ts b/apps/mobile/app/services/settings.ts
index b4c7bed93..cf77b9924 100644
--- a/apps/mobile/app/services/settings.ts
+++ b/apps/mobile/app/services/settings.ts
@@ -26,6 +26,17 @@ import { AndroidModule } from "../utils";
import { getColorScheme } from "../utils/color-scheme/utils";
import { scale, updateSize } from "../utils/size";
import { DDS } from "./device-detection";
+import { setAutobackOffMessage } from "./message";
+
+function reset() {
+ const settings = get();
+ if (settings.reminder !== "off" && settings.reminder !== "useroff") {
+ settings.encryptedBackup = false;
+ settings.reminder = "useroff";
+ setAutobackOffMessage();
+ set(settings);
+ }
+}
function init() {
scale.fontScale = 1;
@@ -128,7 +139,8 @@ const SettingsService = {
get,
toggle,
onFirstLaunch,
- checkOrientation
+ checkOrientation,
+ reset
};
export default SettingsService;