Skip to content

Commit d8a3966

Browse files
committed
Add: add album to queue #93
1 parent a4e5d29 commit d8a3966

5 files changed

Lines changed: 41 additions & 9 deletions

File tree

app/components/options/OptionsAlbum.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { useTranslation } from 'react-i18next'
66
import { ConfigContext } from '~/contexts/config'
77
import { getApi, getApiNetworkFirst } from '~/utils/api'
88
import { urlStream } from '~/utils/url'
9-
import { playSong, downloadSong } from '~/utils/player'
10-
import { SongDispatchContext } from '~/contexts/song'
9+
import { playSong, downloadSong, addToQueue } from '~/utils/player'
10+
import { SongContext, SongDispatchContext } from '~/contexts/song'
1111
import { SettingsContext } from '~/contexts/settings'
1212
import OptionsPopup from '~/components/popup/OptionsPopup'
1313

@@ -18,6 +18,7 @@ const OptionsAlbum = ({ album, isOpen, onClose }) => {
1818
const refOption = React.useRef()
1919
const settings = React.useContext(SettingsContext)
2020
const songDispatch = React.useContext(SongDispatchContext)
21+
const song = React.useContext(SongContext)
2122

2223
const playSimilarSongs = () => {
2324
getApiNetworkFirst(config, 'getSimilarSongs', `id=${album.id}&count=50`)
@@ -44,6 +45,17 @@ const OptionsAlbum = ({ album, isOpen, onClose }) => {
4445
icon: 'play',
4546
onPress: playSimilarSongs
4647
},
48+
{
49+
name: t('Add to queue'),
50+
icon: 'list-ul',
51+
onPress: () => {
52+
refOption.current.close()
53+
for (const song of album.song) {
54+
addToQueue(songDispatch, song)
55+
}
56+
},
57+
hidden: !song.queue?.length
58+
},
4759
{
4860
name: t('Cache all songs'),
4961
icon: 'cloud-download',

app/components/options/OptionsAlbums.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import { useTranslation } from 'react-i18next'
66
import { getApi } from '~/utils/api'
77
import { ConfigContext } from '~/contexts/config'
88
import OptionsPopup from '~/components/popup/OptionsPopup'
9-
import { playSong } from '~/utils/player'
10-
import { SongDispatchContext } from '~/contexts/song'
11-
import { getApiNetworkFirst } from '~/utils/api'
9+
import { playSong, addToQueue } from '~/utils/player'
10+
import { SongContext, SongDispatchContext } from '~/contexts/song'
11+
import { getApiNetworkFirst, getApiCacheFirst } from '~/utils/api'
1212

1313
const OptionsAlbums = ({ albums, indexOptions, setIndexOptions }) => {
1414
const { t } = useTranslation()
15+
const song = React.useContext(SongContext)
1516
const navigation = useNavigation()
1617
const config = React.useContext(ConfigContext)
1718
const refOption = React.useRef()
@@ -38,6 +39,23 @@ const OptionsAlbums = ({ albums, indexOptions, setIndexOptions }) => {
3839
icon: 'play',
3940
onPress: playSimilarSongs
4041
},
42+
{
43+
name: t('Add to queue'),
44+
icon: 'list-ul',
45+
onPress: () => {
46+
refOption.current.close()
47+
getApiCacheFirst(config, 'getAlbum', { id: albums[indexOptions].id })
48+
.then((json) => {
49+
if (json.album?.song?.length) {
50+
for (const song of json.album.song) {
51+
addToQueue(songDispatch, song)
52+
}
53+
}
54+
})
55+
.catch(() => { })
56+
},
57+
hidden: !song.queue?.length
58+
},
4159
{
4260
name: t('Go to artist'),
4361
icon: 'user',

app/components/options/OptionsSongsList.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ const OptionsSongsList = ({ songs, indexOptions, setIndexOptions, onUpdate = ()
4747
}
4848

4949
const addQueue = () => {
50-
if (song.queue) addToQueue(config, songDispatch, songs[indexOptions])
50+
if (song.queue) addToQueue(songDispatch, songs[indexOptions])
5151
else playSong(config, songDispatch, [songs[indexOptions]], 0)
5252
refOption.current.close()
5353
}
5454

5555
const playNext = () => {
5656
if (song.queue) {
57-
addToQueue(config, songDispatch, songs[indexOptions], song.index + 1)
57+
addToQueue(songDispatch, songs[indexOptions], song.index + 1)
5858
} else {
5959
playSong(config, songDispatch, [songs[indexOptions]], 0)
6060
}

app/utils/player.native.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ export const removeFromQueue = async (songDispatch, index) => {
278278
songDispatch({ type: 'removeFromQueue', index })
279279
}
280280

281-
export const addToQueue = async (config, songDispatch, track, index = null) => {
281+
// when index is null, add to the end of the queue
282+
export const addToQueue = (songDispatch, track, index = null) => {
282283
songDispatch({ type: 'addToQueue', track, index })
283284
}
284285

app/utils/player.web.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ export const removeFromQueue = async (songDispatch, index) => {
332332
songDispatch({ type: 'removeFromQueue', index })
333333
}
334334

335-
export const addToQueue = async (_config, songDispatch, track, index = null) => {
335+
// when index is null, add to the end of the queue
336+
export const addToQueue = (songDispatch, track, index = null) => {
336337
songDispatch({ type: 'addToQueue', track, index })
337338
}
338339

0 commit comments

Comments
 (0)