mirror of
https://github.com/austinried/subtracks.git
synced 2026-02-10 15:02:42 +01:00
reorg again, absolute (module) imports
This commit is contained in:
204
app/state/music.ts
Normal file
204
app/state/music.ts
Normal file
@@ -0,0 +1,204 @@
|
||||
import { atom, useAtom } from 'jotai'
|
||||
import { atomFamily, useAtomValue, useUpdateAtom } from 'jotai/utils'
|
||||
import { Album, AlbumArt, AlbumWithSongs, Artist, ArtistArt, ArtistInfo, Song } from '@app/models/music'
|
||||
import { SubsonicApiClient } from '@app/subsonic/api'
|
||||
import { AlbumID3Element, ArtistInfo2Element, ChildElement } from '@app/subsonic/elements'
|
||||
import { GetArtistResponse } from '@app/subsonic/responses'
|
||||
import { activeServerAtom } from '@app/state/settings'
|
||||
|
||||
export const artistsAtom = atom<Artist[]>([])
|
||||
export const artistsUpdatingAtom = atom(false)
|
||||
|
||||
export const useUpdateArtists = () => {
|
||||
const server = useAtomValue(activeServerAtom)
|
||||
const [updating, setUpdating] = useAtom(artistsUpdatingAtom)
|
||||
const setArtists = useUpdateAtom(artistsAtom)
|
||||
|
||||
if (!server) {
|
||||
return () => Promise.resolve()
|
||||
}
|
||||
|
||||
return async () => {
|
||||
if (updating) {
|
||||
return
|
||||
}
|
||||
setUpdating(true)
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const response = await client.getArtists()
|
||||
|
||||
setArtists(
|
||||
response.data.artists.map(x => ({
|
||||
id: x.id,
|
||||
name: x.name,
|
||||
starred: x.starred,
|
||||
})),
|
||||
)
|
||||
setUpdating(false)
|
||||
}
|
||||
}
|
||||
|
||||
export const albumsAtom = atom<Record<string, Album>>({})
|
||||
export const albumsUpdatingAtom = atom(false)
|
||||
|
||||
export const useUpdateAlbums = () => {
|
||||
const server = useAtomValue(activeServerAtom)
|
||||
const [updating, setUpdating] = useAtom(albumsUpdatingAtom)
|
||||
const setAlbums = useUpdateAtom(albumsAtom)
|
||||
|
||||
if (!server) {
|
||||
return () => Promise.resolve()
|
||||
}
|
||||
|
||||
return async () => {
|
||||
if (updating) {
|
||||
return
|
||||
}
|
||||
setUpdating(true)
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const response = await client.getAlbumList2({ type: 'alphabeticalByArtist', size: 500 })
|
||||
|
||||
setAlbums(
|
||||
response.data.albums.reduce((acc, next) => {
|
||||
const album = mapAlbumID3(next, client)
|
||||
acc[album.id] = album
|
||||
return acc
|
||||
}, {} as Record<string, Album>),
|
||||
)
|
||||
setUpdating(false)
|
||||
}
|
||||
}
|
||||
|
||||
export const albumAtomFamily = atomFamily((id: string) =>
|
||||
atom<AlbumWithSongs | undefined>(async get => {
|
||||
const server = get(activeServerAtom)
|
||||
if (!server) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const response = await client.getAlbum({ id })
|
||||
return mapAlbumID3WithSongs(response.data.album, response.data.songs, client)
|
||||
}),
|
||||
)
|
||||
|
||||
export const albumArtAtomFamily = atomFamily((id: string) =>
|
||||
atom<AlbumArt | undefined>(async get => {
|
||||
const server = get(activeServerAtom)
|
||||
if (!server) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const albums = get(albumsAtom)
|
||||
const album = id in albums ? albums[id] : undefined
|
||||
if (!album) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
|
||||
return {
|
||||
uri: album.coverArt ? client.getCoverArtUri({ id: album.coverArt }) : undefined,
|
||||
thumbUri: album.coverArt ? client.getCoverArtUri({ id: album.coverArt, size: '256' }) : undefined,
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
export const artistInfoAtomFamily = atomFamily((id: string) =>
|
||||
atom<ArtistInfo | undefined>(async get => {
|
||||
const server = get(activeServerAtom)
|
||||
if (!server) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const client = new SubsonicApiClient(server)
|
||||
const [artistResponse, artistInfoResponse] = await Promise.all([
|
||||
client.getArtist({ id }),
|
||||
client.getArtistInfo2({ id }),
|
||||
])
|
||||
return mapArtistInfo(artistResponse.data, artistInfoResponse.data.artistInfo, client)
|
||||
}),
|
||||
)
|
||||
|
||||
export const artistArtAtomFamily = atomFamily((id: string) =>
|
||||
atom<ArtistArt | undefined>(async get => {
|
||||
const artistInfo = get(artistInfoAtomFamily(id))
|
||||
if (!artistInfo) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const coverArtUris = artistInfo.albums
|
||||
.filter(a => a.coverArtThumbUri !== undefined)
|
||||
.sort((a, b) => {
|
||||
if (b.year && a.year) {
|
||||
return b.year - a.year
|
||||
} else {
|
||||
return a.name.localeCompare(b.name)
|
||||
}
|
||||
})
|
||||
.map(a => a.coverArtThumbUri) as string[]
|
||||
|
||||
return {
|
||||
coverArtUris,
|
||||
uri: artistInfo.mediumImageUrl,
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
||||
function mapArtistInfo(
|
||||
artistResponse: GetArtistResponse,
|
||||
artistInfo: ArtistInfo2Element,
|
||||
client: SubsonicApiClient,
|
||||
): ArtistInfo {
|
||||
const info = { ...artistInfo } as any
|
||||
delete info.similarArtists
|
||||
|
||||
const { artist, albums } = artistResponse
|
||||
|
||||
const mappedAlbums = albums.map(a => mapAlbumID3(a, client))
|
||||
const coverArtUris = mappedAlbums
|
||||
.sort((a, b) => {
|
||||
if (a.year && b.year) {
|
||||
return a.year - b.year
|
||||
} else {
|
||||
return a.name.localeCompare(b.name) - 9000
|
||||
}
|
||||
})
|
||||
.map(a => a.coverArtThumbUri)
|
||||
|
||||
return {
|
||||
...artist,
|
||||
...info,
|
||||
albums: mappedAlbums,
|
||||
coverArtUris,
|
||||
}
|
||||
}
|
||||
|
||||
function mapAlbumID3(album: AlbumID3Element, client: SubsonicApiClient): Album {
|
||||
return {
|
||||
...album,
|
||||
coverArtUri: album.coverArt ? client.getCoverArtUri({ id: album.coverArt }) : undefined,
|
||||
coverArtThumbUri: album.coverArt ? client.getCoverArtUri({ id: album.coverArt, size: '256' }) : undefined,
|
||||
}
|
||||
}
|
||||
|
||||
function mapChildToSong(child: ChildElement, client: SubsonicApiClient): Song {
|
||||
return {
|
||||
...child,
|
||||
streamUri: client.streamUri({ id: child.id }),
|
||||
coverArtUri: child.coverArt ? client.getCoverArtUri({ id: child.coverArt }) : undefined,
|
||||
coverArtThumbUri: child.coverArt ? client.getCoverArtUri({ id: child.coverArt, size: '256' }) : undefined,
|
||||
}
|
||||
}
|
||||
|
||||
function mapAlbumID3WithSongs(
|
||||
album: AlbumID3Element,
|
||||
songs: ChildElement[],
|
||||
client: SubsonicApiClient,
|
||||
): AlbumWithSongs {
|
||||
return {
|
||||
...mapAlbumID3(album, client),
|
||||
songs: songs.map(s => mapChildToSong(s, client)),
|
||||
}
|
||||
}
|
||||
12
app/state/settings.ts
Normal file
12
app/state/settings.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { atom } from 'jotai'
|
||||
import { AppSettings } from '@app/models/settings'
|
||||
import atomWithAsyncStorage from '@app/storage/atomWithAsyncStorage'
|
||||
|
||||
export const appSettingsAtom = atomWithAsyncStorage<AppSettings>('@appSettings', {
|
||||
servers: [],
|
||||
})
|
||||
|
||||
export const activeServerAtom = atom(get => {
|
||||
const appSettings = get(appSettingsAtom)
|
||||
return appSettings.servers.find(x => x.id === appSettings.activeServer)
|
||||
})
|
||||
278
app/state/trackplayer.ts
Normal file
278
app/state/trackplayer.ts
Normal file
@@ -0,0 +1,278 @@
|
||||
import equal from 'fast-deep-equal'
|
||||
import { atom } from 'jotai'
|
||||
import { useAtomValue, useUpdateAtom } from 'jotai/utils'
|
||||
import { useEffect } from 'react'
|
||||
import TrackPlayer, { State, Track } from 'react-native-track-player'
|
||||
import { Song } from '@app/models/music'
|
||||
import PromiseQueue from '@app/util/PromiseQueue'
|
||||
|
||||
type TrackExt = Track & {
|
||||
id: string
|
||||
queueName: string
|
||||
artworkThumb?: string
|
||||
}
|
||||
|
||||
type OptionalTrackExt = TrackExt | undefined
|
||||
|
||||
type Progress = {
|
||||
position: number
|
||||
duration: number
|
||||
buffered: number
|
||||
}
|
||||
|
||||
const playerState = atom<State>(State.None)
|
||||
export const playerStateAtom = atom<State, State>(
|
||||
get => get(playerState),
|
||||
(get, set, update) => {
|
||||
if (get(playerState) !== update) {
|
||||
set(playerState, update)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
const currentTrack = atom<OptionalTrackExt>(undefined)
|
||||
export const currentTrackAtom = atom<OptionalTrackExt, OptionalTrackExt>(
|
||||
get => get(currentTrack),
|
||||
(get, set, update) => {
|
||||
if (!equal(get(currentTrack), update)) {
|
||||
set(currentTrack, update)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
const _queue = atom<TrackExt[]>([])
|
||||
export const queueReadAtom = atom<TrackExt[]>(get => get(_queue))
|
||||
export const queueWriteAtom = atom<TrackExt[], TrackExt[]>(
|
||||
get => get(_queue),
|
||||
(get, set, update) => {
|
||||
if (!equal(get(_queue), update)) {
|
||||
set(_queue, update)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
export const queueNameAtom = atom<string | undefined>(get => {
|
||||
const queue = get(_queue)
|
||||
if (queue.length > 0) {
|
||||
return queue[0].queueName
|
||||
}
|
||||
return undefined
|
||||
})
|
||||
|
||||
const _progress = atom<Progress>({ position: 0, duration: 0, buffered: 0 })
|
||||
export const progressAtom = atom<Progress, Progress>(
|
||||
get => get(_progress),
|
||||
(get, set, update) => {
|
||||
if (!equal(get(_progress), update)) {
|
||||
set(_progress, update)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
const progressSubs = atom(0)
|
||||
export const progressSubsAtom = atom(get => get(progressSubs))
|
||||
const addProgressSub = atom(null, (get, set) => {
|
||||
set(progressSubs, get(progressSubs) + 1)
|
||||
})
|
||||
const removeProgressSub = atom(null, (get, set) => {
|
||||
set(progressSubs, get(progressSubs) - 1)
|
||||
})
|
||||
|
||||
export const trackPlayerCommands = new PromiseQueue(1)
|
||||
|
||||
const getQueue = async (): Promise<TrackExt[]> => {
|
||||
return ((await TrackPlayer.getQueue()) as TrackExt[]) || []
|
||||
}
|
||||
|
||||
const getTrack = async (index: number): Promise<TrackExt> => {
|
||||
return ((await TrackPlayer.getTrack(index)) as TrackExt) || undefined
|
||||
}
|
||||
|
||||
const getPlayerState = async (): Promise<State> => {
|
||||
return (await TrackPlayer.getState()) || State.None
|
||||
}
|
||||
|
||||
const getProgress = async (): Promise<Progress> => {
|
||||
const [position, duration, buffered] = await Promise.all([
|
||||
TrackPlayer.getPosition(),
|
||||
TrackPlayer.getDuration(),
|
||||
TrackPlayer.getBufferedPosition(),
|
||||
])
|
||||
return {
|
||||
position: position || 0,
|
||||
duration: duration || 0,
|
||||
buffered: buffered || 0,
|
||||
}
|
||||
}
|
||||
|
||||
export const useRefreshQueue = () => {
|
||||
const setQueue = useUpdateAtom(queueWriteAtom)
|
||||
|
||||
return () =>
|
||||
trackPlayerCommands.enqueue(async () => {
|
||||
setQueue(await getQueue())
|
||||
})
|
||||
}
|
||||
|
||||
export const useRefreshCurrentTrack = () => {
|
||||
const setCurrentTrack = useUpdateAtom(currentTrackAtom)
|
||||
|
||||
return () =>
|
||||
trackPlayerCommands.enqueue(async () => {
|
||||
const index = await TrackPlayer.getCurrentTrack()
|
||||
if (typeof index === 'number' && index >= 0) {
|
||||
setCurrentTrack(await getTrack(index))
|
||||
} else {
|
||||
setCurrentTrack(undefined)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const useRefreshPlayerState = () => {
|
||||
const setPlayerState = useUpdateAtom(playerStateAtom)
|
||||
|
||||
return () =>
|
||||
trackPlayerCommands.enqueue(async () => {
|
||||
setPlayerState(await getPlayerState())
|
||||
})
|
||||
}
|
||||
|
||||
export const useRefreshProgress = () => {
|
||||
const setProgress = useUpdateAtom(progressAtom)
|
||||
|
||||
return () =>
|
||||
trackPlayerCommands.enqueue(async () => {
|
||||
setProgress(await getProgress())
|
||||
})
|
||||
}
|
||||
|
||||
export const usePlay = () => {
|
||||
return () => trackPlayerCommands.enqueue(() => TrackPlayer.play())
|
||||
}
|
||||
|
||||
export const usePause = () => {
|
||||
return () => trackPlayerCommands.enqueue(() => TrackPlayer.pause())
|
||||
}
|
||||
|
||||
export const usePrevious = () => {
|
||||
const setCurrentTrack = useUpdateAtom(currentTrackAtom)
|
||||
|
||||
return () =>
|
||||
trackPlayerCommands.enqueue(async () => {
|
||||
const [current, queue] = await Promise.all([await TrackPlayer.getCurrentTrack(), await getQueue()])
|
||||
if (current > 0) {
|
||||
await TrackPlayer.skipToPrevious()
|
||||
setCurrentTrack(queue[current - 1])
|
||||
} else {
|
||||
await TrackPlayer.seekTo(0)
|
||||
}
|
||||
await TrackPlayer.play()
|
||||
})
|
||||
}
|
||||
|
||||
export const useNext = () => {
|
||||
const setCurrentTrack = useUpdateAtom(currentTrackAtom)
|
||||
|
||||
return () =>
|
||||
trackPlayerCommands.enqueue(async () => {
|
||||
const [current, queue] = await Promise.all([await TrackPlayer.getCurrentTrack(), await getQueue()])
|
||||
if (current >= queue.length - 1) {
|
||||
await TrackPlayer.skip(0)
|
||||
await TrackPlayer.pause()
|
||||
setCurrentTrack(queue[0])
|
||||
} else {
|
||||
await TrackPlayer.skipToNext()
|
||||
setCurrentTrack(queue[current + 1])
|
||||
await TrackPlayer.play()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const useAdd = () => {
|
||||
const setQueue = useUpdateAtom(queueWriteAtom)
|
||||
const setCurrentTrack = useUpdateAtom(currentTrackAtom)
|
||||
|
||||
return (tracks: TrackExt | TrackExt[], insertBeforeindex?: number) =>
|
||||
trackPlayerCommands.enqueue(async () => {
|
||||
await TrackPlayer.add(tracks, insertBeforeindex)
|
||||
|
||||
const queue = await getQueue()
|
||||
setQueue(queue)
|
||||
setCurrentTrack(queue.length > 0 ? queue[await TrackPlayer.getCurrentTrack()] : undefined)
|
||||
})
|
||||
}
|
||||
|
||||
export const useReset = (enqueue = true) => {
|
||||
const setQueue = useUpdateAtom(queueWriteAtom)
|
||||
const setCurrentTrack = useUpdateAtom(currentTrackAtom)
|
||||
|
||||
const reset = async () => {
|
||||
await TrackPlayer.reset()
|
||||
setQueue([])
|
||||
setCurrentTrack(undefined)
|
||||
}
|
||||
|
||||
return enqueue ? () => trackPlayerCommands.enqueue(reset) : reset
|
||||
}
|
||||
|
||||
export const useSetQueue = () => {
|
||||
const setCurrentTrack = useUpdateAtom(currentTrackAtom)
|
||||
const setQueue = useUpdateAtom(queueWriteAtom)
|
||||
const reset = useReset(false)
|
||||
|
||||
return async (songs: Song[], name: string, playId?: string) =>
|
||||
trackPlayerCommands.enqueue(async () => {
|
||||
await TrackPlayer.setupPlayer()
|
||||
await reset()
|
||||
const tracks = songs.map(s => mapSongToTrack(s, name))
|
||||
|
||||
if (playId) {
|
||||
setCurrentTrack(tracks.find(t => t.id === playId))
|
||||
}
|
||||
|
||||
if (!playId) {
|
||||
await TrackPlayer.add(tracks)
|
||||
} else if (playId === tracks[0].id) {
|
||||
await TrackPlayer.add(tracks)
|
||||
await TrackPlayer.play()
|
||||
} else {
|
||||
const playIndex = tracks.findIndex(t => t.id === playId)
|
||||
const tracks1 = tracks.slice(0, playIndex)
|
||||
const tracks2 = tracks.slice(playIndex)
|
||||
|
||||
await TrackPlayer.add(tracks2)
|
||||
await TrackPlayer.play()
|
||||
|
||||
await TrackPlayer.add(tracks1, 0)
|
||||
}
|
||||
|
||||
setQueue(await getQueue())
|
||||
})
|
||||
}
|
||||
|
||||
export const useProgress = () => {
|
||||
const progress = useAtomValue(progressAtom)
|
||||
const addSub = useUpdateAtom(addProgressSub)
|
||||
const removeSub = useUpdateAtom(removeProgressSub)
|
||||
|
||||
useEffect(() => {
|
||||
addSub()
|
||||
return removeSub
|
||||
}, [addSub, removeSub])
|
||||
|
||||
return progress
|
||||
}
|
||||
|
||||
function mapSongToTrack(song: Song, queueName: string): TrackExt {
|
||||
return {
|
||||
id: song.id,
|
||||
queueName,
|
||||
title: song.title,
|
||||
artist: song.artist || 'Unknown Artist',
|
||||
album: song.album || 'Unknown Album',
|
||||
url: song.streamUri,
|
||||
artwork: song.coverArtUri,
|
||||
artworkThumb: song.coverArtThumbUri,
|
||||
duration: song.duration,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user