Skip to content

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

mobile: add support for progress

ammarahm-ede62267042ead
diff --git a/apps/mobile/app/common/filesystem/download.js b/apps/mobile/app/common/filesystem/download.js
index 60138f431..4821e755c 100644
--- a/apps/mobile/app/common/filesystem/download.js
+++ b/apps/mobile/app/common/filesystem/download.js
@@ -37,6 +37,7 @@ export async function downloadFile(filename, data, cancelToken) {
try {
let exists = await RNFetchBlob.fs.exists(path);
if (exists) {
+ console.log("exists", filename);
return true;
}
@@ -74,22 +75,24 @@ export async function downloadFile(filename, data, cancelToken) {
useAttachmentStore.getState().remove(filename);
return status >= 200 && status < 300;
} catch (e) {
- ToastEvent.show({
- heading: "Error downloading file",
- message: e.message,
- type: "error",
- context: "global"
- });
- ToastEvent.show({
- heading: "Error downloading file",
- message: e.message,
- type: "error",
- context: "local"
- });
+ if (e.message !== "canceled") {
+ ToastEvent.show({
+ heading: "Error downloading file",
+ message: e.message,
+ type: "error",
+ context: "global"
+ });
+ ToastEvent.show({
+ heading: "Error downloading file",
+ message: e.message,
+ type: "error",
+ context: "local"
+ });
+ }
useAttachmentStore.getState().remove(filename);
RNFetchBlob.fs.unlink(path).catch(console.log);
- console.log("download file error: ", e, url, headers);
+ console.log("Download file error:", e, url, headers);
return false;
}
}
diff --git a/apps/mobile/app/common/filesystem/io.js b/apps/mobile/app/common/filesystem/io.js
index c1db4d66a..dbaa0a681 100644
--- a/apps/mobile/app/common/filesystem/io.js
+++ b/apps/mobile/app/common/filesystem/io.js
@@ -29,6 +29,7 @@ export async function readEncrypted(filename, key, cipherData) {
await migrateFilesFromCache();
console.log("read encrypted file...");
let path = `${cacheDir}/${filename}`;
+
try {
const iosAppGroup =
Platform.OS === "ios"
@@ -41,6 +42,7 @@ export async function readEncrypted(filename, key, cipherData) {
(Platform.OS === "ios" && (await RNFetchBlob.fs.exists(appGroupPath)));
if (!exists) {
+ console.log("Will download file...", filename);
return false;
} else {
RNFetchBlob.fs.stat(path).then((r) => {
diff --git a/apps/mobile/app/components/attachments/attachment-item.js b/apps/mobile/app/components/attachments/attachment-item.js
index ad24b8d47..0433c0cfe 100644
--- a/apps/mobile/app/components/attachments/attachment-item.js
+++ b/apps/mobile/app/components/attachments/attachment-item.js
@@ -132,7 +132,7 @@ export const AttachmentItem = ({
activeOpacity={0.9}
onPress={() => {
if (encryption || !pressable) return;
- db.fs.cancel(attachment.metadata.hash);
+ db.fs.cancel(attachment.metadata.hash, currentProgress.type);
setCurrentProgress(null);
}}
style={{
diff --git a/apps/mobile/app/components/sheets/publish-note/index.js b/apps/mobile/app/components/sheets/publish-note/index.js
index ac16509ce..083358bda 100644
--- a/apps/mobile/app/components/sheets/publish-note/index.js
+++ b/apps/mobile/app/components/sheets/publish-note/index.js
@@ -40,7 +40,9 @@ import { requestInAppReview } from "../../../services/app-review";
const PublishNoteSheet = ({ note: item, update }) => {
const { colors } = useThemeColors();
const actionSheetRef = useRef();
- const loading = useAttachmentStore((state) => state.loading);
+
+ const attachmentDownloads = useAttachmentStore((state) => state.downloading);
+ const downloading = attachmentDownloads[`monograph-${item.id}`];
const [selfDestruct, setSelfDestruct] = useState(false);
const [isLocked, setIsLocked] = useState(false);
const [note, setNote] = useState(item);
@@ -137,9 +139,9 @@ const PublishNoteSheet = ({ note: item, update }) => {
}}
>
Please wait...
- {loading && loading.current && loading.total
+ {downloading && downloading.current && downloading.total
? `\nDownloading attachments (${
- loading?.current / loading?.total
+ downloading?.current / downloading?.total
})`
: ""}
</Paragraph>
diff --git a/apps/mobile/app/hooks/use-app-events.js b/apps/mobile/app/hooks/use-app-events.js
index 5c08e41b2..3de712898 100644
--- a/apps/mobile/app/hooks/use-app-events.js
+++ b/apps/mobile/app/hooks/use-app-events.js
@@ -83,7 +83,6 @@ import {
import { getGithubVersion } from "../utils/github-version";
import { tabBarRef } from "../utils/global-refs";
import { sleep } from "../utils/time";
-import { useThemeColors } from "@notesnook/theme";
const onCheckSyncStatus = async (type) => {
const { disableSync, disableAutoSync } = SettingsService.get();
@@ -107,10 +106,20 @@ const onFileEncryptionProgress = ({ total, progress }) => {
.setEncryptionProgress((progress / total).toFixed(2));
};
-const onLoadingAttachmentProgress = (data) => {
- useAttachmentStore
- .getState()
- .setLoading(data.total === data.current ? null : data);
+const onDownloadingAttachmentProgress = (data) => {
+ useAttachmentStore.getState().setDownloading(data);
+};
+
+const onUploadingAttachmentProgress = (data) => {
+ useAttachmentStore.getState().setUploading(data);
+};
+
+const onDownloadedAttachmentProgress = (data) => {
+ useAttachmentStore.getState().setDownloading(data);
+};
+
+const onUploadedAttachmentProgress = (data) => {
+ useAttachmentStore.getState().setUploading(data);
};
const onUserSessionExpired = async () => {
@@ -301,7 +310,16 @@ export const useAppEvents = () => {
EVENTS.userSubscriptionUpdated,
onUserSubscriptionStatusChanged
),
- EV.subscribe(EVENTS.attachmentsLoading, onLoadingAttachmentProgress),
+ EV.subscribe(EVENTS.fileDownload, onDownloadingAttachmentProgress),
+ EV.subscribe(EVENTS.fileUpload, onUploadingAttachmentProgress),
+ EV.subscribe(EVENTS.fileDownloaded, onDownloadedAttachmentProgress),
+ EV.subscribe(EVENTS.fileUploaded, onUploadedAttachmentProgress),
+ EV.subscribe(EVENTS.downloadCanceled, (data) => {
+ useAttachmentStore.getState().setDownloading(data);
+ }),
+ EV.subscribe(EVENTS.uploadCanceled, (data) => {
+ useAttachmentStore.getState().setUploading(data);
+ }),
eSubscribeEvent(eUserLoggedIn, onUserUpdated)
];
diff --git a/apps/mobile/app/screens/editor/progress.js b/apps/mobile/app/screens/editor/progress.js
deleted file mode 100644
index c533bbdf2..000000000
--- a/apps/mobile/app/screens/editor/progress.js
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
-This file is part of the Notesnook project (https://notesnook.com/)
-
-Copyright (C) 2023 Streetwriters (Private) Limited
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-import React, { useEffect, useRef, useState } from "react";
-import { View } from "react-native";
-import { ProgressBarComponent } from "../../components/ui/svg/lazy";
-import useGlobalSafeAreaInsets from "../../hooks/use-global-safe-area-insets";
-import { useAttachmentStore } from "../../stores/use-attachment-store";
-import { useThemeColors } from "@notesnook/theme";
-import { SIZE } from "../../utils/size";
-export const ProgressBar = () => {
- const { colors } = useThemeColors();
- const loading = useAttachmentStore((state) => state.loading);
- const [prog, setProg] = useState(0);
- const [visible, setVisible] = useState(false);
- const timer = useRef();
- const insets = useGlobalSafeAreaInsets();
- const [width, setWidth] = useState(false);
-
- useEffect(() => {
- if (loading) {
- if (loading.current !== loading.total) {
- setVisible(true);
- setProg(loading.current / loading.total);
- } else {
- clear();
- }
- } else {
- clear();
- }
- }, [loading]);
-
- const clear = () => {
- clearTimeout(timer.current);
- timer.current = null;
- timer.current = setTimeout(() => {
- setProg(1);
- setTimeout(() => {
- setVisible(false);
- }, 1000);
- }, 100);
- };
-
- return visible ? (
- <View
- style={{
- justifyContent: "center",
- position: "absolute",
- zIndex: 1,
- marginTop: insets.top + 45,
- width: "100%"
- }}
- onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
- >
- <ProgressBarComponent
- size={SIZE.xxl}
- progress={prog}
- color={colors.primary.accent}
- borderWidth={0}
- height={1}
- width={width || 400}
- />
- </View>
- ) : null;
-};
diff --git a/apps/mobile/app/screens/editor/progress.tsx b/apps/mobile/app/screens/editor/progress.tsx
new file mode 100644
index 000000000..c235908df
--- /dev/null
+++ b/apps/mobile/app/screens/editor/progress.tsx
@@ -0,0 +1,135 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { useThemeColors } from "@notesnook/theme";
+import React, { useEffect, useRef, useState } from "react";
+import { View } from "react-native";
+import { ProgressBarComponent } from "../../components/ui/svg/lazy";
+import useGlobalSafeAreaInsets from "../../hooks/use-global-safe-area-insets";
+import { useAttachmentStore } from "../../stores/use-attachment-store";
+import { useEditorStore } from "../../stores/use-editor-store";
+
+export const ProgressBar = () => {
+ const { colors } = useThemeColors();
+ const currentlyEditingNote = useEditorStore(
+ (state) => state.currentEditingNote
+ );
+ const downloading = useAttachmentStore((state) => state.downloading);
+
+ const loading = currentlyEditingNote
+ ? downloading?.[currentlyEditingNote]
+ : undefined;
+
+ const attachmentProgress = useAttachmentStore((state) => state.progress);
+ const [progress, setProgress] = useState(0);
+ const [visible, setVisible] = useState(false);
+ const timer = useRef<NodeJS.Timeout>();
+ const insets = useGlobalSafeAreaInsets();
+ const [width, setWidth] = useState(400);
+
+ const groupProgressInfo = useRef<{
+ [name: string]: {
+ total: number;
+ current: number;
+ };
+ }>({});
+
+ const currentItemProgress = loading?.filename
+ ? attachmentProgress?.[loading?.filename]
+ : undefined;
+
+ console.log(loading);
+
+ useEffect(() => {
+ if (loading) {
+ if (loading.current === loading.total && loading.success) {
+ console.log("download completed");
+ clear();
+ return;
+ }
+ setVisible(true);
+ if (!loading.filename) return;
+
+ if (!groupProgressInfo.current[loading.filename]) {
+ groupProgressInfo.current[loading.filename] = {
+ total: 1,
+ current: 0
+ };
+ }
+
+ const itemProgressCurrent = groupProgressInfo.current[loading.filename];
+
+ const itemTotalSize =
+ currentItemProgress?.total || itemProgressCurrent.total;
+
+ const itemCurrentProgress =
+ currentItemProgress?.recieved ||
+ currentItemProgress?.sent ||
+ itemProgressCurrent.current;
+
+ groupProgressInfo.current[loading.filename] = {
+ current: itemCurrentProgress,
+ total: itemTotalSize
+ };
+
+ const itemProgressPercent = itemCurrentProgress / itemTotalSize;
+
+ setProgress(
+ ((loading.current || 0) + itemProgressPercent) / (loading.total || 1)
+ );
+ } else {
+ clear();
+ }
+ }, [currentItemProgress, loading]);
+
+ const clear = () => {
+ clearTimeout(timer.current);
+ timer.current = undefined;
+ timer.current = setTimeout(() => {
+ setProgress(1);
+ setTimeout(() => {
+ setVisible(false);
+ setProgress(0);
+ groupProgressInfo.current = {};
+ }, 1000);
+ }, 0);
+ };
+
+ return (
+ <View
+ style={{
+ justifyContent: "center",
+ position: "absolute",
+ zIndex: visible ? 1 : -1,
+ marginTop: insets.top + 45,
+ width: "100%"
+ }}
+ onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
+ >
+ <ProgressBarComponent
+ progress={progress}
+ color={colors.primary.accent}
+ borderWidth={0}
+ borderRadius={0}
+ height={2}
+ width={width || 400}
+ />
+ </View>
+ );
+};
diff --git a/apps/mobile/app/screens/editor/tiptap/use-editor.ts b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
index dea3f5368..17658a110 100644
--- a/apps/mobile/app/screens/editor/tiptap/use-editor.ts
+++ b/apps/mobile/app/screens/editor/tiptap/use-editor.ts
@@ -134,7 +134,8 @@ export const useEditor = (
const reset = useCallback(
async (resetState = true, resetContent = true) => {
- currentNote.current?.id && db.fs?.cancel(currentNote.current.id, null);
+ currentNote.current?.id &&
+ db.fs?.cancel(currentNote.current.id, "download");
currentNote.current = null;
loadedImages.current = {};
currentContent.current = null;
diff --git a/apps/mobile/app/stores/use-attachment-store.ts b/apps/mobile/app/stores/use-attachment-store.ts
index b4efa5767..604498261 100644
--- a/apps/mobile/app/stores/use-attachment-store.ts
+++ b/apps/mobile/app/stores/use-attachment-store.ts
@@ -20,6 +20,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import create from "zustand";
import { editorController } from "../screens/editor/tiptap/utils";
+export type AttachmentGroupProgress = {
+ total: number;
+ current: number;
+ groupId: string;
+ filename: string;
+ canceled?: boolean;
+ success?: boolean;
+};
+
interface AttachmentStore {
progress?: {
[name: string]: {
@@ -30,6 +39,7 @@ interface AttachmentStore {
type: "upload" | "download";
} | null;
};
+
encryptionProgress: number;
setEncryptionProgress: (encryptionProgress: number) => void;
remove: (hash: string) => void;
@@ -40,8 +50,14 @@ interface AttachmentStore {
recieved: number,
type: "upload" | "download"
) => void;
- loading: { total: number; current: number };
- setLoading: (data: { total: number; current: number }) => void;
+ downloading?: {
+ [groupId: string]: AttachmentGroupProgress | undefined;
+ };
+ setDownloading: (data: AttachmentGroupProgress) => void;
+ uploading?: {
+ [groupId: string]: AttachmentGroupProgress | undefined;
+ };
+ setUploading: (data: AttachmentGroupProgress) => void;
}
export const useAttachmentStore = create<AttachmentStore>((set, get) => ({
@@ -73,6 +89,21 @@ export const useAttachmentStore = create<AttachmentStore>((set, get) => ({
encryptionProgress: 0,
setEncryptionProgress: (encryptionProgress) =>
set({ encryptionProgress: encryptionProgress }),
- loading: { total: 0, current: 0 },
- setLoading: (data) => set({ loading: data ? { ...data } : data })
+
+ downloading: {},
+ setDownloading: (data) =>
+ set({
+ downloading: {
+ ...get().downloading,
+ [data.groupId]: data?.canceled ? undefined : data
+ }
+ }),
+ uploading: {},
+ setUploading: (data) =>
+ set({
+ uploading: {
+ ...get().uploading,
+ [data.groupId]: data?.canceled ? undefined : data
+ }
+ })
}));