From a5e1c3fb4d42ba344a68ad4cdfa65a07fba1b065 Mon Sep 17 00:00:00 2001 From: Miloua Mokhtar Date: Sat, 5 Oct 2024 20:10:32 +0100 Subject: [PATCH] feat: added the possiblity to select multiple text items --- mobile-app/App.tsx | 190 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 149 insertions(+), 41 deletions(-) diff --git a/mobile-app/App.tsx b/mobile-app/App.tsx index e9d09cd..ce4dd00 100644 --- a/mobile-app/App.tsx +++ b/mobile-app/App.tsx @@ -116,6 +116,7 @@ export default function App() { const responseListener = useRef(); const [appStateVisible, setAppStateVisible] = useState(appState.current); const [loading, setLoading] = useState(false); + const [selectedItems, setSelectedItems] = useState>(new Set()); useEffect(() => { async function getAddress() { @@ -376,9 +377,7 @@ export default function App() { const deleteDatabase = () => { db.transaction((tx) => { // Drop the clips table if it exists - tx.executeSql("DROP TABLE IF EXISTS clips", [], () => { - console.log("Table dropped successfully"); - }); + tx.executeSql("DROP TABLE IF EXISTS clips", [], () => {}); }); // Recreate the clips table @@ -386,9 +385,6 @@ export default function App() { tx.executeSql( "CREATE TABLE IF NOT EXISTS clips (id INTEGER PRIMARY KEY AUTOINCREMENT, clip TEXT)", [], - () => { - console.log("Table created successfully"); - }, ); }); @@ -411,12 +407,12 @@ export default function App() { } const resetDatabase = () => { - deleteAllClipsDb(); + if (connection) { + deleteAllClipsDb(); + } db.transaction((tx) => { // Drop the clips table if it exists - tx.executeSql("DROP TABLE IF EXISTS clips", [], () => { - console.log("Table dropped successfully"); - }); + tx.executeSql("DROP TABLE IF EXISTS clips", [], () => {}); }); // Recreate the clips table @@ -424,9 +420,6 @@ export default function App() { tx.executeSql( "CREATE TABLE IF NOT EXISTS clips (id INTEGER PRIMARY KEY AUTOINCREMENT, clip TEXT)", [], - () => { - console.log("Table created successfully"); - }, ); }); @@ -454,7 +447,6 @@ export default function App() { }; const addClip = () => { - console.log(currentVal); if (currentVal === undefined || !currentVal?.trim()) { return ToastAndroid.show("Your input is empty", ToastAndroid.CENTER); } @@ -486,6 +478,26 @@ export default function App() { } }; + const editClip = (id: number, newText: string) => { + db.transaction((tx) => { + tx.executeSql( + "UPDATE clips SET clips_text = ? WHERE id = ?", + [newText, id], + (txObj, resultSet) => { + if (resultSet.rowsAffected > 0) { + let updatedClips = [...val].map((clip) => { + if (clip.id === id) { + return { ...clip, clips_text: newText }; + } + return clip; + }); + setVal(updatedClips); + } + }, + ); + }); + }; + const deleteClip = (id: number) => { db.transaction((tx) => { tx.executeSql( @@ -528,6 +540,47 @@ export default function App() { } } + const toggleSelection = (id: number) => { + setSelectedItems((prev) => { + const newSelection = new Set(prev); + if (newSelection.has(id)) { + newSelection.delete(id); + } else { + newSelection.add(id); + } + return newSelection; + }); + }; + + const toggleSelectAll = () => { + if (selectedItems.size === reversedClipsDb.length) { + setSelectedItems(new Set()); + } else { + const allClipIds = reversedClipsDb.map((clip) => clip.id); + setSelectedItems(new Set(allClipIds)); + } + }; + + const copySelectedItems = () => { + const selectedClipsText = reversedClipsDb + .filter((clip) => selectedItems.has(clip.id)) + .map((clip) => clip.clips_text) + .join("\n"); + + Clipboard.setStringAsync(selectedClipsText); + ToastAndroid.show("Text copied to clipboard", ToastAndroid.CENTER); + }; + + const deleteSelectedItems = () => { + selectedItems.forEach((id) => { + deleteClipsDb(id); + }); + clearSelection(); + }; + const clearSelection = () => { + setSelectedItems(new Set()); + }; + const reversedVal = [...val].reverse(); const showClips = ({ item: clip }: { item: Clip }) => { return ( @@ -544,7 +597,10 @@ export default function App() { Clipboard.setStringAsync(clip.clip)} + onPress={() => { + Clipboard.setStringAsync(clip.clip); + ToastAndroid.show("Text copied to clipboard", ToastAndroid.SHORT); + }} className="active:bg-stone-600 w-[2rem] h-9 p-1 rounded" > @@ -579,23 +635,38 @@ export default function App() { const reversedClipsDb = [...clipsDb].reverse(); const showClipsBd = ({ item: clip }: { item: ClipDb }) => { + const isSelected = selectedItems.has(clip.id); return ( - toggleSelection(clip.id)} + delayLongPress={selectedItems.size >= 1 ? 1 : 500} > - - + + {clip.clips_text} - - - {clip.date} | {clip.user_name} + + + {clip?.date} {clip.date && "|"} {clip.user_name} - + Clipboard.setStringAsync(clip.clips_text)} + onPress={() => { + Clipboard.setStringAsync(clip.clips_text); + ToastAndroid.show("Text copied to clipboard", ToastAndroid.SHORT); + }} className="active:bg-stone-600 w-[2rem] h-9 p-1 rounded" > @@ -628,7 +699,7 @@ export default function App() { - + ); }; @@ -786,22 +857,56 @@ export default function App() { ) : connection ? ( - - } - data={reversedClipsDb} - renderItem={showClipsBd} - keyExtractor={(clip) => clip.id.toString()} - style={styles.background} - contentContainerStyle={{ - alignItems: "center", - }} - className="w-[97%]" - /> + <> + {selectedItems.size >= 1 && ( + + + Copy + + + Edit + + + + {selectedItems.size === reversedClipsDb.length + ? "Deselect All" + : "Select All"} + + + + Delete + + + )} + + } + data={reversedClipsDb} + renderItem={showClipsBd} + keyExtractor={(clip) => clip.id.toString()} + style={styles.background} + contentContainerStyle={{ + alignItems: "center", + }} + className="w-[97%]" + /> + ) : (