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

65 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-06-17 14:58:04 +02:00
import React, { useCallback, useEffect } from 'react';
2020-06-21 10:30:41 +02:00
import { useGetImage } from 'utility/JellyfinApi';
2020-06-17 14:58:04 +02:00
import { Album, NavigationProp } from '../types';
2020-06-21 13:02:23 +02:00
import { Text, SafeAreaView, FlatList } from 'react-native';
2020-06-17 14:58:04 +02:00
import { useDispatch } from 'react-redux';
import { useNavigation } from '@react-navigation/native';
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 { fetchAllAlbums } from 'store/music/actions';
import { ALBUM_CACHE_AMOUNT_OF_DAYS } from 'CONSTANTS';
import TouchableHandler from 'components/TouchableHandler';
2020-06-21 13:02:23 +02:00
import ListContainer from './components/ListContainer';
import AlbumImage, { AlbumItem } from './components/AlbumImage';
import { useAlbumsByArtist } from 'store/music/selectors';
2020-06-16 17:51:51 +02:00
2020-06-17 14:58:04 +02:00
const Albums: React.FC = () => {
// Retrieve data from store
2020-06-21 13:02:23 +02:00
const { entities: albums } = useTypedSelector((state) => state.music.albums);
const ids = useAlbumsByArtist();
2020-06-17 14:58:04 +02:00
const isLoading = useTypedSelector((state) => state.music.albums.isLoading);
2020-06-20 23:10:19 +02:00
const lastRefreshed = useTypedSelector((state) => state.music.lastRefreshed);
2020-06-16 17:51:51 +02:00
2020-06-17 14:58:04 +02:00
// Initialise helpers
const dispatch = useDispatch();
const navigation = useNavigation<NavigationProp>();
const getImage = useGetImage();
2020-06-16 17:51:51 +02:00
2020-06-17 14:58:04 +02:00
// Set callbacks
const retrieveData = useCallback(() => dispatch(fetchAllAlbums()), [dispatch]);
const selectAlbum = useCallback((id: string) => navigation.navigate('Album', { id, album: albums[id] as Album }), [navigation, albums]);
// Retrieve data on mount
2020-06-20 23:10:19 +02:00
useEffect(() => {
2020-06-21 10:30:41 +02:00
// GUARD: Only refresh this API call every set amounts of days
2020-06-20 23:10:19 +02:00
if (!lastRefreshed || differenceInDays(lastRefreshed, new Date()) > ALBUM_CACHE_AMOUNT_OF_DAYS) {
retrieveData();
}
}, []);
2020-06-17 14:58:04 +02:00
return (
<SafeAreaView>
2020-06-21 13:02:23 +02:00
<ListContainer>
2020-06-17 14:58:04 +02:00
<FlatList
data={ids as string[]}
refreshing={isLoading}
onRefresh={retrieveData}
numColumns={2}
keyExtractor={d => d}
renderItem={({ item }) => (
2020-06-21 10:30:41 +02:00
<TouchableHandler id={item} onPress={selectAlbum}>
<AlbumItem>
<AlbumImage source={{ uri: getImage(item) }} />
<Text>{albums[item]?.Name}</Text>
<Text style={{ opacity: 0.5 }}>{albums[item]?.AlbumArtist}</Text>
</AlbumItem>
</TouchableHandler>
2020-06-17 14:58:04 +02:00
)}
/>
2020-06-21 13:02:23 +02:00
</ListContainer>
2020-06-17 14:58:04 +02:00
</SafeAreaView>
);
};
2020-06-16 17:51:51 +02:00
export default Albums;