Files
jellyfin-audio-player/src/screens/Albums/components/Album.tsx

87 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-06-17 14:58:04 +02:00
import React, { useCallback, useEffect } from 'react';
import { StackParams } from '../types';
2020-06-21 10:30:41 +02:00
import { Text, ScrollView, Dimensions, Button, RefreshControl } from 'react-native';
import { useGetImage } from 'utility/JellyfinApi';
2020-06-16 17:51:51 +02:00
import styled from 'styled-components/native';
2020-06-17 14:58:04 +02:00
import { useRoute, RouteProp } from '@react-navigation/native';
2020-06-20 22:49:51 +02:00
import FastImage from 'react-native-fast-image';
2020-06-17 14:58:04 +02:00
import { useDispatch } from 'react-redux';
2020-06-20 23:10:19 +02:00
import { differenceInDays } from 'date-fns';
2020-06-21 10:30:41 +02:00
import { useTypedSelector } from 'store';
import { fetchTracksByAlbum } from 'store/music/actions';
import { ALBUM_CACHE_AMOUNT_OF_DAYS } from 'CONSTANTS';
import usePlayAlbum from 'utility/usePlayAlbum';
import usePlayTrack from 'utility/usePlayTrack';
import TouchableHandler from 'components/TouchableHandler';
2020-06-16 17:51:51 +02:00
2020-06-17 14:58:04 +02:00
type Route = RouteProp<StackParams, 'Album'>;
2020-06-16 17:51:51 +02:00
const Screen = Dimensions.get('screen');
2020-06-20 22:49:51 +02:00
const AlbumImage = styled(FastImage)`
2020-06-16 17:51:51 +02:00
border-radius: 10px;
width: ${Screen.width * 0.6}px;
height: ${Screen.width * 0.6}px;
margin: 10px auto;
`;
const TrackContainer = styled.View`
padding: 15px;
border-bottom-width: 1px;
border-bottom-color: #eee;
flex-direction: row;
`;
2020-06-17 14:58:04 +02:00
const Album: React.FC = () => {
// Retrieve state
const { params: { id } } = useRoute<Route>();
const tracks = useTypedSelector((state) => state.music.tracks.entities);
const album = useTypedSelector((state) => state.music.albums.entities[id]);
2020-06-17 15:09:19 +02:00
const isLoading = useTypedSelector((state) => state.music.tracks.isLoading);
2020-06-16 17:51:51 +02:00
2020-06-17 14:58:04 +02:00
// Retrieve helpers
const dispatch = useDispatch();
const getImage = useGetImage();
2020-06-21 10:30:41 +02:00
const playAlbum = usePlayAlbum();
2020-06-16 17:51:51 +02:00
2020-06-21 10:30:41 +02:00
// Setup callbacks
const selectAlbum = useCallback(() => { playAlbum(id); }, [playAlbum]);
const selectTrack = usePlayTrack();
2020-06-17 15:09:19 +02:00
const refresh = useCallback(() => { dispatch(fetchTracksByAlbum(id)); }, [id]);
2020-06-16 17:51:51 +02:00
2020-06-17 14:58:04 +02:00
// Retrieve album tracks on load
2020-06-20 23:10:19 +02:00
useEffect(() => {
if (!album?.lastRefreshed || differenceInDays(album.lastRefreshed, new Date()) > ALBUM_CACHE_AMOUNT_OF_DAYS) {
refresh();
}
}, []);
2020-06-17 14:58:04 +02:00
2020-06-21 10:30:41 +02:00
// GUARD: If there is no album, we cannot render a thing
if (!album) {
return null;
}
2020-06-17 14:58:04 +02:00
return (
2020-06-17 15:09:19 +02:00
<ScrollView
style={{ backgroundColor: '#f6f6f6', padding: 20, paddingBottom: 50 }}
refreshControl={
<RefreshControl refreshing={isLoading} onRefresh={refresh} />
}
>
2020-06-21 10:30:41 +02:00
<AlbumImage source={{ uri: getImage(album?.Id) }} />
2020-06-17 14:58:04 +02:00
<Text style={{ fontSize: 36, fontWeight: 'bold' }} >{album?.Name}</Text>
<Text style={{ fontSize: 24, opacity: 0.5, marginBottom: 24 }}>{album?.AlbumArtist}</Text>
2020-06-21 10:30:41 +02:00
<Button title="Play Album" onPress={selectAlbum} />
2020-06-17 14:58:04 +02:00
{album?.Tracks?.length ? album.Tracks.map((trackId) =>
2020-06-21 10:30:41 +02:00
<TouchableHandler key={trackId} id={trackId} onPress={selectTrack}>
<TrackContainer>
<Text style={{ width: 20, opacity: 0.5, marginRight: 5 }}>{tracks[trackId]?.IndexNumber}</Text>
<Text>{tracks[trackId]?.Name}</Text>
</TrackContainer>
</TouchableHandler>
2020-06-17 14:58:04 +02:00
) : undefined}
</ScrollView>
);
};
2020-06-16 17:51:51 +02:00
2020-06-17 14:58:04 +02:00
export default Album;