From 7d89ec676ac648da7be1149ea6ef318805c2d061 Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Sat, 27 Jan 2024 22:18:30 +0500 Subject: [PATCH] mobile: fix attachments not loading when added from share ext --- .../common/filesystem/download-attachment.js | 11 +++--- .../app/components/attachments/actions.js | 19 ++++++---- .../app/components/attachments/index.js | 20 ++++++----- apps/mobile/app/screens/editor/index.tsx | 4 ++- apps/mobile/app/utils/note-bundle.js | 36 +++++++++++-------- 5 files changed, 54 insertions(+), 36 deletions(-) diff --git a/apps/mobile/app/common/filesystem/download-attachment.js b/apps/mobile/app/common/filesystem/download-attachment.js index 8a900f9de..b97e0468c 100644 --- a/apps/mobile/app/common/filesystem/download-attachment.js +++ b/apps/mobile/app/common/filesystem/download-attachment.js @@ -34,7 +34,8 @@ import { useAttachmentStore } from "../../stores/use-attachment-store"; import { db } from "../database"; import Storage from "../database/storage"; import { cacheDir, copyFileAsync, releasePermissions } from "./utils"; -import { createCacheDir } from "./io"; +import { createCacheDir, exists } from "./io"; +import { IOS_APPGROUPID } from "../../utils/constants"; export const FileDownloadStatus = { Success: 1, @@ -214,10 +215,9 @@ export default async function downloadAttachment( attachment.metadata.hash ); - if ( - !(await RNFetchBlob.fs.exists(`${cacheDir}/${attachment.metadata.hash}`)) - ) + if (!(await exists(attachment.metadata.hash))) { return; + } if (options.base64) { return await db.attachments.read(attachment.metadata.hash, "base64"); @@ -239,7 +239,8 @@ export default async function downloadAttachment( mime: attachment.metadata.type, fileName: options.cache ? undefined : filename, uri: options.cache ? undefined : folder.uri, - chunkSize: attachment.chunkSize + chunkSize: attachment.chunkSize, + appGroupId: IOS_APPGROUPID }; let fileUri = await Sodium.decryptFile( diff --git a/apps/mobile/app/components/attachments/actions.js b/apps/mobile/app/components/attachments/actions.js index 4eb194e49..820d480d6 100644 --- a/apps/mobile/app/components/attachments/actions.js +++ b/apps/mobile/app/components/attachments/actions.js @@ -48,7 +48,7 @@ import Heading from "../ui/typography/heading"; import Paragraph from "../ui/typography/paragraph"; import { formatBytes } from "@notesnook/common"; -const Actions = ({ attachment, setAttachments, fwdRef }) => { +const Actions = ({ attachment, setAttachments, fwdRef, close }) => { const { colors } = useThemeColors(); const contextId = attachment.metadata.hash; const [filename, setFilename] = useState(attachment.metadata.filename); @@ -117,7 +117,7 @@ const Actions = ({ attachment, setAttachments, fwdRef }) => { }); } - setAttachments([...db.attachments.all]); + setAttachments(); setLoading({ name: null }); @@ -140,7 +140,7 @@ const Actions = ({ attachment, setAttachments, fwdRef }) => { filename: value }); setFilename(value); - setAttachments([...db.attachments.all]); + setAttachments(); } }, positiveText: "Rename" @@ -152,8 +152,8 @@ const Actions = ({ attachment, setAttachments, fwdRef }) => { name: "Delete", onPress: async () => { await db.attachments.remove(attachment.metadata.hash, false); - setAttachments([...db.attachments.all]); - eSendEvent(eCloseSheet, contextId); + setAttachments(); + close(); }, icon: "delete-outline" } @@ -362,8 +362,13 @@ const Actions = ({ attachment, setAttachments, fwdRef }) => { Actions.present = (attachment, set, context) => { presentSheet({ context: context, - component: (ref) => ( - + component: (ref, close) => ( + ) }); }; diff --git a/apps/mobile/app/components/attachments/index.js b/apps/mobile/app/components/attachments/index.js index 634d48dbe..362f9147f 100644 --- a/apps/mobile/app/components/attachments/index.js +++ b/apps/mobile/app/components/attachments/index.js @@ -68,7 +68,7 @@ export const AttachmentDialog = ({ note }) => { !attachmentSearchValue.current || attachmentSearchValue.current === "" ) { - setAttachments([...attachments]); + setAttachments(filterAttachments(currentFilter)); } clearTimeout(searchTimer.current); searchTimer.current = setTimeout(() => { @@ -77,13 +77,16 @@ export const AttachmentDialog = ({ note }) => { attachmentSearchValue.current ); if (results.length === 0) return; - setAttachments(results); + + setAttachments(filterAttachments(currentFilter, results)); }, 300); }; const renderItem = ({ item }) => ( { + setAttachments(filterAttachments(currentFilter)); + }} attachment={item} context="attachments-list" /> @@ -133,11 +136,12 @@ export const AttachmentDialog = ({ note }) => { } ]; - const filterAttachments = (type) => { - const attachments = note - ? db.attachments.ofNote(note.id, "all") - : [...(db.attachments.all || [])]; - isDocument; + const filterAttachments = (type, _attachments) => { + const attachments = + _attachments || note + ? db.attachments.ofNote(note.id, "all") + : [...(db.attachments.all || [])]; + switch (type) { case "all": return attachments; diff --git a/apps/mobile/app/screens/editor/index.tsx b/apps/mobile/app/screens/editor/index.tsx index b0008a77f..dc6595697 100755 --- a/apps/mobile/app/screens/editor/index.tsx +++ b/apps/mobile/app/screens/editor/index.tsx @@ -45,6 +45,7 @@ import { EditorProps, useEditorType } from "./tiptap/types"; import { useEditor } from "./tiptap/use-editor"; import { useEditorEvents } from "./tiptap/use-editor-events"; import { editorController } from "./tiptap/utils"; +import { useThemeColors } from "@notesnook/theme"; const style: ViewStyle = { height: "100%", @@ -176,6 +177,7 @@ export default Editor; const ReadonlyButton = ({ editor }: { editor: useEditorType }) => { const readonly = useEditorStore((state) => state.readonly); const keyboard = useKeyboard(); + const { colors } = useThemeColors(); const onPress = async () => { if (editor.note.current) { @@ -191,7 +193,7 @@ const ReadonlyButton = ({ editor }: { editor: useEditorType }) => { name="pencil-lock" type="grayBg" onPress={onPress} - color="accent" + color={colors.primary.accent} customStyle={{ position: "absolute", bottom: 60, diff --git a/apps/mobile/app/utils/note-bundle.js b/apps/mobile/app/utils/note-bundle.js index 20b8ce414..9ec9ddce2 100644 --- a/apps/mobile/app/utils/note-bundle.js +++ b/apps/mobile/app/utils/note-bundle.js @@ -21,7 +21,7 @@ import Sodium from "@ammarahmed/react-native-sodium"; import { isImage } from "@notesnook/core/dist/utils/filename"; import { Platform } from "react-native"; import RNFetchBlob from "react-native-blob-util"; -import { db } from "../common/database"; +import { DatabaseLogger, db } from "../common/database"; import { IOS_APPGROUPID } from "./constants"; const santizeUri = (uri) => { @@ -57,6 +57,7 @@ export async function attachFile(uri, hash, type, filename, options) { return true; } catch (e) { if (Platform.OS === "ios") RNFetchBlob.fs.unlink(uri).catch(console.log); + DatabaseLogger.error(e, "Attach file error"); console.log("attach file error: ", e); return false; } @@ -107,26 +108,31 @@ async function createNotes(bundle) { uri: uri, type: "cache" }); - await attachFile(uri, hash, file.type, file.name, { + const attached = await attachFile(uri, hash, file.type, file.name, { type: "cache", id: id, appGroupId: IOS_APPGROUPID }); let content = ``; - if (isImage(file.type)) { - content = ``; - } else { - content = `

`; + + if (attached) { + if (isImage(file.type)) { + content = ``; + } else { + content = `

`; + } + const rawContent = await db.content.raw( + db.notes.note(id).data?.contentId + ); + await db.notes.add({ + id: id, + content: { + type: "tiptap", + data: rawContent?.data ? rawContent?.data + content : content + }, + sessionId: sessionId + }); } - const rawContent = await db.content.raw(db.notes.note(id).data?.contentId); - await db.notes.add({ - id: id, - content: { - type: "tiptap", - data: rawContent?.data ? rawContent?.data + content : content - }, - sessionId: sessionId - }); } }