Skip to content

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

show attachment progress

ammarahm-edb920c49ea4d4
diff --git a/apps/mobile/src/components/AttachmentDialog/index.js b/apps/mobile/src/components/AttachmentDialog/index.js
index 3a473fb05..0091d6ad6 100644
--- a/apps/mobile/src/components/AttachmentDialog/index.js
+++ b/apps/mobile/src/components/AttachmentDialog/index.js
@@ -1,26 +1,35 @@
-import React, { useEffect, useRef, useState } from 'react';
-import { TouchableOpacity, View } from 'react-native';
-import { FlatList } from 'react-native-gesture-handler';
+import React, {useEffect, useRef, useState} from 'react';
+import {Platform, TouchableOpacity, View} from 'react-native';
+import {FlatList} from 'react-native-gesture-handler';
import * as Progress from 'react-native-progress';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
-import { useTracked } from '../../provider';
-import { useAttachmentStore } from '../../provider/stores';
+import {useTracked} from '../../provider';
+import {useAttachmentStore} from '../../provider/stores';
import {
+ eSendEvent,
eSubscribeEvent,
eUnSubscribeEvent
} from '../../services/EventManager';
-import { db } from '../../utils/database';
+import {db} from '../../utils/database';
import {
eCloseAttachmentDialog,
- eOpenAttachmentsDialog
+ eOpenAttachmentsDialog,
+ eOpenProgressDialog
} from '../../utils/Events';
import filesystem from '../../utils/filesystem';
-import { SIZE } from '../../utils/SizeUtils';
-import { ActionIcon } from '../ActionIcon';
+import {SIZE} from '../../utils/SizeUtils';
+import {ActionIcon} from '../ActionIcon';
import ActionSheetWrapper from '../ActionSheetComponent/ActionSheetWrapper';
import DialogHeader from '../Dialog/dialog-header';
+import {presentDialog} from '../Dialog/functions';
import Paragraph from '../Typography/Paragraph';
-
+import FileViewer from 'react-native-file-viewer';
+import {ToastEvent} from '../../services/EventManager';
+import RNFetchBlob from 'rn-fetch-blob';
+import {Button} from '../Button';
+import Share from 'react-native-share';
+import ProgressDialog from '../ProgressDialog';
+import {ShareComponent} from '../ExportDialog/share';
export const AttachmentDialog = () => {
const [state] = useTracked();
const colors = state.colors;
@@ -41,7 +50,7 @@ export const AttachmentDialog = () => {
const open = item => {
setNote(item);
setVisible(true);
- let _attachments = db.attachments.ofNote(item.id,"files");
+ let _attachments = db.attachments.ofNote(item.id, 'files');
setAttachments(_attachments);
};
@@ -128,14 +137,20 @@ function getFileExtension(filename) {
return ext == null ? '' : ext[1];
}
-export const Attachment = ({attachment,encryption}) => {
+export const Attachment = ({attachment, encryption}) => {
const [state] = useTracked();
const colors = state.colors;
const progress = useAttachmentStore(state => state.progress);
- const [currentProgress, setCurrentProgress] = useState(encryption ? {
- type:"encrypt"
- }: null);
- const encryptionProgress = encryption ? useAttachmentStore(state => state.encryptionProgress) : null
+ const [currentProgress, setCurrentProgress] = useState(
+ encryption
+ ? {
+ type: 'encrypt'
+ }
+ : null
+ );
+ const encryptionProgress = encryption
+ ? useAttachmentStore(state => state.encryptionProgress)
+ : null;
const onPress = async () => {
if (currentProgress) {
@@ -143,7 +158,7 @@ export const Attachment = ({attachment,encryption}) => {
useAttachmentStore.getState().remove(attachment.metadata.hash);
return;
}
- filesystem.downloadAttachment(attachment.metadata.hash);
+ filesystem.downloadAttachment(attachment.metadata.hash,false);
};
useEffect(() => {
@@ -177,6 +192,7 @@ export const Attachment = ({attachment,encryption}) => {
backgroundColor: colors.nav
}}
type="grayBg">
+ <ProgressDialog context={attachment.metadata.hash} />
<View
style={{
flexShrink: 1,
@@ -221,7 +237,9 @@ export const Attachment = ({attachment,encryption}) => {
<Paragraph color={colors.icon} size={SIZE.xs}>
{formatBytes(attachment.length)}{' '}
- {currentProgress?.type ? '(' + currentProgress.type + 'ing - tap to cancel)' : ''}
+ {currentProgress?.type
+ ? '(' + currentProgress.type + 'ing - tap to cancel)'
+ : ''}
</Paragraph>
</View>
</View>
@@ -237,12 +255,18 @@ export const Attachment = ({attachment,encryption}) => {
style={{
justifyContent: 'center',
marginLeft: 5,
- marginTop:5,
- marginRight:-5
+ marginTop: 5,
+ marginRight: -5
}}>
<Progress.Circle
size={SIZE.xxl}
- progress={encryptionProgress ? encryptionProgress : currentProgress?.value ? currentProgress?.value / 100 : 0}
+ progress={
+ encryptionProgress
+ ? encryptionProgress
+ : currentProgress?.value
+ ? currentProgress?.value / 100
+ : 0
+ }
showsText
textStyle={{
fontSize: 10
diff --git a/apps/mobile/src/components/ExportDialog/share.js b/apps/mobile/src/components/ExportDialog/share.js
new file mode 100644
index 000000000..406ecba13
--- /dev/null
+++ b/apps/mobile/src/components/ExportDialog/share.js
@@ -0,0 +1,52 @@
+import React from 'react';
+import {View} from 'react-native';
+import FileViewer from 'react-native-file-viewer';
+import Share from 'react-native-share';
+import {ToastEvent} from '../../services/EventManager';
+import {SIZE} from '../../utils/SizeUtils';
+import {Button} from '../Button';
+
+export const ShareComponent = ({uri, name, padding}) => {
+ return (
+ <View
+ style={{
+ paddingHorizontal: padding
+ }}>
+ <Button
+ title="Open"
+ type="accent"
+ width="100%"
+ fontSize={SIZE.md}
+ onPress={async () => {
+ FileViewer.open(uri, {
+ showOpenWithDialog: true,
+ showAppsSuggestions: true
+ }).catch(e => {
+ ToastEvent.show({
+ heading: 'Cannot open',
+ message: `No application found to open ${name} file.`,
+ type: 'success',
+ context: 'local'
+ });
+ });
+ }}
+ height={50}
+ />
+ <Button
+ title="Share"
+ type="shade"
+ width="100%"
+ fontSize={SIZE.md}
+ style={{
+ marginTop: 10
+ }}
+ onPress={async () => {
+ Share.open({
+ url: uri
+ }).catch(console.log);
+ }}
+ height={50}
+ />
+ </View>
+ );
+};
diff --git a/apps/mobile/src/components/ProgressDialog/index.js b/apps/mobile/src/components/ProgressDialog/index.js
index 4167c4a30..1a6e6e841 100644
--- a/apps/mobile/src/components/ProgressDialog/index.js
+++ b/apps/mobile/src/components/ProgressDialog/index.js
@@ -1,19 +1,17 @@
-import React, {useEffect, useRef, useState} from 'react';
-import {ActivityIndicator, View} from 'react-native';
+import React, { useEffect, useRef, useState } from 'react';
+import { ActivityIndicator, View } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
-import {useTracked} from '../../provider';
-import {eSubscribeEvent, eUnSubscribeEvent} from '../../services/EventManager';
-import {eCloseProgressDialog, eOpenProgressDialog} from '../../utils/Events';
-import {SIZE} from '../../utils/SizeUtils';
-import {sleep} from '../../utils/TimeUtils';
+import { useTracked } from '../../provider';
+import { eSubscribeEvent, eUnSubscribeEvent } from '../../services/EventManager';
+import { eCloseProgressDialog, eOpenProgressDialog } from '../../utils/Events';
+import { SIZE } from '../../utils/SizeUtils';
+import { sleep } from '../../utils/TimeUtils';
import ActionSheetWrapper from '../ActionSheetComponent/ActionSheetWrapper';
-import {Button} from '../Button';
-import Seperator from '../Seperator';
-import {Toast} from '../Toast';
+import { Button } from '../Button';
import Heading from '../Typography/Heading';
import Paragraph from '../Typography/Paragraph';
-const ProgressDialog = () => {
+const ProgressDialog = ({context}) => {
const [state] = useTracked();
const {colors} = state;
const [visible, setVisible] = useState(false);
@@ -32,6 +30,9 @@ const ProgressDialog = () => {
}, []);
const open = async data => {
+ if ((data.context && !context) || (data.context && data.context !== context)) {
+ return;
+ }
setDialogData(data);
setVisible(true);
await sleep(1);
@@ -81,7 +82,7 @@ const ProgressDialog = () => {
</Paragraph>
) : null}
</Paragraph>
- ) : null}
+ ) : null}
</View>
{dialogData.component}
diff --git a/apps/mobile/src/utils/filesystem.js b/apps/mobile/src/utils/filesystem.js
index a8601f72d..39cd1bd05 100644
--- a/apps/mobile/src/utils/filesystem.js
+++ b/apps/mobile/src/utils/filesystem.js
@@ -1,10 +1,14 @@
+import React from 'react';
import Sodium from 'react-native-sodium';
import RNFetchBlob from 'rn-fetch-blob';
import {useAttachmentStore} from '../provider/stores';
-import {ToastEvent} from '../services/EventManager';
+import {eSendEvent, ToastEvent} from '../services/EventManager';
import {db} from './database';
import Storage from './storage';
import * as ScopedStorage from 'react-native-scoped-storage';
+import {eOpenProgressDialog} from './Events';
+import {ShareComponent} from '../components/ExportDialog/share';
+import {Platform} from 'react-native';
const cacheDir = RNFetchBlob.fs.dirs.CacheDir;
@@ -28,7 +32,7 @@ async function readEncrypted(filename, key, cipherData) {
return output;
} catch (e) {
console.log(e);
- console.log('error')
+ console.log('error');
return false;
}
}
@@ -51,20 +55,20 @@ async function uploadFile(filename, {url, headers}, cancelToken) {
console.log('uploading file: ', filename, headers);
try {
- let res = await fetch(url,{
- method:"PUT",
+ let res = await fetch(url, {
+ method: 'PUT',
headers
});
- const uploadUrl =await res.text();
+ const uploadUrl = await res.text();
console.log(uploadUrl);
let request = RNFetchBlob.config({
- IOSBackgroundTask: true,
+ IOSBackgroundTask: true
})
.fetch(
'PUT',
uploadUrl,
{
- 'content-type': '',
+ 'content-type': ''
},
RNFetchBlob.wrap(`${cacheDir}/${filename}`)
)
@@ -97,7 +101,7 @@ async function downloadFile(filename, {url, headers}, cancelToken) {
path: path,
IOSBackgroundTask: true
})
- .fetch('GET', url, headers)
+ .fetch('GET', url, __DEV__ ? null : headers)
.progress((recieved, total) => {
useAttachmentStore
.getState()
@@ -148,7 +152,7 @@ function cancelable(operation) {
};
}
-async function downloadAttachment(hash) {
+async function downloadAttachment(hash, global = true) {
let attachment = db.attachments.attachment(hash);
if (!attachment) {
console.log('attachment not found');
@@ -159,7 +163,7 @@ async function downloadAttachment(hash) {
folder = await ScopedStorage.openDocumentTree(false);
if (!folder) return;
} else {
- folder.uri = await Storage.checkAndCreateDir('/Downloads/');
+ folder.uri = await Storage.checkAndCreateDir('/downloads/');
}
try {
@@ -183,12 +187,40 @@ async function downloadAttachment(hash) {
fileName: attachment.metadata.filename,
uri: folder.uri
};
- await Sodium.decryptFile(key, info, false);
+ let fileUri = await Sodium.decryptFile(key, info, false);
ToastEvent.show({
heading: 'Download successful',
message: attachment.metadata.filename + ' downloaded',
- type: 'success',
+ type: 'success'
+ });
+ RNFetchBlob.fs.unlink(
+ RNFetchBlob.fs.dirs.CacheDir + `/${attachment.metadata.hash}`
+ );
+
+ if (Platform.OS === 'ios') {
+ fileUri = file.uri + '/' + attachment.hash.filename;
+ }
+ console.log('saved file uri: ',fileUri)
+
+ eSendEvent(eOpenProgressDialog, {
+ title: `File downloaded`,
+ paragraph: `${attachment.metadata.filename} is saved to ${
+ Platform.OS === 'android'
+ ? 'the selected path'
+ : 'File Manager/Notesnook/downloads'
+ }`,
+ noProgress: true,
+ icon:"download",
+ context: global ? null : attachment.metadata.hash,
+ component: (
+ <ShareComponent
+ uri={fileUri}
+ name={attachment.metadata.filename}
+ padding={12}
+ />
+ )
});
+ return fileUri;
} catch (e) {
console.log('download attachment error: ', e);
useAttachmentStore.getState().remove(attachment.metadata.hash);
diff --git a/apps/mobile/src/views/Editor/Functions.js b/apps/mobile/src/views/Editor/Functions.js
index 83ac83562..55814b0ef 100644
--- a/apps/mobile/src/views/Editor/Functions.js
+++ b/apps/mobile/src/views/Editor/Functions.js
@@ -319,7 +319,7 @@ export const _onMessage = async evt => {
eSendEvent('editorScroll', message);
break;
case 'attachment_download':
- filesystem.downloadAttachment(message.value);
+ filesystem.downloadAttachment(message.value,true);
break;
case 'noteLoaded':
tiny.call(EditorWebView, tiny.notLoading);