From e217fcff3653955e6243507c4984005d8d704127 Mon Sep 17 00:00:00 2001 From: Fredrik Burmester Date: Fri, 21 Feb 2025 10:49:26 +0100 Subject: [PATCH] fix: refactor settings location for notifications --- app/(auth)/(tabs)/(home)/settings.tsx | 32 ++++++++++-- components/settings/OtherSettings.tsx | 9 +--- .../settings/RecentlyAddedNotifications.tsx | 50 +++++++++++++++++++ components/settings/StorageSettings.tsx | 22 +------- 4 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 components/settings/RecentlyAddedNotifications.tsx diff --git a/app/(auth)/(tabs)/(home)/settings.tsx b/app/(auth)/(tabs)/(home)/settings.tsx index aba54ae1..415d0c02 100644 --- a/app/(auth)/(tabs)/(home)/settings.tsx +++ b/app/(auth)/(tabs)/(home)/settings.tsx @@ -16,11 +16,21 @@ import { useHaptic } from "@/hooks/useHaptic"; import { useJellyfin } from "@/providers/JellyfinProvider"; import { clearLogs } from "@/utils/log"; import { storage } from "@/utils/mmkv"; +import { RECENTLY_ADDED_SENT_NOTIFICATIONS_ITEM_IDS_KEY } from "@/utils/recently-added-notifications"; import { useNavigation, useRouter } from "expo-router"; import { t } from "i18next"; -import React, { useEffect } from "react"; -import { ScrollView, TouchableOpacity, View } from "react-native"; +import React, { useCallback, useEffect, useMemo } from "react"; +import { + ScrollView, + StyleSheet, + Switch, + TouchableOpacity, + View, +} from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; +import * as TaskManager from "expo-task-manager"; +import { BACKGROUND_FETCH_TASK_RECENTLY_ADDED } from "@/utils/background-tasks"; +import { RecentlyAddedNotificationsSettings } from "@/components/settings/RecentlyAddedNotifications"; export default function settings() { const router = useRouter(); @@ -91,7 +101,7 @@ export default function settings() { /> - + router.push("/settings/logs/page")} @@ -106,7 +116,21 @@ export default function settings() { - + + + + + + + ); diff --git a/components/settings/OtherSettings.tsx b/components/settings/OtherSettings.tsx index c408d333..eb6fc673 100644 --- a/components/settings/OtherSettings.tsx +++ b/components/settings/OtherSettings.tsx @@ -22,6 +22,7 @@ import { ListItem } from "../list/ListItem"; import { useTranslation } from "react-i18next"; import DisabledSetting from "@/components/settings/DisabledSetting"; import Dropdown from "@/components/common/Dropdown"; +import { RECENTLY_ADDED_SENT_NOTIFICATIONS_ITEM_IDS_KEY } from "@/utils/recently-added-notifications"; export const OtherSettings: React.FC = () => { const router = useRouter(); @@ -202,14 +203,6 @@ export const OtherSettings: React.FC = () => { } /> - - - updateSettings({ recentlyAddedNotifications }) - } - /> - ); diff --git a/components/settings/RecentlyAddedNotifications.tsx b/components/settings/RecentlyAddedNotifications.tsx new file mode 100644 index 00000000..a4ef2040 --- /dev/null +++ b/components/settings/RecentlyAddedNotifications.tsx @@ -0,0 +1,50 @@ +import settings from "@/app/(auth)/(tabs)/(home)/settings"; +import { Switch, View } from "react-native"; +import { ListGroup } from "../list/ListGroup"; +import { ListItem } from "../list/ListItem"; +import { useSettings } from "@/utils/atoms/settings"; +import React, { useCallback, useEffect, useMemo } from "react"; +import { BACKGROUND_FETCH_TASK_RECENTLY_ADDED } from "@/utils/background-tasks"; +import { storage } from "@/utils/mmkv"; +import { RECENTLY_ADDED_SENT_NOTIFICATIONS_ITEM_IDS_KEY } from "@/utils/recently-added-notifications"; +import * as TaskManager from "expo-task-manager"; +import * as BackgroundFetch from "expo-background-fetch"; + +export const RecentlyAddedNotificationsSettings: React.FC = ({ ...props }) => { + const [settings, updateSettings] = useSettings(); + + const clearRecentlyAddedNotifications = useCallback(() => { + storage.delete(RECENTLY_ADDED_SENT_NOTIFICATIONS_ITEM_IDS_KEY); + }, []); + + const recentlyAddedNotificationsItemIds = useMemo(() => { + const s = storage.getString(RECENTLY_ADDED_SENT_NOTIFICATIONS_ITEM_IDS_KEY); + if (!s) return [] as string[]; + try { + const t: string[] = JSON.parse(s); + return t; + } catch (e) { + throw new Error("Failed to parse recently added notifications item ids"); + } + }, []); + + return ( + + + + + updateSettings({ recentlyAddedNotifications }) + } + /> + + + + + ); +}; diff --git a/components/settings/StorageSettings.tsx b/components/settings/StorageSettings.tsx index 240d4029..cc001db7 100644 --- a/components/settings/StorageSettings.tsx +++ b/components/settings/StorageSettings.tsx @@ -44,20 +44,6 @@ export const StorageSettings = () => { return ((value / total) * 100).toFixed(2); }; - const clearRecentlyAddedNotifications = useCallback(() => { - storage.delete(RECENTLY_ADDED_SENT_NOTIFICATIONS_ITEM_IDS_KEY); - }, []); - - const recentlyAddedNotificationsItemIds = useMemo(() => { - const s = storage.getString(RECENTLY_ADDED_SENT_NOTIFICATIONS_ITEM_IDS_KEY); - if (!s) return [] as string[]; - try { - const t: string[] = JSON.parse(s); - return t; - } catch (e) { - throw new Error("Failed to parse recently added notifications item ids"); - } - }, []); return ( @@ -127,13 +113,7 @@ export const StorageSettings = () => { title={t("home.settings.storage.delete_all_downloaded_files")} /> - - - + ); };