diff --git a/front/components/PartitionCoord.tsx b/front/components/PartitionCoord.tsx index 1ad6a28..210382e 100644 --- a/front/components/PartitionCoord.tsx +++ b/front/components/PartitionCoord.tsx @@ -8,6 +8,8 @@ type PartitionCoordProps = { file: string; onPartitionReady: () => void; onEndReached: () => void; + onResume: () => void; + onPause: () => void; // Timestamp of the play session, in milisecond timestamp: number; }; @@ -16,6 +18,8 @@ const PartitionCoord = ({ file, onPartitionReady, onEndReached, + onPause, + onResume, timestamp, }: PartitionCoordProps) => { const [partitionData, setPartitionData] = React.useState< @@ -41,6 +45,9 @@ const PartitionCoord = ({ { onEndReached(); }} diff --git a/front/components/PartitionVisualizer/PhaserCanvas.tsx b/front/components/PartitionVisualizer/PhaserCanvas.tsx index 221b6d0..e6725b9 100644 --- a/front/components/PartitionVisualizer/PhaserCanvas.tsx +++ b/front/components/PartitionVisualizer/PhaserCanvas.tsx @@ -1,11 +1,10 @@ // create a simple phaser effect with a canvas that can be easily imported as a react component import * as React from 'react'; -import { useEffect, useRef } from 'react'; +import { useEffect } from 'react'; import Phaser from 'phaser'; import useColorScheme from '../../hooks/colorScheme'; -import { PartitionContext } from '../../views/PlayView'; -import store, { RootState, useSelector } from '../../state/Store'; +import { RootState, useSelector } from '../../state/Store'; import { setSoundPlayer as setSPStore } from '../../state/SoundPlayerSlice'; import { useDispatch } from 'react-redux'; import { SplendidGrandPiano, CacheStorage } from 'smplr'; @@ -106,35 +105,34 @@ export type PhaserCanvasProps = { partitionB64: string; cursorPositions: PianoCursorPosition[]; onEndReached: () => void; + onPause: () => void; + onResume: () => void; + // Timestamp of the play session, in milisecond + timestamp: number; }; -const PhaserCanvas = ({ partitionB64, cursorPositions, onEndReached }: PhaserCanvasProps) => { +const PhaserCanvas = ({ partitionB64, cursorPositions, onEndReached, timestamp }: PhaserCanvasProps) => { const colorScheme = useColorScheme(); const dispatch = useDispatch(); const soundPlayer = useSelector((state: RootState) => state.soundPlayer.soundPlayer); - const { timestamp } = React.useContext(PartitionContext); const [game, setGame] = React.useState(null); globalTimestamp = timestamp; useEffect(() => { if (isValidSoundPlayer(soundPlayer)) { - console.log('cache soundplayer', soundPlayer); return; } - console.log('creating soundplayer'); new SplendidGrandPiano(new AudioContext(), { storage: new CacheStorage(), }) .loaded() .then((sp) => { - console.log('sp', sp); dispatch(setSPStore(sp)); }); }, []); useEffect(() => { - console.log('soundPlayer', soundPlayer); if (!isValidSoundPlayer(soundPlayer) || !soundPlayer) return; const pianoScene = getPianoScene( partitionB64, @@ -152,13 +150,12 @@ const PhaserCanvas = ({ partitionB64, cursorPositions, onEndReached }: PhaserCan scene: pianoScene, scale: { mode: Phaser.Scale.FIT, - autoCenter: Phaser.Scale.CENTER_BOTH, + autoCenter: Phaser.Scale.CENTER_HORIZONTALLY, }, }; setGame(new Phaser.Game(config)); return () => { - console.log('destroying phaser game sp'); if (game) { // currently the condition is always false game.destroy(true); diff --git a/front/views/PlayView.tsx b/front/views/PlayView.tsx index 50bde7a..9dc0c9d 100644 --- a/front/views/PlayView.tsx +++ b/front/views/PlayView.tsx @@ -15,19 +15,17 @@ import { HStack, } from 'native-base'; import IconButton from '../components/IconButton'; -import { Ionicons, MaterialCommunityIcons } from '@expo/vector-icons'; +import { Ionicons } from '@expo/vector-icons'; import { RouteProps, useNavigation } from '../Navigation'; import { transformQuery, useQuery } from '../Queries'; import API from '../API'; import LoadingComponent, { LoadingView } from '../components/Loading'; import Constants from 'expo-constants'; -import { strToKey, keyToStr, Note } from '../models/Piano'; import { useSelector } from 'react-redux'; import { RootState } from '../state/Store'; import { translate } from '../i18n/i18n'; import { ColorSchemeType } from 'native-base/lib/typescript/components/types'; import { useStopwatch } from 'react-use-precision-timer'; -import PartitionView from '../components/PartitionView'; import PartitionCoord from '../components/PartitionCoord'; import TextButton from '../components/TextButton'; import { MIDIAccess, MIDIMessageEvent, requestMIDIAccess } from '@motiz88/react-native-midi'; @@ -124,8 +122,11 @@ const PlayView = ({ songId, type, route }: RouteProps) => { ); }; const onEnd = () => { + stopwatch.stop(); if (webSocket.current?.readyState != WebSocket.OPEN) { - navigation.navigate('Error'); + console.warn('onEnd: Websocket not open'); + navigation.navigate('Home'); + return; } webSocket.current?.send( JSON.stringify({ @@ -158,7 +159,10 @@ const PlayView = ({ songId, type, route }: RouteProps) => { webSocket.current.onclose = () => { console.log('Websocket closed', endMsgReceived); if (!endMsgReceived) { - navigation.replace('Error'); + toast.show({ description: 'Connection lost with Scorometer' }); + // the special case when the front send the end message succesfully + // but the websocket is closed before the end message is received + // is not handled return; } }; @@ -168,7 +172,7 @@ const PlayView = ({ songId, type, route }: RouteProps) => { if (data.type == 'end') { endMsgReceived = true; webSocket.current?.close(); - navigation.replace('Score', { songId: song.data!.id, ...data }); + navigation.navigate('Score', { songId: song.data!.id, ...data }); return; } const points = data.info.score; @@ -272,11 +276,6 @@ const PlayView = ({ songId, type, route }: RouteProps) => { } return ( - ) => { - {/* setPartitionRendered(true)} - timestamp={Math.max(0, time)} - onEndReached={() => { - onEnd(); - }} - /> */} { - onEnd(); - }} + timestamp={time} + onEndReached={onEnd} + onPause={onPause} + onResume={onResume} onPartitionReady={() => setPartitionRendered(true)} /> {!partitionRendered && } @@ -389,7 +379,6 @@ const PlayView = ({ songId, type, route }: RouteProps) => { - ); };