2022-01-01 19:09:21 +01:00
|
|
|
import React, { useCallback } from 'react';
|
|
|
|
|
import { Text, ScrollView, Dimensions, RefreshControl, StyleSheet, View } from 'react-native';
|
|
|
|
|
import { useGetImage } from 'utility/JellyfinApi';
|
|
|
|
|
import styled, { css } from 'styled-components/native';
|
|
|
|
|
import { useNavigation } from '@react-navigation/native';
|
|
|
|
|
import FastImage from 'react-native-fast-image';
|
|
|
|
|
import { useTypedSelector } from 'store';
|
|
|
|
|
import { THEME_COLOR } from 'CONSTANTS';
|
|
|
|
|
import TouchableHandler from 'components/TouchableHandler';
|
|
|
|
|
import useCurrentTrack from 'utility/useCurrentTrack';
|
|
|
|
|
import TrackPlayer from 'react-native-track-player';
|
|
|
|
|
import Play from 'assets/play.svg';
|
|
|
|
|
import Shuffle from 'assets/shuffle.svg';
|
|
|
|
|
import useDefaultStyles from 'components/Colors';
|
|
|
|
|
import usePlayTracks from 'utility/usePlayTracks';
|
|
|
|
|
import { EntityId } from '@reduxjs/toolkit';
|
2022-01-01 21:55:32 +01:00
|
|
|
import { WrappableButtonRow, WrappableButton } from 'components/WrappableButtonRow';
|
2022-01-01 22:36:05 +01:00
|
|
|
import { MusicNavigationProp } from 'screens/Music/types';
|
2022-01-02 02:28:52 +01:00
|
|
|
import DownloadIcon from 'components/DownloadIcon';
|
|
|
|
|
import CloudDownArrow from 'assets/cloud-down-arrow.svg';
|
2022-01-02 17:46:50 +01:00
|
|
|
import Trash from 'assets/trash.svg';
|
2022-01-02 02:28:52 +01:00
|
|
|
import { useDispatch } from 'react-redux';
|
2022-01-02 17:46:50 +01:00
|
|
|
import { downloadTrack, removeDownloadedTrack } from 'store/downloads/actions';
|
|
|
|
|
import { selectDownloadedTracks } from 'store/downloads/selectors';
|
2022-01-01 19:09:21 +01:00
|
|
|
|
|
|
|
|
const Screen = Dimensions.get('screen');
|
|
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
|
name: {
|
|
|
|
|
fontSize: 36,
|
|
|
|
|
fontWeight: 'bold'
|
|
|
|
|
},
|
|
|
|
|
artist: {
|
|
|
|
|
fontSize: 24,
|
|
|
|
|
opacity: 0.5,
|
2022-01-01 21:55:32 +01:00
|
|
|
marginBottom: 12
|
2022-01-01 19:09:21 +01:00
|
|
|
},
|
|
|
|
|
index: {
|
|
|
|
|
width: 20,
|
|
|
|
|
opacity: 0.5,
|
|
|
|
|
marginRight: 5
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const AlbumImage = styled(FastImage)`
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
width: ${Screen.width * 0.6}px;
|
|
|
|
|
height: ${Screen.width * 0.6}px;
|
|
|
|
|
margin: 10px auto;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const TrackContainer = styled.View<{isPlaying: boolean}>`
|
2022-01-02 02:28:52 +01:00
|
|
|
padding: 15px 4px;
|
2022-01-01 19:09:21 +01:00
|
|
|
border-bottom-width: 1px;
|
|
|
|
|
flex-direction: row;
|
|
|
|
|
|
|
|
|
|
${props => props.isPlaying && css`
|
|
|
|
|
margin: 0 -20px;
|
2022-01-02 02:28:52 +01:00
|
|
|
padding: 15px 24px;
|
2022-01-01 19:09:21 +01:00
|
|
|
`}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
interface TrackListViewProps {
|
|
|
|
|
title?: string;
|
|
|
|
|
artist?: string;
|
|
|
|
|
trackIds: EntityId[];
|
|
|
|
|
entityId: string;
|
|
|
|
|
refresh: () => void;
|
|
|
|
|
playButtonText: string;
|
|
|
|
|
shuffleButtonText: string;
|
2022-01-02 17:46:50 +01:00
|
|
|
downloadButtonText: string;
|
|
|
|
|
deleteButtonText: string;
|
2022-01-01 19:09:21 +01:00
|
|
|
listNumberingStyle?: 'album' | 'index';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const TrackListView: React.FC<TrackListViewProps> = ({
|
|
|
|
|
trackIds,
|
|
|
|
|
entityId,
|
|
|
|
|
title,
|
|
|
|
|
artist,
|
|
|
|
|
refresh,
|
|
|
|
|
playButtonText,
|
|
|
|
|
shuffleButtonText,
|
2022-01-02 17:46:50 +01:00
|
|
|
downloadButtonText,
|
|
|
|
|
deleteButtonText,
|
2022-01-01 19:09:21 +01:00
|
|
|
listNumberingStyle = 'album',
|
|
|
|
|
}) => {
|
|
|
|
|
const defaultStyles = useDefaultStyles();
|
|
|
|
|
|
|
|
|
|
// Retrieve state
|
|
|
|
|
const tracks = useTypedSelector((state) => state.music.tracks.entities);
|
|
|
|
|
const isLoading = useTypedSelector((state) => state.music.tracks.isLoading);
|
2022-01-02 17:46:50 +01:00
|
|
|
const downloadedTracks = useTypedSelector(selectDownloadedTracks(trackIds));
|
2022-01-01 19:09:21 +01:00
|
|
|
|
|
|
|
|
// Retrieve helpers
|
|
|
|
|
const getImage = useGetImage();
|
|
|
|
|
const playTracks = usePlayTracks();
|
|
|
|
|
const { track: currentTrack } = useCurrentTrack();
|
2022-01-01 22:36:05 +01:00
|
|
|
const navigation = useNavigation<MusicNavigationProp>();
|
2022-01-02 02:28:52 +01:00
|
|
|
const dispatch = useDispatch();
|
2022-01-01 19:09:21 +01:00
|
|
|
|
|
|
|
|
// Setup callbacks
|
|
|
|
|
const playEntity = useCallback(() => { playTracks(trackIds); }, [playTracks, trackIds]);
|
2022-01-02 02:28:52 +01:00
|
|
|
const shuffleEntity = useCallback(() => { playTracks(trackIds, { shuffle: true }); }, [playTracks, trackIds]);
|
2022-01-01 19:09:21 +01:00
|
|
|
const selectTrack = useCallback(async (index: number) => {
|
2022-01-02 02:28:52 +01:00
|
|
|
await playTracks(trackIds, { play: false });
|
2022-01-01 19:09:21 +01:00
|
|
|
await TrackPlayer.skip(index);
|
|
|
|
|
await TrackPlayer.play();
|
|
|
|
|
}, [playTracks, trackIds]);
|
|
|
|
|
const longPressTrack = useCallback((index: number) => {
|
|
|
|
|
navigation.navigate('TrackPopupMenu', { trackId: trackIds[index] });
|
|
|
|
|
}, [navigation, trackIds]);
|
2022-01-02 02:28:52 +01:00
|
|
|
const downloadAllTracks = useCallback(() => {
|
|
|
|
|
trackIds.forEach((trackId) => dispatch(downloadTrack(trackId)));
|
|
|
|
|
}, [dispatch, trackIds]);
|
2022-01-02 17:46:50 +01:00
|
|
|
const deleteAllTracks = useCallback(() => {
|
|
|
|
|
downloadedTracks.forEach((trackId) => dispatch(removeDownloadedTrack(trackId)));
|
|
|
|
|
}, [dispatch, downloadedTracks]);
|
2022-01-01 19:09:21 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ScrollView
|
|
|
|
|
contentContainerStyle={{ padding: 20, paddingBottom: 50 }}
|
|
|
|
|
refreshControl={
|
|
|
|
|
<RefreshControl refreshing={isLoading} onRefresh={refresh} />
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<AlbumImage source={{ uri: getImage(entityId) }} style={defaultStyles.imageBackground} />
|
|
|
|
|
<Text style={[ defaultStyles.text, styles.name ]} >{title}</Text>
|
|
|
|
|
<Text style={[ defaultStyles.text, styles.artist ]}>{artist}</Text>
|
2022-01-01 21:55:32 +01:00
|
|
|
<WrappableButtonRow>
|
|
|
|
|
<WrappableButton title={playButtonText} icon={Play} onPress={playEntity} />
|
|
|
|
|
<WrappableButton title={shuffleButtonText} icon={Shuffle} onPress={shuffleEntity} />
|
|
|
|
|
</WrappableButtonRow>
|
|
|
|
|
<View style={{ marginTop: 8 }}>
|
2022-01-01 19:09:21 +01:00
|
|
|
{trackIds.map((trackId, i) =>
|
|
|
|
|
<TouchableHandler
|
|
|
|
|
key={trackId}
|
|
|
|
|
id={i}
|
|
|
|
|
onPress={selectTrack}
|
|
|
|
|
onLongPress={longPressTrack}
|
|
|
|
|
>
|
2022-01-02 23:02:54 +01:00
|
|
|
<TrackContainer
|
|
|
|
|
isPlaying={currentTrack?.backendId === trackId || false}
|
|
|
|
|
style={[defaultStyles.border, currentTrack?.backendId === trackId || false ? defaultStyles.activeBackground : null ]}
|
|
|
|
|
>
|
2022-01-01 19:09:21 +01:00
|
|
|
<Text
|
|
|
|
|
style={[
|
|
|
|
|
defaultStyles.text,
|
|
|
|
|
styles.index,
|
|
|
|
|
currentTrack?.backendId === trackId && {
|
|
|
|
|
color: THEME_COLOR,
|
|
|
|
|
opacity: 1
|
|
|
|
|
}
|
|
|
|
|
]}
|
|
|
|
|
>
|
|
|
|
|
{listNumberingStyle === 'index'
|
|
|
|
|
? i + 1
|
|
|
|
|
: tracks[trackId]?.IndexNumber}
|
|
|
|
|
</Text>
|
|
|
|
|
<Text
|
|
|
|
|
style={currentTrack?.backendId === trackId
|
|
|
|
|
? { color: THEME_COLOR, fontWeight: '700' }
|
|
|
|
|
: defaultStyles.text
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
{tracks[trackId]?.Name}
|
|
|
|
|
</Text>
|
2022-01-02 02:28:52 +01:00
|
|
|
<View style={{ marginLeft: 'auto' }}>
|
|
|
|
|
<DownloadIcon trackId={trackId} />
|
|
|
|
|
</View>
|
2022-01-01 19:09:21 +01:00
|
|
|
</TrackContainer>
|
|
|
|
|
</TouchableHandler>
|
|
|
|
|
)}
|
2022-01-02 17:46:50 +01:00
|
|
|
<WrappableButtonRow style={{ marginTop: 24 }}>
|
|
|
|
|
<WrappableButton
|
|
|
|
|
icon={CloudDownArrow}
|
|
|
|
|
title={downloadButtonText}
|
|
|
|
|
onPress={downloadAllTracks}
|
|
|
|
|
disabled={downloadedTracks.length === trackIds.length}
|
|
|
|
|
/>
|
|
|
|
|
<WrappableButton
|
|
|
|
|
icon={Trash}
|
|
|
|
|
title={deleteButtonText}
|
|
|
|
|
onPress={deleteAllTracks}
|
|
|
|
|
disabled={downloadedTracks.length === 0}
|
|
|
|
|
/>
|
|
|
|
|
</WrappableButtonRow>
|
2022-01-01 19:09:21 +01:00
|
|
|
</View>
|
|
|
|
|
</ScrollView>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default TrackListView;
|