2020-06-17 14:58:04 +02:00
|
|
|
import React, { useCallback, useEffect } from 'react';
|
|
|
|
|
import { StackParams } from '../types';
|
2020-07-26 14:45:32 +02:00
|
|
|
import { Text, ScrollView, Dimensions, Button, RefreshControl, StyleSheet } from 'react-native';
|
2020-06-21 10:30:41 +02:00
|
|
|
import { useGetImage } from 'utility/JellyfinApi';
|
2020-07-07 14:40:03 +02:00
|
|
|
import styled, { css } 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';
|
2020-07-10 15:25:32 +02:00
|
|
|
import { ALBUM_CACHE_AMOUNT_OF_DAYS, THEME_COLOR } from 'CONSTANTS';
|
2020-06-21 10:30:41 +02:00
|
|
|
import usePlayAlbum from 'utility/usePlayAlbum';
|
|
|
|
|
import usePlayTrack from 'utility/usePlayTrack';
|
|
|
|
|
import TouchableHandler from 'components/TouchableHandler';
|
2020-07-07 14:40:03 +02:00
|
|
|
import useCurrentTrack from 'utility/useCurrentTrack';
|
2020-07-26 14:45:32 +02:00
|
|
|
import { colors } from 'components/Colors';
|
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-07-26 14:45:32 +02:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
|
name: {
|
|
|
|
|
...colors.text,
|
|
|
|
|
fontSize: 36,
|
|
|
|
|
fontWeight: 'bold'
|
|
|
|
|
},
|
|
|
|
|
artist: {
|
|
|
|
|
...colors.text,
|
|
|
|
|
fontSize: 24,
|
|
|
|
|
opacity: 0.5,
|
|
|
|
|
marginBottom: 24
|
|
|
|
|
},
|
|
|
|
|
index: {
|
|
|
|
|
...colors.text,
|
|
|
|
|
width: 20,
|
|
|
|
|
opacity: 0.5,
|
|
|
|
|
marginRight: 5
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
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;
|
|
|
|
|
`;
|
|
|
|
|
|
2020-07-07 14:40:03 +02:00
|
|
|
const TrackContainer = styled.View<{isPlaying: boolean}>`
|
2020-06-16 17:51:51 +02:00
|
|
|
padding: 15px;
|
|
|
|
|
border-bottom-width: 1px;
|
|
|
|
|
flex-direction: row;
|
2020-07-07 14:40:03 +02:00
|
|
|
|
|
|
|
|
${props => props.isPlaying && css`
|
2020-07-10 15:37:04 +02:00
|
|
|
background-color: ${THEME_COLOR}16;
|
2020-07-07 14:40:03 +02:00
|
|
|
margin: 0 -20px;
|
|
|
|
|
padding: 15px 35px;
|
|
|
|
|
`}
|
2020-06-16 17:51:51 +02:00
|
|
|
`;
|
|
|
|
|
|
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-07-07 14:40:03 +02:00
|
|
|
const currentTrack = useCurrentTrack();
|
2020-06-16 17:51:51 +02:00
|
|
|
|
2020-06-21 10:30:41 +02:00
|
|
|
// Setup callbacks
|
2020-07-06 16:56:23 +02:00
|
|
|
const selectAlbum = useCallback(() => { playAlbum(id); }, [playAlbum, id]);
|
2020-06-21 10:30:41 +02:00
|
|
|
const selectTrack = usePlayTrack();
|
2020-07-06 16:56:23 +02:00
|
|
|
const refresh = useCallback(() => { dispatch(fetchTracksByAlbum(id)); }, [id, dispatch]);
|
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(() => {
|
2020-06-21 10:52:54 +02:00
|
|
|
if (!album?.lastRefreshed || differenceInDays(album?.lastRefreshed, new Date()) > ALBUM_CACHE_AMOUNT_OF_DAYS) {
|
2020-06-20 23:10:19 +02:00
|
|
|
refresh();
|
|
|
|
|
}
|
2020-07-06 16:56:23 +02:00
|
|
|
}, [album?.lastRefreshed, 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
|
2020-07-26 12:34:00 +02:00
|
|
|
contentContainerStyle={{ padding: 20, paddingBottom: 50 }}
|
2020-06-17 15:09:19 +02:00
|
|
|
refreshControl={
|
|
|
|
|
<RefreshControl refreshing={isLoading} onRefresh={refresh} />
|
|
|
|
|
}
|
|
|
|
|
>
|
2020-07-26 14:45:32 +02:00
|
|
|
<AlbumImage source={{ uri: getImage(album?.Id) }} style={colors.imageBackground} />
|
|
|
|
|
<Text style={styles.name} >{album?.Name}</Text>
|
|
|
|
|
<Text style={styles.artist}>{album?.AlbumArtist}</Text>
|
2020-07-10 15:25:32 +02:00
|
|
|
<Button title="Play Album" onPress={selectAlbum} color={THEME_COLOR} />
|
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}>
|
2020-07-26 14:45:32 +02:00
|
|
|
<TrackContainer isPlaying={currentTrack?.id.startsWith(trackId) || false} style={colors.border}>
|
|
|
|
|
<Text style={styles.index}>
|
2020-07-07 14:40:03 +02:00
|
|
|
{tracks[trackId]?.IndexNumber}
|
|
|
|
|
</Text>
|
2020-07-26 14:45:32 +02:00
|
|
|
<Text style={colors.text}>{tracks[trackId]?.Name}</Text>
|
2020-06-21 10:30:41 +02:00
|
|
|
</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;
|