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

103 lines
3.9 KiB
TypeScript
Raw Normal View History

import React, { useCallback, useEffect } from 'react';
import { useRoute, RouteProp, useNavigation } from '@react-navigation/native';
2023-06-19 22:26:41 +02:00
import { useAppDispatch, useTypedSelector } from '@/store';
2022-01-01 19:09:21 +01:00
import TrackListView from './components/TrackListView';
2023-06-19 22:26:41 +02:00
import { fetchAlbum, fetchTracksByAlbum } from '@/store/music/actions';
2022-01-01 19:09:21 +01:00
import { differenceInDays } from 'date-fns';
2023-06-19 22:26:41 +02:00
import { ALBUM_CACHE_AMOUNT_OF_DAYS } from '@/CONSTANTS';
import { t } from '@/localisation';
import { NavigationProp, StackParams } from 'screens/types';
2023-06-19 22:26:41 +02:00
import { SubHeader, Text } from '@/components/Typography';
import { ScrollView } from 'react-native-gesture-handler';
2023-06-19 22:26:41 +02:00
import { useGetImage } from '@/utility/JellyfinApi';
import styled from 'styled-components';
import { Dimensions, Pressable } from 'react-native';
import AlbumImage from './components/AlbumImage';
2020-06-16 17:51:51 +02:00
2023-04-22 23:31:37 +02:00
type Route = RouteProp<StackParams, 'Album'>;
2020-06-16 17:51:51 +02:00
const Screen = Dimensions.get('screen');
const Cover = styled(AlbumImage)`
height: ${Screen.width / 2.8}px;
width: ${Screen.width / 2.8}px;
border-radius: 12px;
2023-04-23 01:29:59 +02:00
margin-bottom: 8px;
`;
function SimilarAlbum({ id }: { id: string }) {
const navigation = useNavigation<NavigationProp>();
const getImage = useGetImage();
const album = useTypedSelector((state) => state.music.albums.entities[id]);
const handlePress = useCallback(() => {
album && navigation.push('Album', { id, album });
}, [id, album, navigation]);
return (
<Pressable
style={({ pressed }) => ({
opacity: pressed ? 0.5 : 1.0,
width: Screen.width / 2.8,
marginRight: 12
})}
onPress={handlePress}
>
<Cover key={id} source={{ uri: getImage(id) }} />
2023-04-23 01:29:59 +02:00
<Text numberOfLines={1} style={{ fontSize: 13, marginBottom: 2 }}>{album?.Name}</Text>
<Text numberOfLines={1} style={{ opacity: 0.5, fontSize: 13 }}>{album?.Artists.join(', ')}</Text>
</Pressable>
);
}
2020-06-17 14:58:04 +02:00
const Album: React.FC = () => {
const { params: { id } } = useRoute<Route>();
2022-05-18 22:10:06 +02:00
const dispatch = useAppDispatch();
2020-06-16 17:51:51 +02:00
2022-01-01 19:09:21 +01:00
// Retrieve the album data from the store
const album = useTypedSelector((state) => state.music.albums.entities[id]);
const albumTracks = useTypedSelector((state) => state.music.tracks.byAlbum[id]);
2020-06-16 17:51:51 +02:00
2022-01-01 19:09:21 +01:00
// Define a function for refreshing this entity
const refresh = useCallback(() => {
dispatch(fetchTracksByAlbum(id));
dispatch(fetchAlbum(id));
}, [id, dispatch]);
2020-06-16 17:51:51 +02:00
2022-01-01 19:09:21 +01:00
// Auto-fetch the track data periodically
2020-06-20 23:10:19 +02:00
useEffect(() => {
if (!album?.lastRefreshed || differenceInDays(album?.lastRefreshed, new Date()) > ALBUM_CACHE_AMOUNT_OF_DAYS) {
2020-06-20 23:10:19 +02:00
refresh();
}
}, [album?.lastRefreshed, refresh]);
2020-06-17 14:58:04 +02:00
return (
2022-01-01 19:09:21 +01:00
<TrackListView
trackIds={albumTracks || []}
title={album?.Name}
artist={album?.AlbumArtist}
entityId={id}
refresh={refresh}
playButtonText={t('play-album')}
shuffleButtonText={t('shuffle-album')}
downloadButtonText={t('download-album')}
deleteButtonText={t('delete-album')}
>
{album?.Overview ? (
<Text style={{ opacity: 0.5, lineHeight: 20, fontSize: 12, paddingBottom: 24 }}>{album?.Overview}</Text>
) : null}
{album?.Similar?.length ? (
<>
2023-04-23 22:06:39 +02:00
<SubHeader>{t('similar-albums')}</SubHeader>
<ScrollView horizontal style={{ marginLeft: -24, marginRight: -24, marginTop: 8 }} contentContainerStyle={{ paddingHorizontal: 24 }} showsHorizontalScrollIndicator={false}>
{album.Similar.map((id) => (
<SimilarAlbum id={id} key={id} />
))}
</ScrollView>
</>
) : null}
</TrackListView>
2020-06-17 14:58:04 +02:00
);
};
2020-06-16 17:51:51 +02:00
2020-06-17 14:58:04 +02:00
export default Album;