From 15b31001070680617aa7172cd5e49ecdf1c8f47d Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Wed, 30 Jun 2021 22:13:57 +0900 Subject: [PATCH] pick from dict better than find from array --- src/components/common/AlbumView.tsx | 21 ++++++++++++++++++-- src/components/common/GradientBackground.tsx | 10 +++++++--- src/components/library/AlbumsTab.tsx | 6 ++++-- src/state/music.ts | 12 +++++++---- src/styles/text.ts | 2 +- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/components/common/AlbumView.tsx b/src/components/common/AlbumView.tsx index e349b8d..d39c0b2 100644 --- a/src/components/common/AlbumView.tsx +++ b/src/components/common/AlbumView.tsx @@ -1,13 +1,14 @@ import { useNavigation } from '@react-navigation/native'; import { useAtomValue } from 'jotai/utils'; import React, { useEffect, useState } from 'react'; -import { GestureResponderEvent, Image, Pressable, Text, useWindowDimensions, View } from 'react-native'; +import { ActivityIndicator, GestureResponderEvent, Image, Pressable, Text, useWindowDimensions, View } from 'react-native'; import { useCurrentTrackId, useSetQueue } from '../../hooks/player'; import { albumAtomFamily } from '../../state/music'; import colors from '../../styles/colors'; import text from '../../styles/text'; import AlbumArt from './AlbumArt'; import Button from './Button'; +import GradientBackground from './GradientBackground'; import GradientScrollView from './GradientScrollView'; const SongItem: React.FC<{ @@ -153,6 +154,21 @@ const AlbumDetails: React.FC<{ ); } +const AlbumViewFallback = () => { + const layout = useWindowDimensions(); + + const coverSize = layout.width - layout.width / 2.5; + + return ( + + + + ); +} + const AlbumView: React.FC<{ id: string, title: string; @@ -164,8 +180,9 @@ const AlbumView: React.FC<{ }); return ( - Loading...}> + }> + {/* */} ); }; diff --git a/src/components/common/GradientBackground.tsx b/src/components/common/GradientBackground.tsx index 23e5c52..e7c9071 100644 --- a/src/components/common/GradientBackground.tsx +++ b/src/components/common/GradientBackground.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { useWindowDimensions } from 'react-native'; +import { useWindowDimensions, ViewStyle } from 'react-native'; import LinearGradient from 'react-native-linear-gradient'; import colors from '../../styles/colors'; @@ -7,7 +7,8 @@ const GradientBackground: React.FC<{ height?: number | string; width?: number | string; position?: 'relative' | 'absolute'; -}> = ({ height, width, position }) => { + style?: ViewStyle; +}> = ({ height, width, position, style, children }) => { const layout = useWindowDimensions(); return ( @@ -15,11 +16,14 @@ const GradientBackground: React.FC<{ colors={[colors.gradient.high, colors.gradient.low]} locations={[0.01,0.7]} style={{ + ...style, width: width || '100%', height: height || layout.height, position: position || 'absolute', }} - /> + > + {children} + ); }; diff --git a/src/components/library/AlbumsTab.tsx b/src/components/library/AlbumsTab.tsx index 92fcd4c..e5cc30c 100644 --- a/src/components/library/AlbumsTab.tsx +++ b/src/components/library/AlbumsTab.tsx @@ -61,8 +61,10 @@ const AlbumsList = () => { const updating = useAtomValue(albumsUpdatingAtom); const updateAlbums = useUpdateAlbums(); + const albumsList = Object.values(albums); + useEffect(() => { - if (albums.length === 0) { + if (albumsList.length === 0) { updateAlbums(); } }); @@ -70,7 +72,7 @@ const AlbumsList = () => { return ( item.id} numColumns={3} diff --git a/src/state/music.ts b/src/state/music.ts index 13cf431..264b818 100644 --- a/src/state/music.ts +++ b/src/state/music.ts @@ -2,7 +2,7 @@ import { atom, useAtom } from 'jotai'; import { atomFamily, useAtomValue, useUpdateAtom } from 'jotai/utils'; import { Album, AlbumArt, AlbumWithSongs, Artist, ArtistArt, ArtistInfo, Song } from '../models/music'; import { SubsonicApiClient } from '../subsonic/api'; -import { AlbumID3Element, ArtistID3Element, ArtistInfo2Element, ChildElement } from '../subsonic/elements'; +import { AlbumID3Element, ArtistInfo2Element, ChildElement } from '../subsonic/elements'; import { GetArtistResponse } from '../subsonic/responses'; import { activeServerAtom } from './settings'; @@ -36,7 +36,7 @@ export const useUpdateArtists = () => { } } -export const albumsAtom = atom([]); +export const albumsAtom = atom>({}); export const albumsUpdatingAtom = atom(false); export const useUpdateAlbums = () => { @@ -57,7 +57,11 @@ export const useUpdateAlbums = () => { const client = new SubsonicApiClient(server); const response = await client.getAlbumList2({ type: 'alphabeticalByArtist', size: 500 }); - setAlbums(response.data.albums.map(a => mapAlbumID3(a, client))); + setAlbums(response.data.albums.reduce((acc, next) => { + const album = mapAlbumID3(next, client); + acc[album.id] = album; + return acc; + }, {} as Record)); setUpdating(false); } } @@ -80,7 +84,7 @@ export const albumArtAtomFamily = atomFamily((id: string) => atom a.id === id); + const album = id in albums ? albums[id] : undefined; if (!album) { return undefined; } diff --git a/src/styles/text.ts b/src/styles/text.ts index c3e0462..cefc3a1 100644 --- a/src/styles/text.ts +++ b/src/styles/text.ts @@ -13,7 +13,7 @@ const paragraph: TextStyle = { const header: TextStyle = { ...paragraph, - fontSize: 19, + fontSize: 18, fontFamily: fontSemiBold, };