feat(mobile): add device-local artwork colors toggle
This commit is contained in:
3 files changed
+99
-2
No files matched your search
@@ -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() {
|
||||
})}
|
||||
</View>
|
||||
|
||||
{section === "profile" ? <ProfileSection user={user} /> : null}
|
||||
{section === "profile" ? (
|
||||
<>
|
||||
<AppearanceSection />
|
||||
<ProfileSection user={user} />
|
||||
</>
|
||||
) : null}
|
||||
{section === "quality" ? <QualitySection user={user} /> : null}
|
||||
{section === "library" && isAdmin ? <LibrarySection /> : 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 (
|
||||
<SettingsCard title="Appearance">
|
||||
<SwitchRow
|
||||
title="Artwork colors"
|
||||
subtitle="Use artwork colors on this device."
|
||||
value={enabled}
|
||||
disabled={!loaded || saving}
|
||||
onValueChange={async (checked) => {
|
||||
setSaving(true)
|
||||
try {
|
||||
await setArtworkColorsEnabled(checked)
|
||||
} catch {
|
||||
showError("Could not save artwork colors preference.")
|
||||
} finally {
|
||||
setSaving(false)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</SettingsCard>
|
||||
)
|
||||
}
|
||||
|
||||
function ProfileSection({ user }: { user: CurrentUser }) {
|
||||
const colors = useThemeColors()
|
||||
const updateName = useMutation(api.users.updateProfileName)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<void> | 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()
|
||||
}
|
||||
Reference in new issue
Block a user