From f859be51a562c7b598b339879960657ff80d92f8 Mon Sep 17 00:00:00 2001
From: austinried <4966622+austinried@users.noreply.github.com>
Date: Tue, 20 Jul 2021 13:02:42 +0900
Subject: [PATCH] impl shuffle, controls are temp
---
app/components/TrackPlayerState.tsx | 4 +-
app/screens/AlbumView.tsx | 7 +-
app/screens/ArtistView.tsx | 6 +-
app/screens/PlaylistView.tsx | 6 +-
app/state/trackplayer.ts | 107 ++++++++++++++++++++--------
5 files changed, 89 insertions(+), 41 deletions(-)
diff --git a/app/components/TrackPlayerState.tsx b/app/components/TrackPlayerState.tsx
index 10527ad..d488b21 100644
--- a/app/components/TrackPlayerState.tsx
+++ b/app/components/TrackPlayerState.tsx
@@ -8,7 +8,7 @@ import {
playerStateAtom,
progressAtom,
progressSubsAtom,
- queueWriteAtom,
+ queueAtom,
useRefreshCurrentTrack,
useRefreshPlayerState,
useRefreshProgress,
@@ -85,7 +85,7 @@ const PlayerState = () => {
}
const QueueState = () => {
- const setQueue = useUpdateAtom(queueWriteAtom)
+ const setQueue = useUpdateAtom(queueAtom)
const refreshQueue = useRefreshQueue()
const update = async (payload?: Payload) => {
diff --git a/app/screens/AlbumView.tsx b/app/screens/AlbumView.tsx
index bd7505b..0b167ee 100644
--- a/app/screens/AlbumView.tsx
+++ b/app/screens/AlbumView.tsx
@@ -26,7 +26,8 @@ const AlbumDetails: React.FC<{
const Songs = () => (
<>
-
{album.songs
@@ -37,8 +38,8 @@ const AlbumDetails: React.FC<{
return a.title.localeCompare(b.title)
}
})
- .map(s => (
- setQueue(album.songs, album.name, s.id)} />
+ .map((s, i) => (
+ setQueue(album.songs, album.name, i)} />
))}
>
diff --git a/app/screens/ArtistView.tsx b/app/screens/ArtistView.tsx
index 1a48a64..f9533a1 100644
--- a/app/screens/ArtistView.tsx
+++ b/app/screens/ArtistView.tsx
@@ -48,13 +48,13 @@ const ArtistDetails: React.FC<{ id: string }> = ({ id }) => {
const TopSongs = () => (
<>
Top Songs
- {artist.topSongs.map(s => (
+ {artist.topSongs.map((s, i) => (
setQueue(artist.topSongs, `Top Songs: ${artist.name}`, s.id)}
+ onPress={() => setQueue(artist.topSongs, `Top Songs: ${artist.name}`, i)}
/>
))}
>
diff --git a/app/screens/PlaylistView.tsx b/app/screens/PlaylistView.tsx
index 3c26182..69d2941 100644
--- a/app/screens/PlaylistView.tsx
+++ b/app/screens/PlaylistView.tsx
@@ -26,11 +26,11 @@ const PlaylistDetails: React.FC<{
const Songs = () => (
<>
- setQueue(playlist.songs, playlist.name, playlist.songs[0].id)} />
+ setQueue(playlist.songs, playlist.name)} />
- {playlist.songs.map((s, index) => (
- setQueue(playlist.songs, playlist.name, s.id)} />
+ {playlist.songs.map((s, i) => (
+ setQueue(playlist.songs, playlist.name, i)} />
))}
>
diff --git a/app/state/trackplayer.ts b/app/state/trackplayer.ts
index 3e86f16..5404081 100644
--- a/app/state/trackplayer.ts
+++ b/app/state/trackplayer.ts
@@ -1,6 +1,6 @@
import equal from 'fast-deep-equal'
import { atom } from 'jotai'
-import { useAtomValue, useUpdateAtom } from 'jotai/utils'
+import { useAtomCallback, useAtomValue, useUpdateAtom } from 'jotai/utils'
import { useEffect } from 'react'
import TrackPlayer, { State, Track } from 'react-native-track-player'
import { Song } from '@app/models/music'
@@ -9,6 +9,7 @@ import PromiseQueue from '@app/util/PromiseQueue'
type TrackExt = Track & {
id: string
queueName: string
+ queueIndex?: number
artworkThumb?: string
}
@@ -41,8 +42,7 @@ export const currentTrackAtom = atom(
)
const _queue = atom([])
-export const queueReadAtom = atom(get => get(_queue))
-export const queueWriteAtom = atom(
+export const queueAtom = atom(
get => get(_queue),
(get, set, update) => {
if (!equal(get(_queue), update)) {
@@ -53,10 +53,22 @@ export const queueWriteAtom = atom(
export const queueNameAtom = atom(get => {
const queue = get(_queue)
- if (queue.length > 0) {
- return queue[0].queueName
+ return queue.length > 0 ? queue[0].queueName : undefined
+})
+
+export const queueShuffledAtom = atom(get => {
+ const queue = get(_queue)
+ return queue.length > 0 ? queue[0].queueIndex !== undefined : false
+})
+
+export const orderedQueueAtom = atom(get => {
+ const queue = get(_queue)
+
+ if (queue.length === 0 || queue[0].queueIndex === undefined) {
+ return queue
}
- return undefined
+
+ return queue.map(t => t.queueIndex as number).map(i => queue[i])
})
const _progress = atom