From a1143f3696a0b272018f7eea43ee23a674c618e1 Mon Sep 17 00:00:00 2001
From: Akshay Nair <phenax5@gmail.com>
Date: Sat, 28 Sep 2024 13:46:22 +0530
Subject: Add context menu for apps (fav,settings,uninstall)

---
 src/components/AppMenu.tsx | 79 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 79 insertions(+)
 create mode 100644 src/components/AppMenu.tsx

(limited to 'src/components/AppMenu.tsx')

diff --git a/src/components/AppMenu.tsx b/src/components/AppMenu.tsx
new file mode 100644
index 0000000..ec0d684
--- /dev/null
+++ b/src/components/AppMenu.tsx
@@ -0,0 +1,79 @@
+import {Text, View, Modal, TouchableOpacity, Pressable} from 'react-native';
+import {AppDetail} from 'react-native-launcher-kit/typescript/Interfaces/InstalledApps';
+import {useFavorites} from '../hooks/useFavorites';
+import React, {useState} from 'react';
+import IntentLauncher from '@angelkrak/react-native-intent-launcher';
+
+export const AppMenu: React.FC<React.PropsWithChildren<{app: AppDetail}>> = ({
+  app,
+  children,
+}) => {
+  const [isOpen, setIsOpen] = useState(false);
+  const {addToFavorites, isFavorite, removeFromFavorites} = useFavorites();
+
+  const menuPress = (fn: () => Promise<void> | void) => async () => {
+    await fn();
+    setIsOpen(false);
+  };
+
+  const menuItems = [
+    isFavorite(app.packageName)
+      ? {
+          label: 'Remove from favorites',
+          onPress: menuPress(() => removeFromFavorites(app.packageName)),
+        }
+      : {
+          label: 'Add to favorites',
+          onPress: menuPress(() => addToFavorites(app.packageName)),
+        },
+    {
+      label: 'App info',
+      onPress: menuPress(() =>
+        IntentLauncher.startActivity({
+          action: 'android.settings.APPLICATION_DETAILS_SETTINGS',
+          data: 'package:' + app.packageName,
+        }),
+      ),
+    },
+    {
+      label: 'Uninstall',
+      onPress: menuPress(() =>
+        IntentLauncher.startActivity({
+          action: 'android.intent.action.DELETE',
+          data: 'package:' + app.packageName,
+        }),
+      ),
+    },
+  ];
+
+  return (
+    <>
+      <TouchableOpacity onLongPress={() => setIsOpen(true)}>
+        {children}
+      </TouchableOpacity>
+
+      <Modal
+        visible={isOpen}
+        transparent={true}
+        animationType="none"
+        onRequestClose={() => setIsOpen(false)}>
+        <TouchableOpacity className="flex-1" onPress={() => setIsOpen(false)}>
+          <View className="flex justify-center items-center h-full">
+            <View className="shadow-lg bg-gray-900 w-2/3">
+              {menuItems.map((menuItem) => (
+                <Pressable
+                  key={menuItem.label}
+                  onPress={menuItem.onPress}
+                  className="py-3 px-4 border-b border-gray-800 last:border-b-0">
+                  <Text className="text-lg text-gray-300">
+                    {menuItem.label}
+                  </Text>
+                </Pressable>
+              ))}
+            </View>
+          </View>
+        </TouchableOpacity>
+      </Modal>
+    </>
+  );
+};
-- 
cgit v1.3.1

