From 1043b7df556f46140a7c6e4cce242e59ed3aa2fb Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Fri, 19 May 2023 16:55:06 +0500 Subject: [PATCH] mobile: improved attachments manager --- .../app/components/attachments/index.js | 120 ++++++++++++++++-- 1 file changed, 108 insertions(+), 12 deletions(-) diff --git a/apps/mobile/app/components/attachments/index.js b/apps/mobile/app/components/attachments/index.js index 8f14b66a7..d85972f12 100644 --- a/apps/mobile/app/components/attachments/index.js +++ b/apps/mobile/app/components/attachments/index.js @@ -18,7 +18,7 @@ along with this program. If not, see . */ import React, { useRef, useState } from "react"; -import { ActivityIndicator, View } from "react-native"; +import { ActivityIndicator, ScrollView, View } from "react-native"; import { FlatList } from "react-native-actions-sheet"; import Icon from "react-native-vector-icons/MaterialCommunityIcons"; import { db } from "../../common/database"; @@ -34,30 +34,40 @@ import Heading from "../ui/typography/heading"; import Paragraph from "../ui/typography/paragraph"; import { AttachmentItem } from "./attachment-item"; import DownloadAttachments from "./download-attachments"; +import { Button } from "../ui/button"; + +const documentMimeTypes = ["application/pdf"]; export const AttachmentDialog = ({ note }) => { const colors = useThemeStore((state) => state.colors); + const [attachments, setAttachments] = useState( note ? db.attachments.ofNote(note.id, "all") : [...(db.attachments.all || [])] ); + const attachmentSearchValue = useRef(); const searchTimer = useRef(); const [loading, setLoading] = useState(false); + const [currentFilter, setCurrentFilter] = useState("all"); const onChangeText = (text) => { + const attachments = note + ? db.attachments.ofNote(note.id, "all") + : [...(db.attachments.all || [])]; + attachmentSearchValue.current = text; if ( !attachmentSearchValue.current || attachmentSearchValue.current === "" ) { - setAttachments([...db.attachments.all]); + setAttachments([...attachments]); } clearTimeout(searchTimer.current); searchTimer.current = setTimeout(() => { let results = db.lookup.attachments( - db.attachments.all, + attachments, attachmentSearchValue.current ); if (results.length === 0) return; @@ -90,6 +100,57 @@ export const AttachmentDialog = ({ note }) => { setLoading(false); }; + const attachmentTypes = [ + { + title: "All", + filterBy: "all" + }, + { + title: "Images", + filterBy: "images" + }, + { + title: "Documents", + filterBy: "documents" + }, + { + title: "Video", + filterBy: "video" + }, + { + title: "Audio", + filterBy: "audio" + } + ]; + + const filterAttachments = (type) => { + const attachments = note + ? db.attachments.ofNote(note.id, "all") + : [...(db.attachments.all || [])]; + + switch (type) { + case "all": + return attachments; + case "images": + return attachments.filter((attachment) => + attachment.metadata.type.startsWith("image/") + ); + case "video": + return attachments.filter((attachment) => + attachment.metadata.type.startsWith("video/") + ); + case "audio": + return attachments.filter((attachment) => + attachment.metadata.type.startsWith("audio/") + ); + case "documents": + return attachments.filter( + (attachment) => + documentMimeTypes.indexOf(attachment.metadata.type) > -1 + ); + } + }; + return ( { - {!note ? ( - { - onChangeText(attachmentSearchValue.current); - }} - /> - ) : null} + { + onChangeText(attachmentSearchValue.current); + }} + /> { maxToRenderPerBatch={10} initialNumToRender={10} windowSize={5} + ListHeaderComponent={ + + {attachmentTypes.map((item) => ( +