Files
jellyfin-audio-player/src/screens/Music/stacks/components/TrackListView.tsx

298 lines
14 KiB
TypeScript
Raw Normal View History

import React, { PropsWithChildren, useCallback, useMemo } from 'react';
import { Platform, RefreshControl, StyleSheet, View } from 'react-native';
import { useGetImage } from '@/utility/JellyfinApi/lib';
2022-01-01 19:09:21 +01:00
import styled, { css } from 'styled-components/native';
import { useNavigation } from '@react-navigation/native';
2023-06-19 22:26:41 +02:00
import { useAppDispatch, useTypedSelector } from '@/store';
import TouchableHandler from '@/components/TouchableHandler';
import useCurrentTrack from '@/utility/useCurrentTrack';
2023-06-19 23:03:17 +02:00
import Play from '@/assets/icons/play.svg';
import Shuffle from '@/assets/icons/shuffle.svg';
2023-06-19 22:26:41 +02:00
import useDefaultStyles from '@/components/Colors';
import usePlayTracks from '@/utility/usePlayTracks';
import { WrappableButtonRow, WrappableButton } from '@/components/WrappableButtonRow';
2023-06-19 23:03:17 +02:00
import { NavigationProp } from '@/screens/types';
2023-06-19 22:26:41 +02:00
import DownloadIcon from '@/components/DownloadIcon';
2023-06-19 23:03:17 +02:00
import CloudDownArrow from '@/assets/icons/cloud-down-arrow.svg';
import Trash from '@/assets/icons/trash.svg';
2023-06-19 22:26:41 +02:00
import { queueTrackForDownload, removeDownloadedTrack } from '@/store/downloads/actions';
import { selectDownloadedTracks } from '@/store/downloads/selectors';
import { Header, SubHeader } from '@/components/Typography';
import { Text } from '@/components/Typography';
2023-06-19 22:26:41 +02:00
import CoverImage from '@/components/CoverImage';
import ticksToDuration from '@/utility/ticksToDuration';
import { t } from '@/localisation';
import { SafeScrollView, useNavigationOffsets } from '@/components/SafeNavigatorView';
import { groupBy } from 'lodash';
import Divider from '@/components/Divider';
2022-01-01 19:09:21 +01:00
const styles = StyleSheet.create({
2022-04-09 15:48:01 +02:00
index: {
marginRight: 12,
textAlign: 'right',
2022-01-01 19:09:21 +01:00
},
2022-04-09 15:48:01 +02:00
activeText: {
fontWeight: '500',
2022-01-01 19:09:21 +01:00
},
discContainer: {
flexDirection: 'row',
gap: 24,
alignItems: 'center',
marginBottom: 12,
}
2022-01-01 19:09:21 +01:00
});
2022-04-09 15:48:01 +02:00
const AlbumImageContainer = styled.View`
margin: 0 12px 24px 12px;
flex: 1;
align-items: center;
2022-01-01 19:09:21 +01:00
`;
const TrackContainer = styled.View<{ isPlaying: boolean, small?: boolean }>`
2022-04-09 15:48:01 +02:00
padding: 12px 4px;
2022-01-01 19:09:21 +01:00
flex-direction: row;
2022-04-09 15:48:01 +02:00
border-radius: 6px;
2023-04-22 23:41:41 +02:00
align-items: flex-start;
2022-01-01 19:09:21 +01:00
${props => props.isPlaying && css`
2022-04-09 15:48:01 +02:00
margin: 0 -12px;
padding: 12px 16px;
2022-01-01 19:09:21 +01:00
`}
${props => props.small && css`
padding: ${Platform.select({ ios: '8px 4px', android: '4px' })};
`}
2022-01-01 19:09:21 +01:00
`;
export interface TrackListViewProps extends PropsWithChildren<{}> {
2022-01-01 19:09:21 +01:00
title?: string;
artist?: string;
2024-02-08 22:11:43 +01:00
trackIds: string[];
2022-01-01 19:09:21 +01:00
entityId: string;
refresh: () => void;
playButtonText: string;
shuffleButtonText: string;
downloadButtonText: string;
deleteButtonText: string;
2022-01-01 19:09:21 +01:00
listNumberingStyle?: 'album' | 'index';
2023-04-22 23:41:41 +02:00
itemDisplayStyle?: 'album' | 'playlist';
2022-01-01 19:09:21 +01:00
}
const TrackListView: React.FC<TrackListViewProps> = ({
trackIds,
entityId,
title,
artist,
refresh,
playButtonText,
shuffleButtonText,
downloadButtonText,
deleteButtonText,
2022-01-01 19:09:21 +01:00
listNumberingStyle = 'album',
2023-04-22 23:41:41 +02:00
itemDisplayStyle = 'album',
children
2022-01-01 19:09:21 +01:00
}) => {
const defaultStyles = useDefaultStyles();
const offsets = useNavigationOffsets();
2022-01-01 19:09:21 +01:00
// Retrieve state
const tracks = useTypedSelector((state) => state.music.tracks.entities);
const isLoading = useTypedSelector((state) => state.music.tracks.isLoading);
const downloadedTracks = useTypedSelector(selectDownloadedTracks(trackIds));
const totalDuration = useMemo(() => (
trackIds.reduce<number>((sum, trackId) => (
sum + (tracks[trackId]?.RunTimeTicks || 0)
), 0)
), [trackIds, tracks]);
2022-01-01 19:09:21 +01:00
// Split all tracks into trackgroups depending on their parent id (i.e. disc
// number).
const trackGroups: [string, string[]][] = useMemo(() => {
// GUARD: Only apply this rendering style for albums
if (listNumberingStyle !== 'album') {
return [['0', trackIds]];
}
const groups = groupBy(trackIds, (id) => tracks[id]?.ParentIndexNumber);
return Object.entries(groups);
}, [trackIds, tracks, listNumberingStyle]);
2022-01-01 19:09:21 +01:00
// Retrieve helpers
const getImage = useGetImage();
const playTracks = usePlayTracks();
const { track: currentTrack } = useCurrentTrack();
const navigation = useNavigation<NavigationProp>();
2022-05-18 22:10:06 +02:00
const dispatch = useAppDispatch();
2022-01-01 19:09:21 +01:00
// Visual helpers
const { indexWidth } = useMemo(() => {
// Retrieve the largest index in the current set of tracks
const largestIndex = trackIds.reduce((max, trackId, i) => {
// Retrieve the index for this trackid, depending on settings
const index = listNumberingStyle === 'index'
? i + 1
: tracks[trackId]?.IndexNumber;
// Check that the current index is larger than the current max.
return index > max ? index : max;
}, 0);
// Retrieve the number of digits in the largest index
const noDigits = largestIndex.toFixed(0).toString().length;
// Set a minWidth proportional to the largest amount of digits in an index
return StyleSheet.create({ indexWidth: { minWidth: noDigits * 8 } });
}, [trackIds, tracks, listNumberingStyle]);
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) => {
await playTracks(trackIds, { playIndex: index });
2022-01-01 19:09:21 +01:00
}, [playTracks, trackIds]);
const longPressTrack = useCallback((index: number) => {
navigation.navigate('TrackPopupMenu', { trackId: trackIds[index].toString() });
2022-01-01 19:09:21 +01:00
}, [navigation, trackIds]);
2022-01-02 02:28:52 +01:00
const downloadAllTracks = useCallback(() => {
trackIds.forEach((trackId) => dispatch(queueTrackForDownload(trackId)));
2022-01-02 02:28:52 +01:00
}, [dispatch, trackIds]);
const deleteAllTracks = useCallback(() => {
downloadedTracks.forEach((trackId) => dispatch(removeDownloadedTrack(trackId)));
}, [dispatch, downloadedTracks]);
2022-01-01 19:09:21 +01:00
return (
<SafeScrollView
2022-04-09 15:48:01 +02:00
style={defaultStyles.view}
2022-01-01 19:09:21 +01:00
refreshControl={
<RefreshControl refreshing={isLoading} onRefresh={refresh} progressViewOffset={offsets.top} />
2022-01-01 19:09:21 +01:00
}
>
<View style={{ padding: 24, paddingTop: 32, paddingBottom: 32 }}>
<AlbumImageContainer>
<CoverImage src={getImage(entityId)} />
</AlbumImageContainer>
<Header>{title}</Header>
<SubHeader>{artist}</SubHeader>
<WrappableButtonRow>
<WrappableButton title={playButtonText} icon={Play} onPress={playEntity} testID="play-album" />
<WrappableButton title={shuffleButtonText} icon={Shuffle} onPress={shuffleEntity} testID="shuffle-album" />
</WrappableButtonRow>
<View style={{ marginTop: 8 }}>
{trackGroups.map(([discNo, groupTrackIds]) => (
<View key={`disc_${discNo}`} style={{ marginBottom: 24 }}>
{trackGroups.length > 1 && (
<View style={styles.discContainer}>
<SubHeader>{t('disc')} {discNo}</SubHeader>
<Divider />
</View>
)}
{groupTrackIds.map((trackId, i) =>
<TouchableHandler
key={trackId}
id={i}
onPress={selectTrack}
onLongPress={longPressTrack}
testID={`play-track-${trackId}`}
2023-04-22 23:41:41 +02:00
>
<TrackContainer
isPlaying={currentTrack?.backendId === trackId || false}
style={[
defaultStyles.border,
currentTrack?.backendId === trackId ? defaultStyles.activeBackground : null
]}
2023-04-22 23:41:41 +02:00
>
<Text
style={[
styles.index,
defaultStyles.textQuarterOpacity,
currentTrack?.backendId === trackId && styles.activeText,
currentTrack?.backendId === trackId && defaultStyles.themeColorQuarterOpacity,
indexWidth,
]}
numberOfLines={1}
>
{listNumberingStyle === 'index'
? i + 1
: tracks[trackId]?.IndexNumber}
</Text>
<View style={{ flexShrink: 1 }}>
<Text
style={[
currentTrack?.backendId === trackId && styles.activeText,
currentTrack?.backendId === trackId && defaultStyles.themeColor,
{
flexShrink: 1,
marginRight: 4,
}
]}
numberOfLines={1}
>
{tracks[trackId]?.Name}
</Text>
{itemDisplayStyle === 'playlist' && (
<Text
style={[
currentTrack?.backendId === trackId && styles.activeText,
currentTrack?.backendId === trackId && defaultStyles.themeColor,
{
flexShrink: 1,
marginRight: 4,
opacity: currentTrack?.backendId === trackId ? 0.5 : 0.25,
}
]}
numberOfLines={1}
>
{tracks[trackId]?.Artists.join(', ')}
</Text>
)}
</View>
<View style={{ marginLeft: 'auto', flexDirection: 'row' }}>
<Text
style={[
{ marginRight: 12 },
defaultStyles.textQuarterOpacity,
currentTrack?.backendId === trackId && styles.activeText,
currentTrack?.backendId === trackId && defaultStyles.themeColorQuarterOpacity,
]}
numberOfLines={1}
>
{ticksToDuration(tracks[trackId]?.RunTimeTicks || 0)}
</Text>
<DownloadIcon
trackId={trackId}
fill={currentTrack?.backendId === trackId ? defaultStyles.themeColorQuarterOpacity.color : undefined}
/>
</View>
</TrackContainer>
</TouchableHandler>
)}
</View>
))}
<Text style={{ paddingTop: 24, paddingBottom: 12, textAlign: 'center', opacity: 0.5 }}>
{t('total-duration')}{': '}{ticksToDuration(totalDuration)}
</Text>
<WrappableButtonRow style={{ marginTop: 24 }}>
<WrappableButton
icon={CloudDownArrow}
title={downloadButtonText}
onPress={downloadAllTracks}
disabled={downloadedTracks.length === trackIds.length}
testID="download-album"
/>
<WrappableButton
icon={Trash}
title={deleteButtonText}
onPress={deleteAllTracks}
disabled={downloadedTracks.length === 0}
testID="delete-album"
/>
</WrappableButtonRow>
</View>
{children}
2022-01-01 19:09:21 +01:00
</View>
</SafeScrollView>
2022-01-01 19:09:21 +01:00
);
};
export default TrackListView;