Add most recent albums
This commit is contained in:
24
src/components/ListButton.tsx
Normal file
24
src/components/ListButton.tsx
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { TouchableOpacityProps, Text } from 'react-native';
|
||||||
|
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
|
||||||
|
import { faChevronRight } from '@fortawesome/free-solid-svg-icons';
|
||||||
|
import styled from 'styled-components/native';
|
||||||
|
|
||||||
|
const Container = styled.TouchableOpacity`
|
||||||
|
padding: 18px 0;
|
||||||
|
border-bottom-width: 1px;
|
||||||
|
border-bottom-color: #eee;
|
||||||
|
flex-direction: row;
|
||||||
|
justify-content: space-between;
|
||||||
|
`;
|
||||||
|
|
||||||
|
const ListButton: React.FC<TouchableOpacityProps> = ({ children, ...props }) => {
|
||||||
|
return (
|
||||||
|
<Container {...props}>
|
||||||
|
<Text style={{ color: 'salmon', fontSize: 16 }}>{children}</Text>
|
||||||
|
<FontAwesomeIcon style={{ color: 'salmon' }} icon={faChevronRight} />
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ListButton;
|
||||||
7
src/components/Typography.ts
Normal file
7
src/components/Typography.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import styled from 'styled-components/native';
|
||||||
|
|
||||||
|
export const Header = styled.Text`
|
||||||
|
margin: 24px 0 12px 0;
|
||||||
|
font-size: 36px;
|
||||||
|
font-weight: bold;
|
||||||
|
`;
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
import { createStackNavigator } from '@react-navigation/stack';
|
|
||||||
import { RootStackParamList } from './types';
|
|
||||||
import Albums from './components/Albums';
|
|
||||||
import Album from './components/Album';
|
|
||||||
|
|
||||||
const Stack = createStackNavigator<RootStackParamList>();
|
|
||||||
|
|
||||||
function AlbumStack() {
|
|
||||||
return (
|
|
||||||
<Stack.Navigator>
|
|
||||||
<Stack.Screen name="Albums" component={Albums} />
|
|
||||||
<Stack.Screen name="Album" component={Album} />
|
|
||||||
</Stack.Navigator>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default AlbumStack;
|
|
||||||
20
src/screens/Music/index.tsx
Normal file
20
src/screens/Music/index.tsx
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { createStackNavigator } from '@react-navigation/stack';
|
||||||
|
import { StackParams } from './types';
|
||||||
|
import Albums from './stacks/Albums';
|
||||||
|
import Album from './stacks/Album';
|
||||||
|
import RecentAlbums from './stacks/RecentAlbums';
|
||||||
|
|
||||||
|
const Stack = createStackNavigator<StackParams>();
|
||||||
|
|
||||||
|
function MusicStack() {
|
||||||
|
return (
|
||||||
|
<Stack.Navigator initialRouteName="RecentAlbums">
|
||||||
|
<Stack.Screen name="RecentAlbums" component={RecentAlbums} options={{ headerTitle: 'Recent Albums' }} />
|
||||||
|
<Stack.Screen name="Albums" component={Albums} />
|
||||||
|
<Stack.Screen name="Album" component={Album} />
|
||||||
|
</Stack.Navigator>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default MusicStack;
|
||||||
@@ -1,43 +1,22 @@
|
|||||||
import React, { useCallback, useEffect } from 'react';
|
import React, { useCallback, useEffect } from 'react';
|
||||||
import { useGetImage } from 'utility/JellyfinApi';
|
import { useGetImage } from 'utility/JellyfinApi';
|
||||||
import { Album, NavigationProp } from '../types';
|
import { Album, NavigationProp } from '../types';
|
||||||
import { Text, SafeAreaView, FlatList, Dimensions } from 'react-native';
|
import { Text, SafeAreaView, FlatList } from 'react-native';
|
||||||
import styled from 'styled-components/native';
|
|
||||||
import { useDispatch } from 'react-redux';
|
import { useDispatch } from 'react-redux';
|
||||||
import { useNavigation } from '@react-navigation/native';
|
import { useNavigation } from '@react-navigation/native';
|
||||||
import FastImage from 'react-native-fast-image';
|
|
||||||
import { differenceInDays } from 'date-fns';
|
import { differenceInDays } from 'date-fns';
|
||||||
import { useTypedSelector } from 'store';
|
import { useTypedSelector } from 'store';
|
||||||
import { fetchAllAlbums } from 'store/music/actions';
|
import { fetchAllAlbums } from 'store/music/actions';
|
||||||
import { ALBUM_CACHE_AMOUNT_OF_DAYS } from 'CONSTANTS';
|
import { ALBUM_CACHE_AMOUNT_OF_DAYS } from 'CONSTANTS';
|
||||||
import TouchableHandler from 'components/TouchableHandler';
|
import TouchableHandler from 'components/TouchableHandler';
|
||||||
|
import ListContainer from './components/ListContainer';
|
||||||
const Screen = Dimensions.get('screen');
|
import AlbumImage, { AlbumItem } from './components/AlbumImage';
|
||||||
|
import { useAlbumsByArtist } from 'store/music/selectors';
|
||||||
const Container = styled.View`
|
|
||||||
/* flex-direction: row;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
flex: 1; */
|
|
||||||
padding: 10px;
|
|
||||||
background-color: #f6f6f6;
|
|
||||||
`;
|
|
||||||
|
|
||||||
const AlbumItem = styled.View`
|
|
||||||
width: ${Screen.width / 2 - 10}px;
|
|
||||||
padding: 10px;
|
|
||||||
`;
|
|
||||||
|
|
||||||
const AlbumImage = styled(FastImage)`
|
|
||||||
border-radius: 10px;
|
|
||||||
width: ${Screen.width / 2 - 40}px;
|
|
||||||
height: ${Screen.width / 2 - 40}px;
|
|
||||||
background-color: #fefefe;
|
|
||||||
margin-bottom: 5px;
|
|
||||||
`;
|
|
||||||
|
|
||||||
const Albums: React.FC = () => {
|
const Albums: React.FC = () => {
|
||||||
// Retrieve data from store
|
// Retrieve data from store
|
||||||
const { ids, entities: albums } = useTypedSelector((state) => state.music.albums);
|
const { entities: albums } = useTypedSelector((state) => state.music.albums);
|
||||||
|
const ids = useAlbumsByArtist();
|
||||||
const isLoading = useTypedSelector((state) => state.music.albums.isLoading);
|
const isLoading = useTypedSelector((state) => state.music.albums.isLoading);
|
||||||
const lastRefreshed = useTypedSelector((state) => state.music.lastRefreshed);
|
const lastRefreshed = useTypedSelector((state) => state.music.lastRefreshed);
|
||||||
|
|
||||||
@@ -60,7 +39,7 @@ const Albums: React.FC = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<SafeAreaView>
|
<SafeAreaView>
|
||||||
<Container>
|
<ListContainer>
|
||||||
<FlatList
|
<FlatList
|
||||||
data={ids as string[]}
|
data={ids as string[]}
|
||||||
refreshing={isLoading}
|
refreshing={isLoading}
|
||||||
@@ -77,7 +56,7 @@ const Albums: React.FC = () => {
|
|||||||
</TouchableHandler>
|
</TouchableHandler>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
</Container>
|
</ListContainer>
|
||||||
</SafeAreaView>
|
</SafeAreaView>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
73
src/screens/Music/stacks/RecentAlbums.tsx
Normal file
73
src/screens/Music/stacks/RecentAlbums.tsx
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import React, { useCallback, useEffect } from 'react';
|
||||||
|
import { useGetImage } from 'utility/JellyfinApi';
|
||||||
|
import { Album, NavigationProp } from '../types';
|
||||||
|
import { Text, SafeAreaView, FlatList, View } from 'react-native';
|
||||||
|
import { useDispatch } from 'react-redux';
|
||||||
|
import { useNavigation } from '@react-navigation/native';
|
||||||
|
import { useTypedSelector } from 'store';
|
||||||
|
import { fetchRecentAlbums } from 'store/music/actions';
|
||||||
|
import TouchableHandler from 'components/TouchableHandler';
|
||||||
|
import ListContainer from './components/ListContainer';
|
||||||
|
import AlbumImage, { AlbumItem } from './components/AlbumImage';
|
||||||
|
import { useRecentAlbums } from 'store/music/selectors';
|
||||||
|
import { Header } from 'components/Typography';
|
||||||
|
import ListButton from 'components/ListButton';
|
||||||
|
|
||||||
|
const NavigationHeader: React.FC = () => {
|
||||||
|
const navigation = useNavigation();
|
||||||
|
const handleAllAlbumsClick = useCallback(() => { navigation.navigate('Albums'); }, [navigation]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ListContainer>
|
||||||
|
<ListButton onPress={handleAllAlbumsClick}>All Albums</ListButton>
|
||||||
|
<Header>Recent Albums</Header>
|
||||||
|
</ListContainer>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const RecentAlbums: React.FC = () => {
|
||||||
|
// Retrieve data from store
|
||||||
|
const { entities: albums } = useTypedSelector((state) => state.music.albums);
|
||||||
|
const recentAlbums = useRecentAlbums(24);
|
||||||
|
const isLoading = useTypedSelector((state) => state.music.albums.isLoading);
|
||||||
|
|
||||||
|
// Initialise helpers
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
const navigation = useNavigation<NavigationProp>();
|
||||||
|
const getImage = useGetImage();
|
||||||
|
|
||||||
|
// Set callbacks
|
||||||
|
const retrieveData = useCallback(() => dispatch(fetchRecentAlbums()), [dispatch]);
|
||||||
|
const selectAlbum = useCallback((id: string) => navigation.navigate('Album', { id, album: albums[id] as Album }), [navigation, albums]);
|
||||||
|
|
||||||
|
console.log(recentAlbums.map((d) => albums[d]?.DateCreated));
|
||||||
|
|
||||||
|
// Retrieve data on mount
|
||||||
|
useEffect(() => { retrieveData(); }, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SafeAreaView>
|
||||||
|
<ListContainer>
|
||||||
|
<FlatList
|
||||||
|
data={recentAlbums as string[]}
|
||||||
|
refreshing={isLoading}
|
||||||
|
onRefresh={retrieveData}
|
||||||
|
numColumns={2}
|
||||||
|
keyExtractor={d => d}
|
||||||
|
ListHeaderComponent={NavigationHeader}
|
||||||
|
renderItem={({ item }) => (
|
||||||
|
<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>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</ListContainer>
|
||||||
|
</SafeAreaView>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default RecentAlbums;
|
||||||
20
src/screens/Music/stacks/components/AlbumImage.ts
Normal file
20
src/screens/Music/stacks/components/AlbumImage.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import styled from 'styled-components/native';
|
||||||
|
import FastImage from 'react-native-fast-image';
|
||||||
|
import { Dimensions } from 'react-native';
|
||||||
|
|
||||||
|
const Screen = Dimensions.get('screen');
|
||||||
|
|
||||||
|
export const AlbumItem = styled.View`
|
||||||
|
width: ${Screen.width / 2 - 10}px;
|
||||||
|
padding: 10px;
|
||||||
|
`;
|
||||||
|
|
||||||
|
const AlbumImage = styled(FastImage)`
|
||||||
|
border-radius: 10px;
|
||||||
|
width: ${Screen.width / 2 - 40}px;
|
||||||
|
height: ${Screen.width / 2 - 40}px;
|
||||||
|
background-color: #fefefe;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
`;
|
||||||
|
|
||||||
|
export default AlbumImage;
|
||||||
8
src/screens/Music/stacks/components/ListContainer.ts
Normal file
8
src/screens/Music/stacks/components/ListContainer.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import styled from 'styled-components/native';
|
||||||
|
|
||||||
|
const ListContainer = styled.View`
|
||||||
|
padding: 10px;
|
||||||
|
background-color: #f6f6f6;
|
||||||
|
`;
|
||||||
|
|
||||||
|
export default ListContainer;
|
||||||
@@ -40,6 +40,7 @@ export interface Album {
|
|||||||
ImageTags: ImageTags;
|
ImageTags: ImageTags;
|
||||||
BackdropImageTags: any[];
|
BackdropImageTags: any[];
|
||||||
LocationType: string;
|
LocationType: string;
|
||||||
|
DateCreated: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AlbumTrack {
|
export interface AlbumTrack {
|
||||||
@@ -68,6 +69,7 @@ export interface AlbumTrack {
|
|||||||
export type StackParams = {
|
export type StackParams = {
|
||||||
Albums: undefined;
|
Albums: undefined;
|
||||||
Album: { id: string, album: Album };
|
Album: { id: string, album: Album };
|
||||||
|
RecentAlbums: undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type NavigationProp = StackNavigationProp<StackParams>;
|
export type NavigationProp = StackNavigationProp<StackParams>;
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { createBottomTabNavigator, BottomTabNavigationProp } from '@react-navigation/bottom-tabs';
|
import { createBottomTabNavigator, BottomTabNavigationProp } from '@react-navigation/bottom-tabs';
|
||||||
import Player from './Player';
|
import Player from './Player';
|
||||||
import Albums from './Albums';
|
import Music from './Music';
|
||||||
import Settings from './Settings';
|
import Settings from './Settings';
|
||||||
import { createStackNavigator, StackNavigationProp } from '@react-navigation/stack';
|
import { createStackNavigator, StackNavigationProp } from '@react-navigation/stack';
|
||||||
import SetJellyfinServer from './modals/SetJellyfinServer';
|
import SetJellyfinServer from './modals/SetJellyfinServer';
|
||||||
@@ -12,15 +12,15 @@ const Tab = createBottomTabNavigator();
|
|||||||
|
|
||||||
type Screens = {
|
type Screens = {
|
||||||
NowPlaying: undefined;
|
NowPlaying: undefined;
|
||||||
Albums: undefined;
|
Music: undefined;
|
||||||
Settings: undefined;
|
Settings: undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Screens() {
|
function Screens() {
|
||||||
return (
|
return (
|
||||||
<Tab.Navigator>
|
<Tab.Navigator>
|
||||||
<Tab.Screen name="NowPlaying" component={Player} />
|
<Tab.Screen name="NowPlaying" component={Player} options={{ tabBarLabel: 'Now Playing' }} />
|
||||||
<Tab.Screen name="Albums" component={Albums} />
|
<Tab.Screen name="Music" component={Music} />
|
||||||
<Tab.Screen name="Settings" component={Settings} />
|
<Tab.Screen name="Settings" component={Settings} />
|
||||||
</Tab.Navigator>
|
</Tab.Navigator>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { configureStore, getDefaultMiddleware, combineReducers } from '@reduxjs/
|
|||||||
import { useSelector, TypedUseSelectorHook } from 'react-redux';
|
import { useSelector, TypedUseSelectorHook } from 'react-redux';
|
||||||
import AsyncStorage from '@react-native-community/async-storage';
|
import AsyncStorage from '@react-native-community/async-storage';
|
||||||
import { persistStore, persistReducer } from 'redux-persist';
|
import { persistStore, persistReducer } from 'redux-persist';
|
||||||
import logger from 'redux-logger';
|
// import logger from 'redux-logger';
|
||||||
|
|
||||||
const persistConfig = {
|
const persistConfig = {
|
||||||
key: 'root',
|
key: 'root',
|
||||||
@@ -22,7 +22,7 @@ const persistedReducer = persistReducer(persistConfig, reducers);
|
|||||||
const store = configureStore({
|
const store = configureStore({
|
||||||
reducer: persistedReducer,
|
reducer: persistedReducer,
|
||||||
middleware: getDefaultMiddleware({ serializableCheck: false, immutableCheck: false }).concat(
|
middleware: getDefaultMiddleware({ serializableCheck: false, immutableCheck: false }).concat(
|
||||||
logger
|
// logger
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,31 +1,46 @@
|
|||||||
import { createAsyncThunk, createEntityAdapter } from '@reduxjs/toolkit';
|
import { createAsyncThunk, createEntityAdapter } from '@reduxjs/toolkit';
|
||||||
import { Album, AlbumTrack } from './types';
|
import { Album, AlbumTrack } from './types';
|
||||||
import { AsyncThunkAPI } from '..';
|
import { AsyncThunkAPI } from '..';
|
||||||
import { retrieveAlbums, retrieveAlbumTracks } from 'utility/JellyfinApi';
|
import { retrieveAlbums, retrieveAlbumTracks, retrieveRecentAlbums } from 'utility/JellyfinApi';
|
||||||
|
|
||||||
export const albumAdapter = createEntityAdapter<Album>({
|
export const albumAdapter = createEntityAdapter<Album>({
|
||||||
selectId: album => album.Id,
|
selectId: album => album.Id,
|
||||||
sortComparer: (a, b) => a.Name.localeCompare(b.Name),
|
sortComparer: (a, b) => a.Name.localeCompare(b.Name),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all albums available on the jellyfin server
|
||||||
|
*/
|
||||||
export const fetchAllAlbums = createAsyncThunk<Album[], undefined, AsyncThunkAPI>(
|
export const fetchAllAlbums = createAsyncThunk<Album[], undefined, AsyncThunkAPI>(
|
||||||
'/albums/all',
|
'/albums/all',
|
||||||
async (empty, thunkAPI) => {
|
async (empty, thunkAPI) => {
|
||||||
console.log('RETRIEVING ALBUMS');
|
|
||||||
const credentials = thunkAPI.getState().settings.jellyfin;
|
const credentials = thunkAPI.getState().settings.jellyfin;
|
||||||
return retrieveAlbums(credentials) as Promise<Album[]>;
|
return retrieveAlbums(credentials) as Promise<Album[]>;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the most recent albums
|
||||||
|
*/
|
||||||
|
export const fetchRecentAlbums = createAsyncThunk<Album[], number | undefined, AsyncThunkAPI>(
|
||||||
|
'/albums/recent',
|
||||||
|
async (numberOfAlbums, thunkAPI) => {
|
||||||
|
const credentials = thunkAPI.getState().settings.jellyfin;
|
||||||
|
return retrieveRecentAlbums(credentials, numberOfAlbums) as Promise<Album[]>;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
export const trackAdapter = createEntityAdapter<AlbumTrack>({
|
export const trackAdapter = createEntityAdapter<AlbumTrack>({
|
||||||
selectId: track => track.Id,
|
selectId: track => track.Id,
|
||||||
sortComparer: (a, b) => a.IndexNumber - b.IndexNumber,
|
sortComparer: (a, b) => a.IndexNumber - b.IndexNumber,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve all tracks from a particular album
|
||||||
|
*/
|
||||||
export const fetchTracksByAlbum = createAsyncThunk<AlbumTrack[], string, AsyncThunkAPI>(
|
export const fetchTracksByAlbum = createAsyncThunk<AlbumTrack[], string, AsyncThunkAPI>(
|
||||||
'/tracks/byAlbum',
|
'/tracks/byAlbum',
|
||||||
async (ItemId, thunkAPI) => {
|
async (ItemId, thunkAPI) => {
|
||||||
console.log('RETRIEVING ALBUMS');
|
|
||||||
const credentials = thunkAPI.getState().settings.jellyfin;
|
const credentials = thunkAPI.getState().settings.jellyfin;
|
||||||
return retrieveAlbumTracks(ItemId, credentials) as Promise<AlbumTrack[]>;
|
return retrieveAlbumTracks(ItemId, credentials) as Promise<AlbumTrack[]>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { fetchAllAlbums, albumAdapter, fetchTracksByAlbum, trackAdapter } from './actions';
|
import { fetchAllAlbums, albumAdapter, fetchTracksByAlbum, trackAdapter, fetchRecentAlbums } from './actions';
|
||||||
import { createSlice, Dictionary, EntityId } from '@reduxjs/toolkit';
|
import { createSlice, Dictionary, EntityId } from '@reduxjs/toolkit';
|
||||||
import { Album, AlbumTrack } from './types';
|
import { Album, AlbumTrack } from './types';
|
||||||
|
|
||||||
@@ -32,6 +32,9 @@ const music = createSlice({
|
|||||||
initialState,
|
initialState,
|
||||||
reducers: {},
|
reducers: {},
|
||||||
extraReducers: builder => {
|
extraReducers: builder => {
|
||||||
|
/**
|
||||||
|
* Fetch All albums
|
||||||
|
*/
|
||||||
builder.addCase(fetchAllAlbums.fulfilled, (state, { payload }) => {
|
builder.addCase(fetchAllAlbums.fulfilled, (state, { payload }) => {
|
||||||
albumAdapter.setAll(state.albums, payload);
|
albumAdapter.setAll(state.albums, payload);
|
||||||
state.albums.isLoading = false;
|
state.albums.isLoading = false;
|
||||||
@@ -39,6 +42,20 @@ const music = createSlice({
|
|||||||
});
|
});
|
||||||
builder.addCase(fetchAllAlbums.pending, (state) => { state.albums.isLoading = true; });
|
builder.addCase(fetchAllAlbums.pending, (state) => { state.albums.isLoading = true; });
|
||||||
builder.addCase(fetchAllAlbums.rejected, (state) => { state.albums.isLoading = false; });
|
builder.addCase(fetchAllAlbums.rejected, (state) => { state.albums.isLoading = false; });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch most recent albums
|
||||||
|
*/
|
||||||
|
builder.addCase(fetchRecentAlbums.fulfilled, (state, { payload }) => {
|
||||||
|
albumAdapter.upsertMany(state.albums, payload);
|
||||||
|
state.albums.isLoading = false;
|
||||||
|
});
|
||||||
|
builder.addCase(fetchRecentAlbums.pending, (state) => { state.albums.isLoading = true; });
|
||||||
|
builder.addCase(fetchRecentAlbums.rejected, (state) => { state.albums.isLoading = false; });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch tracks by album
|
||||||
|
*/
|
||||||
builder.addCase(fetchTracksByAlbum.fulfilled, (state, { payload }) => {
|
builder.addCase(fetchTracksByAlbum.fulfilled, (state, { payload }) => {
|
||||||
trackAdapter.setAll(state.tracks, payload);
|
trackAdapter.setAll(state.tracks, payload);
|
||||||
|
|
||||||
|
|||||||
42
src/store/music/selectors.ts
Normal file
42
src/store/music/selectors.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import { useTypedSelector } from 'store';
|
||||||
|
import { parseISO } from 'date-fns';
|
||||||
|
import { Album } from './types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a list of the n most recent albums
|
||||||
|
*/
|
||||||
|
export function useRecentAlbums(amount: number) {
|
||||||
|
const albums = useTypedSelector((state) => state.music.albums.entities);
|
||||||
|
const albumIds = useTypedSelector((state) => state.music.albums.ids);
|
||||||
|
|
||||||
|
const sorted = [...albumIds].sort((a, b) => {
|
||||||
|
const albumA = albums[a];
|
||||||
|
const albumB = albums[b];
|
||||||
|
const dateA = albumA ? parseISO(albumA.DateCreated).getTime() : 0;
|
||||||
|
const dateB = albumB ? parseISO(albumB.DateCreated).getTime() : 0;
|
||||||
|
return dateB - dateA;
|
||||||
|
});
|
||||||
|
|
||||||
|
return sorted.slice(0, amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useAlbumsByArtist() {
|
||||||
|
const albums = useTypedSelector((state) => state.music.albums.entities);
|
||||||
|
const albumIds = useTypedSelector((state) => state.music.albums.ids);
|
||||||
|
|
||||||
|
const sorted = [...albumIds].sort((a, b) => {
|
||||||
|
const albumA = albums[a];
|
||||||
|
const albumB = albums[b];
|
||||||
|
if ((!albumA && !albumB) || (!albumA?.AlbumArtist && !albumB?.AlbumArtist)) {
|
||||||
|
return 0;
|
||||||
|
} else if (!albumA || !albumA.AlbumArtist) {
|
||||||
|
return 1;
|
||||||
|
} else if (!albumB || !albumB.AlbumArtist) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return albumA.AlbumArtist.localeCompare(albumB.AlbumArtist);
|
||||||
|
});
|
||||||
|
|
||||||
|
return sorted;
|
||||||
|
}
|
||||||
@@ -35,13 +35,14 @@ export interface Album {
|
|||||||
PrimaryImageAspectRatio: number;
|
PrimaryImageAspectRatio: number;
|
||||||
Artists: string[];
|
Artists: string[];
|
||||||
ArtistItems: ArtistItem[];
|
ArtistItems: ArtistItem[];
|
||||||
AlbumArtist: string;
|
AlbumArtist?: string;
|
||||||
AlbumArtists: AlbumArtist[];
|
AlbumArtists: AlbumArtist[];
|
||||||
ImageTags: ImageTags;
|
ImageTags: ImageTags;
|
||||||
BackdropImageTags: any[];
|
BackdropImageTags: any[];
|
||||||
LocationType: string;
|
LocationType: string;
|
||||||
Tracks?: string[];
|
Tracks?: string[];
|
||||||
lastRefreshed?: number;
|
lastRefreshed?: number;
|
||||||
|
DateCreated: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AlbumTrack {
|
export interface AlbumTrack {
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ const albumOptions = {
|
|||||||
SortOrder: 'Ascending',
|
SortOrder: 'Ascending',
|
||||||
IncludeItemTypes: 'MusicAlbum',
|
IncludeItemTypes: 'MusicAlbum',
|
||||||
Recursive: 'true',
|
Recursive: 'true',
|
||||||
Fields: 'PrimaryImageAspectRatio,SortName,BasicSyncInfo',
|
Fields: 'PrimaryImageAspectRatio,SortName,BasicSyncInfo,DateCreated',
|
||||||
ImageTypeLimit: '1',
|
ImageTypeLimit: '1',
|
||||||
EnableImageTypes: 'Primary,Backdrop,Banner,Thumb',
|
EnableImageTypes: 'Primary,Backdrop,Banner,Thumb',
|
||||||
};
|
};
|
||||||
@@ -72,13 +72,39 @@ const albumParams = new URLSearchParams(albumOptions).toString();
|
|||||||
*/
|
*/
|
||||||
export async function retrieveAlbums(credentials: Credentials) {
|
export async function retrieveAlbums(credentials: Credentials) {
|
||||||
const config = generateConfig(credentials);
|
const config = generateConfig(credentials);
|
||||||
console.log(`${credentials?.uri}/Users/${credentials?.user_id}/Items?${albumParams}`);
|
|
||||||
const albums = await fetch(`${credentials?.uri}/Users/${credentials?.user_id}/Items?${albumParams}`, config)
|
const albums = await fetch(`${credentials?.uri}/Users/${credentials?.user_id}/Items?${albumParams}`, config)
|
||||||
.then(response => response.json());
|
.then(response => response.json());
|
||||||
|
|
||||||
return albums.Items;
|
return albums.Items;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const latestAlbumsOptions = {
|
||||||
|
IncludeItemTypes: 'MusicAlbum',
|
||||||
|
Fields: 'DateCreated',
|
||||||
|
SortOrder: 'Ascending',
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the most recently added albums on the Jellyfin server
|
||||||
|
*/
|
||||||
|
export async function retrieveRecentAlbums(credentials: Credentials, numberOfAlbums = 24) {
|
||||||
|
const config = generateConfig(credentials);
|
||||||
|
|
||||||
|
// Generate custom config based on function input
|
||||||
|
const options = {
|
||||||
|
...latestAlbumsOptions,
|
||||||
|
Limit: numberOfAlbums.toString(),
|
||||||
|
};
|
||||||
|
const params = new URLSearchParams(options).toString();
|
||||||
|
|
||||||
|
// Retrieve albums
|
||||||
|
const albums = await fetch(`${credentials?.uri}/Users/${credentials?.user_id}/Items/Latest?${params}`, config)
|
||||||
|
.then(response => response.json());
|
||||||
|
|
||||||
|
return albums;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a single album from the Emby server
|
* Retrieve a single album from the Emby server
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user