diff --git a/apps/mobile/app/(app)/(tabs)/home/settings.tsx b/apps/mobile/app/(app)/(tabs)/home/settings.tsx index 5d86e35..ebbe151 100644 --- a/apps/mobile/app/(app)/(tabs)/home/settings.tsx +++ b/apps/mobile/app/(app)/(tabs)/home/settings.tsx @@ -20,6 +20,11 @@ import { contentBottomInset } from "@/components/player" import { Button, Input, LoadingView } from "@/components/ui/primitives" import { useInvalidateAuthSession } from "@/contexts/convex-auth-session" import { useServerConfig } from "@/contexts/server-config" +import { + setArtworkColorsEnabled, + useArtworkColorsEnabled, + useArtworkColorPreferenceLoaded, +} from "@/lib/artwork-color-preference" import { initialsFromName } from "@/lib/format" import { useCachedQuery } from "@/hooks/use-cached-query" import { changeServer, signOut } from "@/lib/session" @@ -143,7 +148,12 @@ export default function SettingsScreen() { })} - {section === "profile" ? : null} + {section === "profile" ? ( + <> + + + + ) : null} {section === "quality" ? : null} {section === "library" && isAdmin ? : null} {section === "users" && isAdmin && allUsers ? ( @@ -160,6 +170,33 @@ export default function SettingsScreen() { ) } +function AppearanceSection() { + const enabled = useArtworkColorsEnabled() + const loaded = useArtworkColorPreferenceLoaded() + const [saving, setSaving] = useState(false) + + return ( + + { + setSaving(true) + try { + await setArtworkColorsEnabled(checked) + } catch { + showError("Could not save artwork colors preference.") + } finally { + setSaving(false) + } + }} + /> + + ) +} + function ProfileSection({ user }: { user: CurrentUser }) { const colors = useThemeColors() const updateName = useMutation(api.users.updateProfileName) diff --git a/apps/mobile/hooks/use-cover-palette.ts b/apps/mobile/hooks/use-cover-palette.ts index abd2e1c..3413101 100644 --- a/apps/mobile/hooks/use-cover-palette.ts +++ b/apps/mobile/hooks/use-cover-palette.ts @@ -1,4 +1,8 @@ import { useEffect, useMemo, useState } from "react" +import { + artworkColorsEnabled, + useArtworkColorsEnabled, +} from "@/lib/artwork-color-preference" import { extractCoverSwatches } from "@/lib/artwork-colors" import { @@ -52,6 +56,7 @@ function extractSwatches(key: string) { /** Warm visible artwork colors before a detail page is pushed. */ export function prefetchCoverPalette(uri: string) { + if (!artworkColorsEnabled()) return const key = withCoverImageSize(uri, 64) if (cachedSwatches(key) !== undefined) return void extractSwatches(key) @@ -69,9 +74,10 @@ export function useCoverPalette( uri: string, hasCover: boolean | undefined ): { palette: CoverPalette; settled: boolean } { + const enabled = useArtworkColorsEnabled() const theme = useThemeColors() const isDark = useIsDark() - const key = uri ? withCoverImageSize(uri, 64) : "" + const key = enabled && uri ? withCoverImageSize(uri, 64) : "" const [resolved, setResolved] = useState(() => ({ key, swatches: key ? cachedSwatches(key) : null, diff --git a/apps/mobile/lib/artwork-color-preference.ts b/apps/mobile/lib/artwork-color-preference.ts new file mode 100644 index 0000000..2651ddd --- /dev/null +++ b/apps/mobile/lib/artwork-color-preference.ts @@ -0,0 +1,54 @@ +import AsyncStorage from "@react-native-async-storage/async-storage" +import { useSyncExternalStore } from "react" + +const STORAGE_KEY = "otunes:artwork-colors" +const listeners = new Set<() => void>() +let enabled = true +let loaded = false +let loading: Promise | undefined + +function notify() { + for (const listener of listeners) listener() +} + +function load() { + return (loading ??= AsyncStorage.getItem(STORAGE_KEY) + .then((value) => { + enabled = value !== "false" + }) + .catch(() => { + // Keep the default when local storage is unavailable. + }) + .finally(() => { + loaded = true + notify() + })) +} + +function subscribe(listener: () => void) { + listeners.add(listener) + void load() + return () => { + listeners.delete(listener) + } +} + +/** Device preference, kept outside account data and the disposable query cache. */ +export function useArtworkColorsEnabled() { + return useSyncExternalStore(subscribe, () => loaded && enabled, () => false) +} + +export function useArtworkColorPreferenceLoaded() { + return useSyncExternalStore(subscribe, () => loaded, () => false) +} + +export function artworkColorsEnabled() { + return loaded && enabled +} + +export async function setArtworkColorsEnabled(value: boolean) { + await load() + await AsyncStorage.setItem(STORAGE_KEY, String(value)) + enabled = value + notify() +}