Skip to content

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

feat: on demand migration

ammarahm-ed0ab05fb6c2e8
diff --git a/apps/mobile/src/components/launcher/index.js b/apps/mobile/src/components/launcher/index.js
index e519970ae..0ed3fd20f 100644
--- a/apps/mobile/src/components/launcher/index.js
+++ b/apps/mobile/src/components/launcher/index.js
@@ -1,6 +1,5 @@
-import { EVENTS } from '@streetwriters/notesnook-core/common';
import React, { useEffect, useRef } from 'react';
-import { NativeModules, Platform, View } from 'react-native';
+import { Platform, View } from 'react-native';
import RNBootSplash from 'react-native-bootsplash';
import { checkVersion } from 'react-native-check-version';
import { editorState } from '../../screens/editor/tiptap/utils';
@@ -19,11 +18,12 @@ import { useThemeStore } from '../../stores/use-theme-store';
import { useUserStore } from '../../stores/use-user-store';
import { DatabaseLogger, db, loadDatabase } from '../../utils/database';
import { MMKV } from '../../utils/database/mmkv';
-import { eCloseProgressDialog, eOpenAnnouncementDialog } from '../../utils/events';
+import { eOpenAnnouncementDialog } from '../../utils/events';
import { tabBarRef } from '../../utils/global-refs';
import { SIZE } from '../../utils/size';
import { sleep } from '../../utils/time';
import { SVG } from '../auth/background';
+import Migrate from '../sheets/migrate';
import NewFeature from '../sheets/new-feature/index';
import { Update } from '../sheets/update';
import { Button } from '../ui/button';
@@ -48,7 +48,6 @@ const Launcher = React.memo(
const password = useRef();
const introCompleted = useSettingStore(state => state.settings.introCompleted);
const dbInitCompleted = useRef(false);
-
const loadNotes = async () => {
if (verifyUser) {
return;
@@ -61,37 +60,35 @@ const Launcher = React.memo(
setImmediate(() => {
setLoading(false);
setImmediate(() => doAppLoadActions());
- db?.eventManager?.unsubscribe(EVENTS.databaseMigrating, onDatabaseMigratingProgess);
});
});
});
};
- const onDatabaseMigratingProgess = React.useCallback(data => {
- presentSheet({
- progress: true,
- title: 'Migrating database',
- paragraph: 'Please wait while we migrate your data. Do not close the app.'
- });
- });
-
- const onMigrationCompleted = React.useCallback(data => {
- eSendEvent(eCloseProgressDialog);
- });
-
const init = async () => {
if (!dbInitCompleted.current) {
await RNBootSplash.hide({ fade: true });
await loadDatabase();
- db?.eventManager?.subscribe(EVENTS.databaseMigrating, onDatabaseMigratingProgess);
- db?.eventManager?.subscribe(EVENTS.databaseMigrated, onMigrationCompleted);
DatabaseLogger.info('Initializing database');
await db.init();
dbInitCompleted.current = true;
}
+ if (db.migrations.required() && !verifyUser) {
+ presentSheet({
+ component: <Migrate />,
+ onClose: () => {
+ loadNotes();
+ },
+ disableClosing: true
+ });
+ return;
+ }
+
if (!verifyUser) {
loadNotes();
+ } else {
+ useUserStore.getState().setUser(await db.user?.getUser());
}
};
@@ -220,10 +217,6 @@ const Launcher = React.memo(
onUnlockBiometrics();
}
init();
-
- return () => {
- db?.eventManager?.unsubscribe(EVENTS.databaseMigrated, onMigrationCompleted);
- };
}, [verifyUser]);
const onSubmit = async () => {
diff --git a/apps/mobile/src/components/sheets/migrate/index.tsx b/apps/mobile/src/components/sheets/migrate/index.tsx
new file mode 100644
index 000000000..9f0347961
--- /dev/null
+++ b/apps/mobile/src/components/sheets/migrate/index.tsx
@@ -0,0 +1,116 @@
+import React, { useState } from 'react';
+import { View } from 'react-native';
+import BackupService from '../../../services/backup';
+import { eSendEvent, ToastEvent } from '../../../services/event-manager';
+import { useThemeStore } from '../../../stores/use-theme-store';
+import { db } from '../../../utils/database';
+import { eCloseProgressDialog, eCloseSimpleDialog } from '../../../utils/events';
+import { sleep } from '../../../utils/time';
+import { Dialog } from '../../dialog';
+import DialogHeader from '../../dialog/dialog-header';
+import { Button } from '../../ui/button';
+import { Notice } from '../../ui/notice';
+import Seperator from '../../ui/seperator';
+import { ProgressBarComponent } from '../../ui/svg/lazy';
+import Paragraph from '../../ui/typography/paragraph';
+import { presentDialog } from '../../dialog/functions';
+
+export default function Migrate() {
+ const colors = useThemeStore(state => state.colors);
+ const [loading, setLoading] = useState(false);
+
+ return (
+ <View
+ style={{
+ paddingHorizontal: 12,
+ paddingTop: 12
+ }}
+ >
+ <DialogHeader
+ title="Database migration required"
+ paragraph={
+ 'Due to new features we need to migrate your data to a newer version. This is NOT a destructive operation.'
+ }
+ />
+ <Seperator />
+ <Notice
+ type="alert"
+ selectable={true}
+ text={`Read before continuing:
+
+It is required that you download and save a backup of your data.
+
+Some merge conflicts in your notes after a migration are expected. It is recommended that you resolve them carefully. But if you are feeling reckless and want to risk losing some data, you can logout log back in.
+
+If you face any other issues or are unsure about what to do, feel free to reach out to us via https://discord.com/invite/zQBK97EE22 or email us at [email protected]`}
+ />
+
+ {loading ? (
+ <>
+ <View
+ style={{
+ width: 200,
+ height: 100,
+ alignSelf: 'center',
+ justifyContent: 'center'
+ }}
+ >
+ <ProgressBarComponent
+ height={5}
+ width={200}
+ animated={true}
+ useNativeDriver
+ indeterminate
+ unfilledColor={colors.nav}
+ color={colors.accent}
+ borderWidth={0}
+ />
+
+ <Paragraph
+ style={{
+ marginTop: 5,
+ textAlign: 'center'
+ }}
+ >
+ Migration in progress... please wait
+ </Paragraph>
+ </View>
+ </>
+ ) : (
+ <Button
+ title="Start migration"
+ type="accent"
+ width={250}
+ onPress={async () => {
+ setLoading(true);
+ const backupSaved = await BackupService.run(false, 'local');
+ if (!backupSaved) {
+ ToastEvent.show({
+ heading: 'Migration failed',
+ message: 'You must download a backup of your data before migrating.',
+ context: 'local'
+ });
+ setLoading(false);
+ }
+ await db.migrations?.migrate();
+ eSendEvent(eCloseProgressDialog);
+ setLoading(false);
+ await sleep(500);
+ presentDialog({
+ title: 'Migration successful',
+ paragraph:
+ 'Your data has been migrated. If you face any issues after the migration please reach out to us via email or Discord.',
+ context: 'global',
+ negativeText: 'Ok'
+ });
+ }}
+ style={{
+ borderRadius: 100,
+ height: 45
+ }}
+ />
+ )}
+ <Dialog context="local" />
+ </View>
+ );
+}
diff --git a/apps/mobile/src/components/ui/notice/index.tsx b/apps/mobile/src/components/ui/notice/index.tsx
index 2d69f43a8..b9acc70f3 100644
--- a/apps/mobile/src/components/ui/notice/index.tsx
+++ b/apps/mobile/src/components/ui/notice/index.tsx
@@ -9,9 +9,10 @@ export interface NoticeProps {
type?: 'alert' | 'information';
text: string;
size?: 'small' | 'large';
+ selectable?: boolean;
}
-export const Notice = ({ type = 'alert', text, size = 'large' }: NoticeProps) => {
+export const Notice = ({ type = 'alert', text, size = 'large', selectable }: NoticeProps) => {
const colors = useThemeStore(state => state.colors);
const isSmall = size === 'small';
@@ -21,7 +22,8 @@ export const Notice = ({ type = 'alert', text, size = 'large' }: NoticeProps) =>
padding: 12,
flexDirection: 'row',
backgroundColor: colors.nav,
- borderRadius: isSmall ? 5 : 10
+ borderRadius: isSmall ? 5 : 10,
+ alignItems: 'flex-start'
}}
>
<IconButton
@@ -29,7 +31,8 @@ export const Notice = ({ type = 'alert', text, size = 'large' }: NoticeProps) =>
name={type}
customStyle={{
width: isSmall ? undefined : 40,
- height: isSmall ? undefined : 40
+ height: isSmall ? undefined : 40,
+ alignSelf: 'flex-start'
}}
color={type === 'alert' ? colors.errorText : colors.accent}
/>
@@ -38,6 +41,7 @@ export const Notice = ({ type = 'alert', text, size = 'large' }: NoticeProps) =>
marginLeft: 10,
flexShrink: 1
}}
+ selectable={selectable}
size={isSmall ? SIZE.xs + 1 : SIZE.sm}
>
{text}
diff --git a/apps/mobile/src/navigation/navigationstack.js b/apps/mobile/src/navigation/navigationstack.js
index 25710a3d7..0f668fbeb 100644
--- a/apps/mobile/src/navigation/navigationstack.js
+++ b/apps/mobile/src/navigation/navigationstack.js
@@ -63,7 +63,7 @@ const Tabs = React.memo(
() => {
const colors = useThemeStore(state => state.colors);
const homepage = SettingsService.get().homepage;
- const showWelcome = !SettingsService.get().introCompleted;
+ const introCompleted = useSettingStore(state => state.settings.introCompleted);
const height = useSettingStore(state => state.dimensions.height);
const insets = useSafeAreaInsets();
const screenHeight = height - (50 + insets.top + insets.bottom);
@@ -76,7 +76,7 @@ const Tabs = React.memo(
return (
<NativeStack.Navigator
tabBar={() => null}
- initialRouteName={showWelcome ? 'Welcome' : homepage}
+ initialRouteName={!introCompleted ? 'Welcome' : homepage}
backBehavior="history"
screenOptions={{
headerShown: false,
@@ -84,7 +84,7 @@ const Tabs = React.memo(
animation: 'none',
contentStyle: {
backgroundColor: colors.bg,
- height: screenHeight
+ height: !introCompleted ? undefined : screenHeight
}
}}
>
diff --git a/apps/mobile/src/screens/settings/applock.js b/apps/mobile/src/screens/settings/applock.js
index 5b7f50c06..eca7dae17 100644
--- a/apps/mobile/src/screens/settings/applock.js
+++ b/apps/mobile/src/screens/settings/applock.js
@@ -12,13 +12,15 @@ import { BouncingView } from '../../components/ui/transitions/bouncing-view';
import Heading from '../../components/ui/typography/heading';
import Paragraph from '../../components/ui/typography/paragraph';
import { DDS } from '../../services/device-detection';
-import { presentSheet } from '../../services/event-manager';
+import { presentSheet, ToastEvent } from '../../services/event-manager';
import SettingsService from '../../services/settings';
import { useSettingStore } from '../../stores/use-setting-store';
import { useThemeStore } from '../../stores/use-theme-store';
import { getElevation } from '../../utils';
import umami from '../../utils/analytics';
import { SIZE } from '../../utils/size';
+import BiometicService from '../../services/biometrics';
+import { useUserStore } from '../../stores/use-user-store';
const AppLock = ({ navigation, route }) => {
const colors = useThemeStore(state => state.colors);
@@ -118,7 +120,19 @@ const AppLock = ({ navigation, route }) => {
<PressableButton
key={item.title}
type={appLockMode === item.value ? 'grayBg' : 'transparent'}
- onPress={() => {
+ onPress={async () => {
+ if (
+ !(await BiometicService.isBiometryAvailable()) &&
+ !useUserStore.getState().user
+ ) {
+ ToastEvent.show({
+ heading: 'Biometrics not enrolled',
+ type: 'error',
+ message:
+ 'To use app lock, you must enable biometrics such as Fingerprint lock or Face ID on your phone or create an account.'
+ });
+ return;
+ }
SettingsService.set({ appLockMode: item.value });
}}
customStyle={{
diff --git a/apps/mobile/src/services/backup.js b/apps/mobile/src/services/backup.js
index d2ccb9599..4d6fe7298 100644
--- a/apps/mobile/src/services/backup.js
+++ b/apps/mobile/src/services/backup.js
@@ -40,7 +40,7 @@ async function getDirectoryAndroid() {
return subfolder;
}
-async function checkBackupDirExists(reset = false) {
+async function checkBackupDirExists(reset = false, context = 'global') {
if (Platform.OS === 'ios') return true;
let dir = SettingsService.get().backupDirectoryAndroid;
if (reset) dir = null;
@@ -65,7 +65,11 @@ async function checkBackupDirExists(reset = false) {
positivePress: async () => {
resolve(await getDirectoryAndroid());
},
- positiveText: 'Select'
+ onClose: () => {
+ resolve(null);
+ },
+ positiveText: 'Select',
+ context: context
});
});
}
@@ -123,8 +127,8 @@ async function updateNextBackupTime() {
});
}
-async function run(progress) {
- let androidBackupDirectory = await checkBackupDirExists();
+async function run(progress, context) {
+ let androidBackupDirectory = await checkBackupDirExists(false, context);
if (!androidBackupDirectory) return;
let backup;
@@ -179,6 +183,7 @@ async function run(progress) {
let showBackupCompleteSheet = SettingsService.get().showBackupCompleteSheet;
console.log(backupFilePath);
+ if (context) return backupFilePath;
await sleep(300);
if (showBackupCompleteSheet) {
presentBackupCompleteSheet(backupFilePath);
@@ -189,6 +194,7 @@ async function run(progress) {
} catch (e) {
progress && eSendEvent(eCloseProgressDialog);
ToastEvent.error(e, 'Backup failed!');
+ return null;
}
}