From a1143f3696a0b272018f7eea43ee23a674c618e1 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 28 Sep 2024 13:46:22 +0530 Subject: Add context menu for apps (fav,settings,uninstall) --- src/components/AppMenu.tsx | 79 ++++++++++++++++++++++++++++++++++++++++++++ src/components/Clock.tsx | 4 +-- src/components/Favorites.tsx | 49 ++++++++++++++++----------- 3 files changed, 111 insertions(+), 21 deletions(-) create mode 100644 src/components/AppMenu.tsx (limited to 'src/components') 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> = ({ + app, + children, +}) => { + const [isOpen, setIsOpen] = useState(false); + const {addToFavorites, isFavorite, removeFromFavorites} = useFavorites(); + + const menuPress = (fn: () => Promise | 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 ( + <> + setIsOpen(true)}> + {children} + + + setIsOpen(false)}> + setIsOpen(false)}> + + + {menuItems.map((menuItem) => ( + + + {menuItem.label} + + + ))} + + + + + + ); +}; diff --git a/src/components/Clock.tsx b/src/components/Clock.tsx index 113201f..619a31b 100644 --- a/src/components/Clock.tsx +++ b/src/components/Clock.tsx @@ -14,7 +14,7 @@ export const Clock: React.FC = () => { const formatter = new Intl.DateTimeFormat('en', { hour: 'numeric', minute: 'numeric', - second: 'numeric', + // second: 'numeric', }); return formatter.format(date); }, [date]); @@ -30,7 +30,7 @@ export const Clock: React.FC = () => { return ( - {timeText} + {timeText} {dateText} ); diff --git a/src/components/Favorites.tsx b/src/components/Favorites.tsx index 85ab3c2..c2adae7 100644 --- a/src/components/Favorites.tsx +++ b/src/components/Favorites.tsx @@ -1,4 +1,4 @@ -import React, {useCallback, useMemo} from 'react'; +import React, {useCallback, useEffect, useMemo} from 'react'; import {Text, TouchableOpacity, View} from 'react-native'; import {AppDetail} from 'react-native-launcher-kit/typescript/Interfaces/InstalledApps'; import {useFavorites} from '../hooks/useFavorites'; @@ -8,9 +8,13 @@ import DraggableFlatList, { ShadowDecorator, } from 'react-native-draggable-flatlist'; import Icon from 'react-native-vector-icons/MaterialIcons'; +import {AppMenu} from './AppMenu'; -export const Favorites: React.FC<{apps: AppDetail[]}> = ({apps}) => { - const {favoriteAppNames, setFavorites} = useFavorites(); +export const Favorites: React.FC<{apps: AppDetail[]; active: boolean}> = ({ + apps, + active, +}) => { + const {favoriteAppNames, setFavorites, refreshFavorites} = useFavorites(); const appByName = useMemo( () => Object.fromEntries(apps.map((app) => [app.packageName, app])), @@ -29,8 +33,13 @@ export const Favorites: React.FC<{apps: AppDetail[]}> = ({apps}) => { [setFavorites], ); + useEffect(() => { + if (!active) return; + refreshFavorites(); + }, [active, refreshFavorites]); + return ( - + = ({apps}) => { renderItem={({item: app, drag, isActive}) => ( - - {app.label} - - - - - - + + + {app.label} + + + + + + + )} - renderPlaceholder={() => } + renderPlaceholder={() => } /> ); -- cgit v1.3.1