Skip to content

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

migrate app to scoped storage

ammarahm-ed9c73c88d7c48
diff --git a/apps/mobile/src/components/RecoveryKeyDialog/index.js b/apps/mobile/src/components/RecoveryKeyDialog/index.js
index 9be572382..341059724 100644
--- a/apps/mobile/src/components/RecoveryKeyDialog/index.js
+++ b/apps/mobile/src/components/RecoveryKeyDialog/index.js
@@ -3,12 +3,12 @@ import {Platform, View} from 'react-native';
import QRCode from 'react-native-qrcode-svg';
import Share from 'react-native-share';
import {LOGO_BASE64} from '../../assets/images/assets';
-import Clipboard from "@react-native-clipboard/clipboard"
+import Clipboard from '@react-native-clipboard/clipboard';
import {
eSendEvent,
eSubscribeEvent,
eUnSubscribeEvent,
- ToastEvent,
+ ToastEvent
} from '../../services/EventManager';
import {db} from '../../utils/database';
import {eOpenRecoveryKeyDialog, eOpenResultDialog} from '../../utils/Events';
@@ -22,6 +22,8 @@ import DialogHeader from '../Dialog/dialog-header';
import Seperator from '../Seperator';
import Paragraph from '../Typography/Paragraph';
+import * as ScopedStorage from 'react-native-scoped-storage';
+
let RNFetchBlob;
class RecoveryKeyDialog extends React.Component {
@@ -29,7 +31,7 @@ class RecoveryKeyDialog extends React.Component {
super(props);
this.state = {
key: null,
- visible: false,
+ visible: false
};
this.actionSheetRef = createRef();
this.svg = createRef();
@@ -38,17 +40,17 @@ class RecoveryKeyDialog extends React.Component {
this.tapCount = 0;
}
- open = (signup) => {
+ open = signup => {
if (signup) {
this.signup = true;
}
this.setState(
{
- visible: true,
+ visible: true
},
() => {
this.actionSheetRef.current?.setModalVisible(true);
- },
+ }
);
};
@@ -58,7 +60,7 @@ class RecoveryKeyDialog extends React.Component {
heading: 'Did you save recovery key?',
message: 'Tap one more time to confirm.',
type: 'success',
- context: 'local',
+ context: 'local'
});
this.tapCount++;
return;
@@ -67,7 +69,7 @@ class RecoveryKeyDialog extends React.Component {
this.actionSheetRef.current?.setModalVisible(false);
sleep(200).then(() => {
this.setState({
- visible: false,
+ visible: false
});
});
if (this.signup) {
@@ -78,12 +80,13 @@ class RecoveryKeyDialog extends React.Component {
paragraph:
'Please confirm your email to encrypt and sync all your notes.',
icon: 'check',
- button: 'Start taking notes',
+ button: 'Start taking notes'
});
}, 500);
}
};
async componentDidMount() {
+
eSubscribeEvent(eOpenRecoveryKeyDialog, this.open);
}
@@ -92,55 +95,36 @@ class RecoveryKeyDialog extends React.Component {
}
saveQRCODE = async () => {
- if ((await Storage.requestPermission()) === false) {
- ToastEvent.show({
- heading: 'Storage access not granted',
- message: 'You must provide access to phone storage to save QR-Code',
- type: 'error',
- context: 'local',
- });
- return;
- }
-
- this.svg.current?.toDataURL(async (data) => {
+ this.svg.current?.toDataURL(async data => {
try {
let path = await Storage.checkAndCreateDir('/');
RNFetchBlob = require('rn-fetch-blob').default;
let fileName = 'nn_' + this.user.email + '_recovery_key_qrcode';
fileName = sanitizeFilename(fileName, {replacement: '_'});
fileName = fileName + '.png';
- await RNFetchBlob.fs.writeFile(path + fileName, data, 'base64');
if (Platform.OS === 'android') {
- await RNFetchBlob.fs.scanFile([
- {
- path: path + fileName,
- mime: 'image/png',
- },
- ]);
+ await ScopedStorage.createDocument(
+ fileName,
+ 'image/png',
+ data,
+ 'base64'
+ );
+ } else {
+ await RNFetchBlob.fs.writeFile(path + fileName, data, 'base64');
}
ToastEvent.show({
heading: 'Recovery key QR-Code saved',
message:
'QR-Code image has been saved to Gallery at ' + path + fileName,
type: 'success',
- context: 'local',
+ context: 'local'
});
} catch (e) {}
});
};
saveToTextFile = async () => {
- if ((await Storage.requestPermission()) === false) {
- ToastEvent.show({
- heading: 'Storage access not granted',
- message:
- 'You must provide access to phone storage to save recovery key text file.',
- type: 'error',
- context: 'local',
- });
- return;
- }
try {
let path = await Storage.checkAndCreateDir('/');
let fileName = 'nn_' + this.user?.email + '_recovery_key';
@@ -148,13 +132,22 @@ class RecoveryKeyDialog extends React.Component {
fileName = fileName + '.txt';
RNFetchBlob = require('rn-fetch-blob').default;
- await RNFetchBlob.fs.writeFile(path + fileName, this.state.key, 'utf8');
+ if (Platform.OS === 'android') {
+ await ScopedStorage.createDocument(
+ fileName,
+ 'text/plain',
+ this.state.key,
+ 'utf8'
+ );
+ } else {
+ await RNFetchBlob.fs.writeFile(path + fileName, this.state.key, 'utf8');
+ }
ToastEvent.show({
heading: 'Recovery key text file saved',
message: 'Recovery key saved in text file at ' + path + fileName,
type: 'success',
- context: 'local',
+ context: 'local'
});
return path + fileName;
} catch (e) {
@@ -167,7 +160,7 @@ class RecoveryKeyDialog extends React.Component {
this.user = await db.user.getUser();
if (k) {
this.setState({
- key: k.key,
+ key: k.key
});
}
};
@@ -179,7 +172,7 @@ class RecoveryKeyDialog extends React.Component {
await Share.open({
url: Platform.OS === 'ios' ? path : 'file:/' + path,
title: 'Save recovery key to cloud',
- failOnCancel: false,
+ failOnCancel: false
});
} catch (e) {}
};
@@ -200,7 +193,7 @@ class RecoveryKeyDialog extends React.Component {
justifyContent: 'space-between',
paddingHorizontal: 12,
borderRadius: 10,
- paddingTop: 10,
+ paddingTop: 10
}}>
<DialogHeader
title="Your data recovery key"
@@ -213,7 +206,7 @@ class RecoveryKeyDialog extends React.Component {
backgroundColor: colors.nav,
borderRadius: 5,
padding: 10,
- marginTop: 10,
+ marginTop: 10
}}>
<Paragraph
color={colors.icon}
@@ -224,7 +217,7 @@ class RecoveryKeyDialog extends React.Component {
maxWidth: '100%',
paddingRight: 10,
marginBottom: 10,
- textAlign: 'center',
+ textAlign: 'center'
}}>
{this.state.key}
</Paragraph>
@@ -235,7 +228,7 @@ class RecoveryKeyDialog extends React.Component {
ToastEvent.show({
heading: 'Recovery key copied!',
type: 'success',
- context: 'local',
+ context: 'local'
});
}}
icon="content-copy"
@@ -257,7 +250,7 @@ class RecoveryKeyDialog extends React.Component {
justifyContent: 'center',
position: 'absolute',
opacity: 0,
- zIndex: -1,
+ zIndex: -1
}}>
{this.state.key ? (
<QRCode
@@ -307,7 +300,7 @@ class RecoveryKeyDialog extends React.Component {
width: '100%',
maxWidth: '100%',
marginBottom: 5,
- textAlign: 'center',
+ textAlign: 'center'
}}>
Tap twice to confirm you have saved the recovery key.
</Paragraph>
diff --git a/apps/mobile/src/components/RestoreDialog/index.js b/apps/mobile/src/components/RestoreDialog/index.js
index 95be0f454..090618a4b 100644
--- a/apps/mobile/src/components/RestoreDialog/index.js
+++ b/apps/mobile/src/components/RestoreDialog/index.js
@@ -91,22 +91,15 @@ const RestoreDataComponent = ({close, setRestoring, restoring}) => {
if (restoring) {
return;
}
- if (Platform.OS === 'android') {
- let granted = storage.requestPermission();
- if (!granted) {
- ToastEvent.show({
- heading: 'Cannot restore backup',
- message: 'You must provide phone storage access to restore backups.',
- type: 'error',
- context: 'local'
- });
- return;
- }
- }
try {
setRestoring(true);
let prefix = Platform.OS === 'ios' ? '' : 'file:/';
- let backup = await RNFetchBlob.fs.readFile(prefix + item.path, 'utf8');
+ let backup;
+ if (Platform.OS === 'android') {
+ backup = await ScopedStorage.readFile(item.uri, 'utf8');
+ } else {
+ backup = await RNFetchBlob.fs.readFile(prefix + item.path, 'utf8');
+ }
await db.backup.import(backup);
setRestoring(false);
initialize();
diff --git a/apps/mobile/src/services/Backup.js b/apps/mobile/src/services/Backup.js
index 1be8dbc8d..53ed00d51 100644
--- a/apps/mobile/src/services/Backup.js
+++ b/apps/mobile/src/services/Backup.js
@@ -8,6 +8,7 @@ import Share from 'react-native-share';
import {Platform} from 'react-native';
import {sanitizeFilename} from '../utils/filename';
import * as ScopedStorage from 'react-native-scoped-storage';
+import {presentDialog} from '../components/Dialog/functions';
const MS_DAY = 86400000;
const MS_WEEK = MS_DAY * 7;
@@ -30,15 +31,30 @@ async function getDirectoryAndroid() {
async function checkBackupDirExists() {
if (Platform.OS === 'ios') return true;
- let dir;
- if (Platform.OS === 'android') {
- dir = await MMKV.getItem('backupStorageDir');
- if (dir) {
- dir = JSON.parse(dir);
- } else {
- dir = await getDirectoryAndroid();
- }
+ let dir = await MMKV.getItem('backupStorageDir');
+ if (dir) {
+ dir = JSON.parse(dir);
+ let allDirs = await ScopedStorage.getPersistedUriPermissions();
+ let exists = allDirs.findIndex(d => {
+ return d === dir.uri || dir.uri.includes(d);
+ });
+ exists = exists !== -1;
+ dir = exists ? dir : null;
}
+ if (!dir) {
+ dir = await new Promise(resolve => {
+ presentDialog({
+ title: 'Select backup folder',
+ paragraph:
+ 'Please select the folder where you would like to store backup files.',
+ positivePress: async () => {
+ resolve(await getDirectoryAndroid());
+ },
+ positiveText: 'Open file manager'
+ });
+ });
+ }
+
return dir;
}