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

188 lines
7.2 KiB
TypeScript
Raw Normal View History

import React, { useCallback, useEffect, useRef, ReactText, useMemo } from 'react';
2023-06-19 22:26:41 +02:00
import { useGetImage } from '@/utility/JellyfinApi';
2023-04-22 23:31:37 +02:00
import { SectionList, View } from 'react-native';
2020-06-17 14:58:04 +02:00
import { useNavigation } from '@react-navigation/native';
2020-06-20 23:10:19 +02:00
import { differenceInDays } from 'date-fns';
2023-06-19 22:26:41 +02:00
import { useAppDispatch, useTypedSelector } from '@/store';
import { fetchAllAlbums } from '@/store/music/actions';
import { ALBUM_CACHE_AMOUNT_OF_DAYS } from '@/CONSTANTS';
import TouchableHandler from '@/components/TouchableHandler';
2022-05-11 23:57:30 +02:00
import AlbumImage, { AlbumHeight, AlbumItem } from './components/AlbumImage';
2023-06-19 22:26:41 +02:00
import { selectAlbumsByAlphabet, SectionedId } from '@/store/music/selectors';
import AlphabetScroller from '@/components/AlphabetScroller';
import styled from 'styled-components/native';
2023-06-19 22:26:41 +02:00
import useDefaultStyles, { ColoredBlurView } from '@/components/Colors';
import { Album } from '@/store/music/types';
import { Text } from '@/components/Typography';
import { ShadowWrapper } from '@/components/Shadow';
2023-06-19 23:03:17 +02:00
import { NavigationProp } from '@/screens/types';
2023-06-19 22:26:41 +02:00
import { SafeSectionList } from '@/components/SafeNavigatorView';
2022-05-11 23:57:30 +02:00
const HeadingHeight = 50;
2020-06-25 18:07:44 +02:00
function generateSection({ section }: { section: SectionedId }) {
return (
<SectionHeading label={section.label} key={section.label} />
2020-06-25 18:07:44 +02:00
);
}
const SectionContainer = styled.View`
2022-05-11 23:57:30 +02:00
height: ${HeadingHeight}px;
justify-content: center;
2022-05-11 23:57:30 +02:00
padding: 0 24px;
`;
2022-05-11 23:57:30 +02:00
const SectionText = styled(Text)`
font-size: 24px;
2022-05-11 23:57:30 +02:00
font-weight: 400;
`;
const SectionHeading = React.memo(function SectionHeading(props: { label: string }) {
const { label } = props;
2020-07-26 17:18:15 +02:00
return (
2022-05-11 23:57:30 +02:00
<ColoredBlurView>
<SectionContainer>
<SectionText>{label}</SectionText>
</SectionContainer>
</ColoredBlurView>
);
});
2020-06-25 18:07:44 +02:00
interface GeneratedAlbumItemProps {
id: ReactText;
imageUrl: string;
name: string;
artist: string;
onPress: (id: string) => void;
}
const HalfOpacity = styled.Text`
opacity: 0.5;
`;
const GeneratedAlbumItem = React.memo(function GeneratedAlbumItem(props: GeneratedAlbumItemProps) {
const defaultStyles = useDefaultStyles();
const { id, imageUrl, name, artist, onPress } = props;
2020-06-25 18:07:44 +02:00
return (
<TouchableHandler id={id as string} onPress={onPress}>
<AlbumItem>
2022-05-16 22:16:45 +02:00
<ShadowWrapper size="medium">
<AlbumImage source={{ uri: imageUrl }} style={[defaultStyles.imageBackground]} />
</ShadowWrapper>
<Text numberOfLines={1} style={defaultStyles.text}>{name}</Text>
<HalfOpacity style={defaultStyles.text} numberOfLines={1}>{artist}</HalfOpacity>
</AlbumItem>
</TouchableHandler>
);
});
2020-06-25 18:07:44 +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);
2020-06-17 14:58:04 +02:00
const isLoading = useTypedSelector((state) => state.music.albums.isLoading);
2022-01-01 19:09:21 +01:00
const lastRefreshed = useTypedSelector((state) => state.music.albums.lastRefreshed);
2020-06-25 18:07:44 +02:00
const sections = useTypedSelector(selectAlbumsByAlphabet);
2020-06-16 17:51:51 +02:00
2020-06-17 14:58:04 +02:00
// Initialise helpers
2022-05-18 22:10:06 +02:00
const dispatch = useAppDispatch();
const navigation = useNavigation<NavigationProp>();
2020-06-17 14:58:04 +02:00
const getImage = useGetImage();
2024-02-08 22:11:43 +01:00
const listRef = useRef<SectionList<string[]>>(null);
// Create an array that computes all the height data for the entire list in
// advance. We can then use this pre-computed data to respond to
// `getItemLayout` calls, without having to compute things in place (and
// fail horribly).
// This approach was inspired by https://gist.github.com/RaphBlanchet/472ed013e05398c083caae6216b598b5
const itemLayouts = useMemo(() => {
// Create an array in which we will store all possible outputs for
// `getItemLayout`. We will loop through each potential album and add
// items that will be in the list
const layouts: Array<{ length: number; offset: number; index: number }> = [];
// Keep track of both the index of items and the offset (in pixels) from
// the top
let index = 0;
let offset = 0;
// Loop through each individual section (i.e. alphabet letter) and add
// all items in that particular section.
sections.forEach((section) => {
// Each section starts with a header, so we'll need to add the item,
// as well as the offset.
layouts[index] = ({ length: HeadingHeight, offset, index });
index++;
offset += HeadingHeight;
// Then, loop through all the rows (sets of two albums) and add
// items for those as well.
section.data.forEach(() => {
layouts[index] = ({ length: AlbumHeight, offset, index });
index++;
offset += AlbumHeight;
});
// The way SectionList works is that you get an item for a
// SectionHeader and a SectionFooter, no matter if you've specified
// whether you want them or not. Thus, we will need to add an empty
// footer as an item, so that we don't mismatch our indexes
layouts[index] = { length: 0, offset, index };
index++;
});
// Then, store and memoize the output
return layouts;
}, [sections]);
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]);
2020-06-25 18:07:44 +02:00
const selectLetter = useCallback((sectionIndex: number) => {
listRef.current?.scrollToLocation({ sectionIndex, itemIndex: 0, animated: false, });
}, [listRef]);
2024-02-08 22:11:43 +01:00
const generateItem = useCallback(({ item }: { item: string[] }) => {
2020-06-25 18:07:44 +02:00
return (
<View style={{ flexDirection: 'row', marginLeft: 10, marginRight: 10 }} key={item.join('-')}>
{item.map((id) => (
2020-06-25 18:07:44 +02:00
<GeneratedAlbumItem
key={id}
id={id}
imageUrl={getImage(id as string)}
name={albums[id]?.Name || ''}
artist={albums[id]?.AlbumArtist || ''}
2020-06-25 18:07:44 +02:00
onPress={selectAlbum}
/>
))}
2020-06-25 18:07:44 +02:00
</View>
);
}, [albums, getImage, selectAlbum]);
2020-06-25 18:07:44 +02:00
2020-06-17 14:58:04 +02:00
// 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 (
2023-04-22 23:31:37 +02:00
<>
2020-07-26 17:18:15 +02:00
<AlphabetScroller onSelect={selectLetter} />
<SafeSectionList
2020-07-26 17:18:15 +02:00
sections={sections}
refreshing={isLoading}
onRefresh={retrieveData}
getItemLayout={(_, i) => itemLayouts[i] ?? { length: 0, offset: 0, index: i }}
2020-07-26 17:18:15 +02:00
ref={listRef}
keyExtractor={(item) => item.join('-')}
2020-07-26 17:18:15 +02:00
renderSectionHeader={generateSection}
renderItem={generateItem}
/>
2023-04-22 23:31:37 +02:00
</>
2020-06-17 14:58:04 +02:00
);
};
2020-06-16 17:51:51 +02:00
export default Albums;