2022-12-17 12:09:22 +00:00
|
|
|
import { differenceBy, orderBy, sampleSize, take, throttle } from 'lodash'
|
2022-07-07 18:05:46 +00:00
|
|
|
import isMobile from 'ismobilejs'
|
2022-12-17 12:09:22 +00:00
|
|
|
import { computed, provide, reactive, Ref, ref } from 'vue'
|
2022-04-24 08:50:45 +00:00
|
|
|
import { playbackService } from '@/services'
|
2022-04-25 16:38:33 +00:00
|
|
|
import { queueStore, songStore } from '@/stores'
|
2022-11-18 18:44:20 +00:00
|
|
|
import { eventBus, provideReadonly } from '@/utils'
|
|
|
|
import { useRouter } from '@/composables'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-12-17 12:09:22 +00:00
|
|
|
import {
|
|
|
|
SelectedSongsKey,
|
|
|
|
SongListConfigKey,
|
2023-08-20 22:35:58 +00:00
|
|
|
SongListFilterKeywordsKey,
|
2022-12-17 12:09:22 +00:00
|
|
|
SongListSortFieldKey,
|
|
|
|
SongListSortOrderKey,
|
|
|
|
SongsKey
|
|
|
|
} from '@/symbols'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-07-07 18:05:46 +00:00
|
|
|
import ControlsToggle from '@/components/ui/ScreenControlsToggle.vue'
|
|
|
|
import SongList from '@/components/song/SongList.vue'
|
2022-07-16 09:52:39 +00:00
|
|
|
import ThumbnailStack from '@/components/ui/ThumbnailStack.vue'
|
2022-07-07 18:05:46 +00:00
|
|
|
|
2022-12-07 00:44:42 +00:00
|
|
|
export const useSongList = (
|
|
|
|
songs: Ref<Song[]>,
|
2024-01-22 23:11:13 +00:00
|
|
|
config: Partial<SongListConfig> = { sortable: true, reorderable: false, collaborative: false }
|
2022-12-07 00:44:42 +00:00
|
|
|
) => {
|
2022-12-17 12:09:22 +00:00
|
|
|
const filterKeywords = ref('')
|
2022-11-17 16:30:38 +00:00
|
|
|
config = reactive(config)
|
2024-01-22 23:11:13 +00:00
|
|
|
const { isCurrentScreen, go } = useRouter()
|
2022-10-08 10:54:25 +00:00
|
|
|
|
2022-04-23 21:24:02 +00:00
|
|
|
const songList = ref<InstanceType<typeof SongList>>()
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-23 21:24:02 +00:00
|
|
|
const isPhone = isMobile.phone
|
2022-04-15 14:24:30 +00:00
|
|
|
const selectedSongs = ref<Song[]>([])
|
|
|
|
const showingControls = ref(false)
|
2022-07-17 09:07:46 +00:00
|
|
|
const headerLayout = ref<ScreenHeaderLayout>('expanded')
|
2022-07-16 09:52:39 +00:00
|
|
|
|
|
|
|
const onScrollBreakpoint = (direction: 'up' | 'down') => {
|
|
|
|
headerLayout.value = direction === 'down' ? 'collapsed' : 'expanded'
|
|
|
|
}
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-23 21:24:02 +00:00
|
|
|
const duration = computed(() => songStore.getFormattedLength(songs.value))
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-07-16 09:52:39 +00:00
|
|
|
const thumbnails = computed(() => {
|
2024-01-24 22:39:47 +00:00
|
|
|
const songsWithCover = songs.value.filter(({ album_cover }) => album_cover)
|
|
|
|
const sampleCovers = sampleSize(songsWithCover, 20).map(({ album_cover }) => album_cover)
|
2022-07-16 09:52:39 +00:00
|
|
|
return take(Array.from(new Set(sampleCovers)), 4)
|
|
|
|
})
|
|
|
|
|
2022-12-02 16:17:37 +00:00
|
|
|
const getSongsToPlay = (): Song[] => songList.value!.getAllSongsWithSort()
|
2022-10-21 20:06:43 +00:00
|
|
|
|
|
|
|
const playAll = (shuffle: boolean) => {
|
|
|
|
playbackService.queueAndPlay(getSongsToPlay(), shuffle)
|
2022-11-18 18:44:20 +00:00
|
|
|
go('queue')
|
2022-10-21 20:06:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 08:50:45 +00:00
|
|
|
const playSelected = (shuffle: boolean) => playbackService.queueAndPlay(selectedSongs.value, shuffle)
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-12-17 12:09:22 +00:00
|
|
|
const applyFilter = throttle((keywords: string) => (filterKeywords.value = keywords), 200)
|
|
|
|
|
2022-04-21 18:12:11 +00:00
|
|
|
const onPressEnter = async (event: KeyboardEvent) => {
|
|
|
|
if (selectedSongs.value.length === 1) {
|
|
|
|
queueStore.queueIfNotQueued(selectedSongs.value[0])
|
2022-04-24 08:50:45 +00:00
|
|
|
await playbackService.play(selectedSongs.value[0])
|
2022-04-21 18:12:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// • Only Enter: Queue songs to bottom
|
|
|
|
// • Shift+Enter: Queues song to top
|
|
|
|
// • Cmd/Ctrl+Enter: Queues song to bottom and play the first selected song
|
|
|
|
// • Cmd/Ctrl+Shift+Enter: Queue songs to top and play the first queued song
|
|
|
|
event.shiftKey ? queueStore.queueToTop(selectedSongs.value) : queueStore.queue(selectedSongs.value)
|
|
|
|
|
|
|
|
if (event.ctrlKey || event.metaKey) {
|
2022-04-24 08:50:45 +00:00
|
|
|
await playbackService.play(selectedSongs.value[0])
|
2022-04-21 18:12:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 18:44:20 +00:00
|
|
|
go('queue')
|
2022-04-21 18:12:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 15:09:20 +00:00
|
|
|
const sortField = ref<SongListSortField | null>(((): SongListSortField | null => {
|
2022-11-12 21:38:31 +00:00
|
|
|
if (!config.sortable) return null
|
2022-11-18 18:44:20 +00:00
|
|
|
if (isCurrentScreen('Artist', 'Album')) return 'track'
|
|
|
|
if (isCurrentScreen('Search.Songs', 'Queue', 'RecentlyPlayed')) return null
|
2022-11-12 21:38:31 +00:00
|
|
|
return 'title'
|
2022-07-05 15:09:20 +00:00
|
|
|
})())
|
2022-06-10 10:47:46 +00:00
|
|
|
|
2022-07-05 15:09:20 +00:00
|
|
|
const sortOrder = ref<SortOrder>('asc')
|
2022-06-10 10:47:46 +00:00
|
|
|
|
2022-07-05 15:09:20 +00:00
|
|
|
const sort = (by: SongListSortField | null = sortField.value, order: SortOrder = sortOrder.value) => {
|
2022-11-12 21:38:31 +00:00
|
|
|
if (!config.sortable) return
|
2022-07-05 15:09:20 +00:00
|
|
|
if (!by) return
|
|
|
|
|
|
|
|
sortField.value = by
|
|
|
|
sortOrder.value = order
|
|
|
|
|
|
|
|
let sortFields: SongListSortField[] = [by]
|
|
|
|
|
|
|
|
if (by === 'track') {
|
2022-09-16 12:10:09 +00:00
|
|
|
sortFields = ['disc', 'track', 'title']
|
2022-07-05 15:09:20 +00:00
|
|
|
} else if (by === 'album_name') {
|
2022-09-16 12:10:09 +00:00
|
|
|
sortFields.push('artist_name', 'disc', 'track', 'title')
|
2022-07-05 15:09:20 +00:00
|
|
|
} else if (by === 'artist_name') {
|
2022-09-16 12:10:09 +00:00
|
|
|
sortFields.push('album_name', 'disc', 'track', 'title')
|
2022-06-10 10:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-05 15:09:20 +00:00
|
|
|
songs.value = orderBy(songs.value, sortFields, order)
|
2022-06-10 10:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 15:52:38 +00:00
|
|
|
eventBus.on('SONGS_DELETED', deletedSongs => (songs.value = differenceBy(songs.value, deletedSongs, 'id')))
|
2022-09-15 09:07:25 +00:00
|
|
|
|
2022-07-20 08:00:02 +00:00
|
|
|
provideReadonly(SongsKey, songs, false)
|
|
|
|
provideReadonly(SelectedSongsKey, selectedSongs, false)
|
2022-11-17 16:30:38 +00:00
|
|
|
provideReadonly(SongListConfigKey, config)
|
2022-07-20 08:00:02 +00:00
|
|
|
provideReadonly(SongListSortFieldKey, sortField)
|
|
|
|
provideReadonly(SongListSortOrderKey, sortOrder)
|
2022-06-10 10:47:46 +00:00
|
|
|
|
2022-12-17 12:09:22 +00:00
|
|
|
provide(SongListFilterKeywordsKey, filterKeywords)
|
|
|
|
|
2022-04-15 14:24:30 +00:00
|
|
|
return {
|
|
|
|
SongList,
|
2022-06-10 10:47:46 +00:00
|
|
|
ControlsToggle,
|
2022-07-16 09:52:39 +00:00
|
|
|
ThumbnailStack,
|
2022-04-21 16:06:45 +00:00
|
|
|
songs,
|
2024-01-18 11:13:05 +00:00
|
|
|
config,
|
2022-07-16 09:52:39 +00:00
|
|
|
headerLayout,
|
2022-07-05 15:09:20 +00:00
|
|
|
sortField,
|
|
|
|
sortOrder,
|
2022-04-23 21:24:02 +00:00
|
|
|
duration,
|
2022-07-16 09:52:39 +00:00
|
|
|
thumbnails,
|
2022-04-15 14:24:30 +00:00
|
|
|
songList,
|
|
|
|
selectedSongs,
|
|
|
|
showingControls,
|
|
|
|
isPhone,
|
2022-04-21 18:12:11 +00:00
|
|
|
onPressEnter,
|
2022-04-15 14:24:30 +00:00
|
|
|
playAll,
|
|
|
|
playSelected,
|
2022-12-17 12:09:22 +00:00
|
|
|
applyFilter,
|
2022-07-16 09:52:39 +00:00
|
|
|
onScrollBreakpoint,
|
2022-06-10 10:47:46 +00:00
|
|
|
sort
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
}
|