mirror of
https://github.com/austinried/subtracks.git
synced 2025-12-28 17:19:27 +01:00
simplify home lists and use saved setting
This commit is contained in:
parent
13ae61f160
commit
d1598d53f8
@ -8,5 +8,8 @@ export interface Server {
|
|||||||
|
|
||||||
export interface AppSettings {
|
export interface AppSettings {
|
||||||
servers: Server[]
|
servers: Server[]
|
||||||
|
home: {
|
||||||
|
lists: string[]
|
||||||
|
}
|
||||||
activeServer?: string
|
activeServer?: string
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,13 +2,14 @@ import CoverArt from '@app/components/CoverArt'
|
|||||||
import GradientScrollView from '@app/components/GradientScrollView'
|
import GradientScrollView from '@app/components/GradientScrollView'
|
||||||
import PressableOpacity from '@app/components/PressableOpacity'
|
import PressableOpacity from '@app/components/PressableOpacity'
|
||||||
import { AlbumListItem } from '@app/models/music'
|
import { AlbumListItem } from '@app/models/music'
|
||||||
import { albumLists } from '@app/state/music'
|
import { homeListsAtom, homeListsUpdatingAtom, useUpdateHomeLists } from '@app/state/music'
|
||||||
|
import { homeListTypesAtom, useActiveServerRefresh } from '@app/state/settings'
|
||||||
import colors from '@app/styles/colors'
|
import colors from '@app/styles/colors'
|
||||||
import font from '@app/styles/font'
|
import font from '@app/styles/font'
|
||||||
import { useNavigation } from '@react-navigation/native'
|
import { useNavigation } from '@react-navigation/native'
|
||||||
import { useAtomValue } from 'jotai/utils'
|
import { useAtomValue } from 'jotai/utils'
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { ScrollView, StatusBar, StyleSheet, Text, View } from 'react-native'
|
import { RefreshControl, ScrollView, StatusBar, StyleSheet, Text, View } from 'react-native'
|
||||||
|
|
||||||
const AlbumItem = React.memo<{
|
const AlbumItem = React.memo<{
|
||||||
album: AlbumListItem
|
album: AlbumListItem
|
||||||
@ -38,25 +39,18 @@ const AlbumItem = React.memo<{
|
|||||||
|
|
||||||
const Category = React.memo<{
|
const Category = React.memo<{
|
||||||
name: string
|
name: string
|
||||||
type: string
|
data: AlbumListItem[]
|
||||||
}>(({ name, type }) => {
|
}>(({ name, data }) => {
|
||||||
const state = albumLists[type]
|
|
||||||
const list = useAtomValue(state.listAtom)
|
|
||||||
const updating = useAtomValue(state.updatingAtom)
|
|
||||||
const updateList = state.useUpdateList()
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={[styles.category, { backgroundColor: updating ? 'transparent' : 'transparent' }]}>
|
<View style={styles.category}>
|
||||||
<PressableOpacity onPress={updateList} style={{ alignItems: 'flex-start' }}>
|
<Text style={styles.header}>{name}</Text>
|
||||||
<Text style={styles.header}>{name}</Text>
|
|
||||||
</PressableOpacity>
|
|
||||||
<ScrollView
|
<ScrollView
|
||||||
horizontal={true}
|
horizontal={true}
|
||||||
showsHorizontalScrollIndicator={false}
|
showsHorizontalScrollIndicator={false}
|
||||||
overScrollMode={'never'}
|
overScrollMode={'never'}
|
||||||
style={styles.artScroll}
|
style={styles.artScroll}
|
||||||
contentContainerStyle={styles.artScrollContent}>
|
contentContainerStyle={styles.artScrollContent}>
|
||||||
{list.map(album => (
|
{data.map(album => (
|
||||||
<AlbumItem key={album.id} album={album} />
|
<AlbumItem key={album.id} album={album} />
|
||||||
))}
|
))}
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
@ -64,17 +58,34 @@ const Category = React.memo<{
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
const Home = () => (
|
const Home = () => {
|
||||||
<GradientScrollView style={styles.scroll} contentContainerStyle={styles.scrollContentContainer}>
|
const types = useAtomValue(homeListTypesAtom)
|
||||||
<View style={styles.content}>
|
const lists = useAtomValue(homeListsAtom)
|
||||||
<Category name="Random Albums" type="random" />
|
const updating = useAtomValue(homeListsUpdatingAtom)
|
||||||
<Category name="Newest Albums" type="newest" />
|
const update = useUpdateHomeLists()
|
||||||
<Category name="Recent Albums" type="recent" />
|
|
||||||
<Category name="Frequent Albums" type="frequent" />
|
useActiveServerRefresh(update)
|
||||||
<Category name="Starred Albums" type="starred" />
|
|
||||||
</View>
|
return (
|
||||||
</GradientScrollView>
|
<GradientScrollView
|
||||||
)
|
style={styles.scroll}
|
||||||
|
contentContainerStyle={styles.scrollContentContainer}
|
||||||
|
refreshControl={
|
||||||
|
<RefreshControl
|
||||||
|
refreshing={updating}
|
||||||
|
onRefresh={update}
|
||||||
|
colors={[colors.accent, colors.accentLow]}
|
||||||
|
progressViewOffset={StatusBar.currentHeight}
|
||||||
|
/>
|
||||||
|
}>
|
||||||
|
<View style={styles.content}>
|
||||||
|
{types.map(type => (
|
||||||
|
<Category key={type} name={type} data={type in lists ? lists[type] : []} />
|
||||||
|
))}
|
||||||
|
</View>
|
||||||
|
</GradientScrollView>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
scroll: {
|
scroll: {
|
||||||
|
|||||||
@ -2,7 +2,8 @@ import CoverArt from '@app/components/CoverArt'
|
|||||||
import GradientFlatList from '@app/components/GradientFlatList'
|
import GradientFlatList from '@app/components/GradientFlatList'
|
||||||
import PressableOpacity from '@app/components/PressableOpacity'
|
import PressableOpacity from '@app/components/PressableOpacity'
|
||||||
import { Album } from '@app/models/music'
|
import { Album } from '@app/models/music'
|
||||||
import { albumLists } from '@app/state/music'
|
import { albumListAtom, albumListUpdatingAtom, useUpdateAlbumList } from '@app/state/music'
|
||||||
|
import { useActiveServerRefresh } from '@app/state/settings'
|
||||||
import colors from '@app/styles/colors'
|
import colors from '@app/styles/colors'
|
||||||
import font from '@app/styles/font'
|
import font from '@app/styles/font'
|
||||||
import { useNavigation } from '@react-navigation/native'
|
import { useNavigation } from '@react-navigation/native'
|
||||||
@ -51,15 +52,16 @@ const AlbumListRenderItem: React.FC<{
|
|||||||
)
|
)
|
||||||
|
|
||||||
const AlbumsList = () => {
|
const AlbumsList = () => {
|
||||||
const state = albumLists.alphabeticalByArtist
|
const list = useAtomValue(albumListAtom)
|
||||||
const list = useAtomValue(state.listAtom)
|
const updating = useAtomValue(albumListUpdatingAtom)
|
||||||
const updating = useAtomValue(state.updatingAtom)
|
const updateList = useUpdateAlbumList()
|
||||||
const updateList = state.useUpdateList()
|
|
||||||
|
useActiveServerRefresh(updateList)
|
||||||
|
|
||||||
const layout = useWindowDimensions()
|
const layout = useWindowDimensions()
|
||||||
|
|
||||||
const size = layout.width / 3 - styles.item.marginHorizontal * 2
|
const size = layout.width / 3 - styles.item.marginHorizontal * 2
|
||||||
const height = size + 44
|
const height = size + 38
|
||||||
|
|
||||||
const albumsList = list.map(album => ({ album, size, height }))
|
const albumsList = list.map(album => ({ album, size, height }))
|
||||||
|
|
||||||
@ -105,15 +107,10 @@ const styles = StyleSheet.create({
|
|||||||
marginVertical: 4,
|
marginVertical: 4,
|
||||||
marginHorizontal: 2,
|
marginHorizontal: 2,
|
||||||
flex: 1 / 3,
|
flex: 1 / 3,
|
||||||
// backgroundColor: 'green',
|
|
||||||
},
|
|
||||||
art: {
|
|
||||||
// height: 125,
|
|
||||||
},
|
},
|
||||||
itemDetails: {
|
itemDetails: {
|
||||||
flex: 1,
|
flex: 1,
|
||||||
width: '100%',
|
width: '100%',
|
||||||
// width: 125,
|
|
||||||
},
|
},
|
||||||
title: {
|
title: {
|
||||||
fontSize: 12,
|
fontSize: 12,
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
import { Atom, atom, useAtom, WritableAtom } from 'jotai'
|
|
||||||
import { atomFamily, useAtomValue, useUpdateAtom } from 'jotai/utils'
|
|
||||||
import { Album, AlbumListItem, AlbumWithSongs, Artist, ArtistArt, ArtistInfo, Song } from '@app/models/music'
|
import { Album, AlbumListItem, AlbumWithSongs, Artist, ArtistArt, ArtistInfo, Song } from '@app/models/music'
|
||||||
|
import { activeServerAtom, homeListTypesAtom } from '@app/state/settings'
|
||||||
import { SubsonicApiClient } from '@app/subsonic/api'
|
import { SubsonicApiClient } from '@app/subsonic/api'
|
||||||
import { AlbumID3Element, ArtistInfo2Element, ChildElement } from '@app/subsonic/elements'
|
import { AlbumID3Element, ArtistInfo2Element, ChildElement } from '@app/subsonic/elements'
|
||||||
import { GetArtistResponse } from '@app/subsonic/responses'
|
|
||||||
import { activeServerAtom } from '@app/state/settings'
|
|
||||||
import { GetAlbumList2Type } from '@app/subsonic/params'
|
import { GetAlbumList2Type } from '@app/subsonic/params'
|
||||||
|
import { GetArtistResponse } from '@app/subsonic/responses'
|
||||||
|
import { atom, useAtom } from 'jotai'
|
||||||
|
import { atomFamily, useAtomValue, useUpdateAtom } from 'jotai/utils'
|
||||||
|
|
||||||
export const artistsAtom = atom<Artist[]>([])
|
export const artistsAtom = atom<Artist[]>([])
|
||||||
export const artistsUpdatingAtom = atom(false)
|
export const artistsUpdatingAtom = atom(false)
|
||||||
@ -39,15 +39,26 @@ export const useUpdateArtists = () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const useUpdateAlbumListBase = (
|
export type HomeLists = { [key: string]: AlbumListItem[] }
|
||||||
type: GetAlbumList2Type,
|
|
||||||
albumListAtom: WritableAtom<AlbumListItem[], AlbumListItem[]>,
|
export const homeListsUpdatingAtom = atom(false)
|
||||||
updatingAtom: WritableAtom<boolean, boolean>,
|
export const homeListsAtom = atom<HomeLists>({})
|
||||||
size: number,
|
const homeListsWriteAtom = atom<HomeLists, { type: string; albums: AlbumListItem[] }>(
|
||||||
) => {
|
get => get(homeListsAtom),
|
||||||
|
(get, set, { type, albums }) => {
|
||||||
|
const lists = get(homeListsAtom)
|
||||||
|
set(homeListsAtom, {
|
||||||
|
...lists,
|
||||||
|
[type]: albums,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
export const useUpdateHomeLists = () => {
|
||||||
const server = useAtomValue(activeServerAtom)
|
const server = useAtomValue(activeServerAtom)
|
||||||
const setAlbumList = useUpdateAtom(albumListAtom)
|
const types = useAtomValue(homeListTypesAtom)
|
||||||
const [updating, setUpdating] = useAtom(updatingAtom)
|
const updateHomeList = useUpdateAtom(homeListsWriteAtom)
|
||||||
|
const [updating, setUpdating] = useAtom(homeListsUpdatingAtom)
|
||||||
|
|
||||||
if (!server) {
|
if (!server) {
|
||||||
return async () => {}
|
return async () => {}
|
||||||
@ -60,67 +71,45 @@ const useUpdateAlbumListBase = (
|
|||||||
setUpdating(true)
|
setUpdating(true)
|
||||||
|
|
||||||
const client = new SubsonicApiClient(server)
|
const client = new SubsonicApiClient(server)
|
||||||
const response = await client.getAlbumList2({ type, size })
|
|
||||||
|
|
||||||
setAlbumList(response.data.albums.map(a => mapAlbumID3toAlbumListItem(a, client)))
|
const promises: Promise<any>[] = []
|
||||||
|
for (const type of types) {
|
||||||
|
promises.push(
|
||||||
|
client.getAlbumList2({ type: type as GetAlbumList2Type, size: 20 }).then(response => {
|
||||||
|
updateHomeList({ type, albums: response.data.albums.map(a => mapAlbumID3toAlbumListItem(a, client)) })
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
await Promise.all(promises)
|
||||||
|
|
||||||
setUpdating(false)
|
setUpdating(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createAlbumList(type: GetAlbumList2Type, size = 20) {
|
export const albumListUpdatingAtom = atom(false)
|
||||||
const listAtom = atom<AlbumListItem[]>([])
|
export const albumListAtom = atom<AlbumListItem[]>([])
|
||||||
const listReadAtom = atom(get => get(listAtom))
|
|
||||||
const updatingAtom = atom(false)
|
|
||||||
const updatingReadAtom = atom(get => get(updatingAtom))
|
|
||||||
const useUpdateAlbumList = () => useUpdateAlbumListBase(type, listAtom, updatingAtom, size)
|
|
||||||
|
|
||||||
return { listAtom, listReadAtom, updatingAtom, updatingReadAtom, useUpdateAlbumList }
|
export const useUpdateAlbumList = () => {
|
||||||
}
|
const server = useAtomValue(activeServerAtom)
|
||||||
|
const updateList = useUpdateAtom(albumListAtom)
|
||||||
|
const [updating, setUpdating] = useAtom(albumListUpdatingAtom)
|
||||||
|
|
||||||
type ListState<T> = {
|
if (!server) {
|
||||||
listAtom: Atom<T[]>
|
return async () => {}
|
||||||
updatingAtom: Atom<boolean>
|
}
|
||||||
useUpdateList: () => () => Promise<void>
|
|
||||||
}
|
|
||||||
|
|
||||||
const alphabeticalByArtist = createAlbumList('alphabeticalByArtist', 500)
|
return async () => {
|
||||||
const recent = createAlbumList('recent')
|
if (updating) {
|
||||||
const starred = createAlbumList('starred')
|
return
|
||||||
const frequent = createAlbumList('frequent')
|
}
|
||||||
const random = createAlbumList('random')
|
setUpdating(true)
|
||||||
const newest = createAlbumList('newest')
|
|
||||||
|
|
||||||
export const albumLists: { [key: string]: ListState<AlbumListItem> } = {
|
const client = new SubsonicApiClient(server)
|
||||||
alphabeticalByArtist: {
|
const response = await client.getAlbumList2({ type: 'alphabeticalByArtist', size: 500 })
|
||||||
listAtom: alphabeticalByArtist.listReadAtom,
|
|
||||||
updatingAtom: alphabeticalByArtist.updatingReadAtom,
|
updateList(response.data.albums.map(a => mapAlbumID3toAlbumListItem(a, client)))
|
||||||
useUpdateList: alphabeticalByArtist.useUpdateAlbumList,
|
setUpdating(false)
|
||||||
},
|
}
|
||||||
recent: {
|
|
||||||
listAtom: recent.listReadAtom,
|
|
||||||
updatingAtom: recent.updatingReadAtom,
|
|
||||||
useUpdateList: recent.useUpdateAlbumList,
|
|
||||||
},
|
|
||||||
starred: {
|
|
||||||
listAtom: starred.listReadAtom,
|
|
||||||
updatingAtom: starred.updatingReadAtom,
|
|
||||||
useUpdateList: starred.useUpdateAlbumList,
|
|
||||||
},
|
|
||||||
frequent: {
|
|
||||||
listAtom: frequent.listReadAtom,
|
|
||||||
updatingAtom: frequent.updatingReadAtom,
|
|
||||||
useUpdateList: frequent.useUpdateAlbumList,
|
|
||||||
},
|
|
||||||
random: {
|
|
||||||
listAtom: random.listReadAtom,
|
|
||||||
updatingAtom: random.updatingReadAtom,
|
|
||||||
useUpdateList: random.useUpdateAlbumList,
|
|
||||||
},
|
|
||||||
newest: {
|
|
||||||
listAtom: newest.listReadAtom,
|
|
||||||
updatingAtom: newest.updatingReadAtom,
|
|
||||||
useUpdateList: newest.useUpdateAlbumList,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const albumAtomFamily = atomFamily((id: string) =>
|
export const albumAtomFamily = atomFamily((id: string) =>
|
||||||
|
|||||||
@ -1,12 +1,30 @@
|
|||||||
import { atom } from 'jotai'
|
import { atom } from 'jotai'
|
||||||
import { AppSettings } from '@app/models/settings'
|
import { AppSettings } from '@app/models/settings'
|
||||||
import atomWithAsyncStorage from '@app/storage/atomWithAsyncStorage'
|
import atomWithAsyncStorage from '@app/storage/atomWithAsyncStorage'
|
||||||
|
import { useEffect } from 'react'
|
||||||
|
import { useAtomValue } from 'jotai/utils'
|
||||||
|
|
||||||
export const appSettingsAtom = atomWithAsyncStorage<AppSettings>('@appSettings', {
|
export const appSettingsAtom = atomWithAsyncStorage<AppSettings>('@appSettings', {
|
||||||
servers: [],
|
servers: [],
|
||||||
|
home: {
|
||||||
|
lists: ['recent', 'random', 'frequent', 'starred'],
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
export const activeServerAtom = atom(get => {
|
export const activeServerAtom = atom(get => {
|
||||||
const appSettings = get(appSettingsAtom)
|
const appSettings = get(appSettingsAtom)
|
||||||
return appSettings.servers.find(x => x.id === appSettings.activeServer)
|
return appSettings.servers.find(x => x.id === appSettings.activeServer)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const useActiveServerRefresh = (update: () => any) => {
|
||||||
|
const activeServer = useAtomValue(activeServerAtom)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (activeServer) {
|
||||||
|
update()
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [activeServer])
|
||||||
|
}
|
||||||
|
|
||||||
|
export const homeListTypesAtom = atom(get => get(appSettingsAtom).home.lists)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user