From 3f8012fc0bd44cd432e478f33b65d27f4b6439f4 Mon Sep 17 00:00:00 2001 From: Niklas Date: Tue, 26 Jan 2021 23:31:12 +0100 Subject: [PATCH] again, no idea what i am doing --- src/components/ButtonLayout.tsx | 24 ++++++++++++ src/components/DeckButton.tsx | 3 +- src/screens/Deck.tsx | 65 ++++++++++++++++++--------------- src/screens/Home.tsx | 7 +--- src/types/ButtonLayout.ts | 8 ++++ 5 files changed, 69 insertions(+), 38 deletions(-) create mode 100644 src/components/ButtonLayout.tsx create mode 100644 src/types/ButtonLayout.ts diff --git a/src/components/ButtonLayout.tsx b/src/components/ButtonLayout.tsx new file mode 100644 index 0000000..b3afb56 --- /dev/null +++ b/src/components/ButtonLayout.tsx @@ -0,0 +1,24 @@ +import React from 'react'; +import {Layout} from '../types/ButtonLayout'; +import DeckButton from './DeckButton'; + +interface Props { + layout: Layout; + onButtonPress: (index: number) => void; +} + +export default function ButtonLayout(props: Props) { + const buttons: JSX.Element[] = []; + props.layout.buttons.forEach((e) => { + buttons.push( + { + props.onButtonPress(e.pos); + }} + />, + ); + }); + return <>{buttons}; +} diff --git a/src/components/DeckButton.tsx b/src/components/DeckButton.tsx index 54f551d..edca0a2 100644 --- a/src/components/DeckButton.tsx +++ b/src/components/DeckButton.tsx @@ -19,8 +19,7 @@ export default function DeckButton(props: Props) { console.log('long press')}> + onPress={props.onPress}> {props.text} diff --git a/src/screens/Deck.tsx b/src/screens/Deck.tsx index 8fccb33..d90afeb 100644 --- a/src/screens/Deck.tsx +++ b/src/screens/Deck.tsx @@ -1,66 +1,71 @@ import {RouteProp} from '@react-navigation/native'; -import React, {useEffect, useRef} from 'react'; +import React, {useEffect, useRef, useState} from 'react'; import {StatusBar, StyleSheet} from 'react-native'; -import Orientation from 'react-native-orientation-locker'; import {SafeAreaView} from 'react-native-safe-area-context'; import {RootStackParamList} from '../App'; -import DeckButton from '../components/DeckButton'; +import ButtonLayout from '../components/ButtonLayout'; +import {Layout} from '../types/ButtonLayout'; interface Props { remoteAddress: string; route: RouteProp; } +function requestLayout(ws: WebSocket) { + ws.send( + JSON.stringify({ + messageType: 'layout', + }), + ); +} + export default function Deck(props: Props) { - useEffect(() => { - console.log('Lock to portait'); - Orientation.lockToPortrait(); - return () => { - console.log('Unlock orientation'); - Orientation.unlockAllOrientations(); - }; - }); + const remoteAddress = props.route.params.remoteAddress; + + const [layout, setLayout] = useState({buttons: []}); const ws = useRef(null); - const remoteAddress = props.route.params.remoteAddress; useEffect(() => { - console.log('Connect ws at: ' + remoteAddress); // TODO check if remote address is valid ws.current = new WebSocket(`ws://${remoteAddress}:8069/ws`); ws.current.onopen = () => { - console.log('connectet ws'); + requestLayout(ws.current!); }; ws.current.onerror = (e) => { // TODO tell the user something is wrong - console.error(e.message); + console.error(e); + }; + + ws.current.onmessage = (msg) => { + // TODO also add a mesasge type to response messages + const payload = JSON.parse(msg.data); + if (payload.buttons) { + setLayout(payload); + } }; return () => { - console.log('close websocket'); ws.current?.close(); }; - }); + }, [remoteAddress]); - const buttons = []; - - for (let i = 0; i < 21; i++) { - let text = 'btn' + i; - buttons.push( - { - ws.current?.send(i + ''); - }} - />, + function buttonPress(index: number) { + ws.current?.send( + JSON.stringify({ + messageType: 'buttonPress', + buttonIndex: index, + long: false, + }), ); } return ( <>