From ff4926fa80e35214f5c1ef1069f6079e6303079e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Wed, 29 Nov 2023 23:48:11 +0100 Subject: [PATCH] Moved the Loading display to partition Magic to better display svg loading --- front/components/Play/PartitionMagic.tsx | 105 ++++++++++++++--------- 1 file changed, 63 insertions(+), 42 deletions(-) diff --git a/front/components/Play/PartitionMagic.tsx b/front/components/Play/PartitionMagic.tsx index 13d7867..4e38b4a 100644 --- a/front/components/Play/PartitionMagic.tsx +++ b/front/components/Play/PartitionMagic.tsx @@ -8,6 +8,7 @@ import { CursorInfoItem } from '../../models/SongCursorInfos'; import { PianoNotes } from '../../state/SoundPlayerSlice'; import { Audio } from 'expo-av'; import { SvgContainer } from './SvgContainer'; +import LoadingComponent from '../Loading'; // note we are also using timestamp in a context export type ParitionMagicProps = { @@ -41,6 +42,7 @@ const getCursorToPlay = ( const PartitionMagic = ({ songID, onEndReached, onError, onReady }: ParitionMagicProps) => { const { data, isLoading, isError } = useQuery(API.getSongCursorInfos(songID)); const [currentCurIdx, setCurrentCurIdx] = React.useState(-1); + const [isPartitionSvgLoaded, setIsPartitionSvgLoaded] = React.useState(false); const partitionOffset = useSharedValue(0); const pianoCC = React.useContext(PianoCC); const pianoSounds = React.useRef>(); @@ -55,37 +57,40 @@ const PartitionMagic = ({ songID, onEndReached, onError, onReady }: ParitionMagi const cursorTop = (data?.cursors[cursorDisplayIdx]?.y ?? 0) - cursorPaddingVertical; const cursorLeft = (data?.cursors[0]?.x ?? 0) - cursorPaddingHorizontal; - React.useEffect(() => { - if (!pianoSounds.current) { - Promise.all( - Object.entries(PianoNotes).map(([midiNumber, noteResource]) => - Audio.Sound.createAsync(noteResource, { - volume: 1, - progressUpdateIntervalMillis: 100, - }).then((sound) => [midiNumber, sound.sound] as const) - ) - ).then( - (res) => - (pianoSounds.current = res.reduce( - (prev, curr) => ({ ...prev, [curr[0]]: curr[1] }), - {} - )) - ); - } - }, []); + // React.useEffect(() => { + // if (!pianoSounds.current) { + // Promise.all( + // Object.entries(PianoNotes).map(([midiNumber, noteResource]) => + // Audio.Sound.createAsync(noteResource, { + // volume: 1, + // progressUpdateIntervalMillis: 100, + // }).then((sound) => [midiNumber, sound.sound] as const) + // ) + // ).then( + // (res) => + // (pianoSounds.current = res.reduce( + // (prev, curr) => ({ ...prev, [curr[0]]: curr[1] }), + // {} + // )) + // ); + // } + // }, []); const partitionDims = React.useMemo<[number, number]>(() => { return [data?.pageWidth ?? 0, data?.pageHeight ?? 1]; }, [data]); React.useEffect(() => { - if (isLoading) { - return; - } if (isError) { onError('Error while loading partition'); return; } - }, [isLoading, isError]); + }, [isError]); + + React.useEffect(() => { + if (isPartitionSvgLoaded && !isLoading) { + onReady(); + } + }, [isPartitionSvgLoaded, isLoading]); const transitionDuration = 200; @@ -94,17 +99,17 @@ const PartitionMagic = ({ songID, onEndReached, onError, onReady }: ParitionMagi currentCurIdx, pianoCC.timestamp - transitionDuration, (cursor, idx) => { - cursor.notes.forEach(({ note, duration }) => { - try { - const sound = pianoSounds.current![note]!; - sound.playAsync().catch(console.error); - setTimeout(() => { - sound.stopAsync(); - }, duration - 10); - } catch (e) { - console.log(e); - } - }); + // cursor.notes.forEach(({ note, duration }) => { + // try { + // const sound = pianoSounds.current![note]!; + // sound.playAsync().catch(console.error); + // setTimeout(() => { + // sound.stopAsync(); + // }, duration - 10); + // } catch (e) { + // console.log(e); + // } + // }); partitionOffset.value = withTiming( -(cursor.x - data!.cursors[0]!.x) / partitionDims[0], { @@ -130,6 +135,22 @@ const PartitionMagic = ({ songID, onEndReached, onError, onReady }: ParitionMagi overflow: 'hidden', }} > + {(!isPartitionSvgLoaded || isLoading) && ( + + + + )} - {!isLoading && !isError && ( - - )} + { + setIsPartitionSvgLoaded(true); + }} + style={{ + aspectRatio: partitionDims[0] / partitionDims[1], + }} + />