again, no idea what i am doing

This commit is contained in:
Niklas 2021-01-26 23:31:12 +01:00
parent 0aff5e80a7
commit 3f8012fc0b
5 changed files with 69 additions and 38 deletions

View File

@ -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(
<DeckButton
text={e.title}
key={e.pos}
onPress={() => {
props.onButtonPress(e.pos);
}}
/>,
);
});
return <>{buttons}</>;
}

View File

@ -19,8 +19,7 @@ export default function DeckButton(props: Props) {
<TouchableHighlight <TouchableHighlight
style={style.touchable} style={style.touchable}
underlayColor={'#0e86d4'} underlayColor={'#0e86d4'}
onPress={props.onPress} onPress={props.onPress}>
onLongPress={() => console.log('long press')}>
<Text style={style.text}>{props.text}</Text> <Text style={style.text}>{props.text}</Text>
</TouchableHighlight> </TouchableHighlight>
</View> </View>

View File

@ -1,66 +1,71 @@
import {RouteProp} from '@react-navigation/native'; 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 {StatusBar, StyleSheet} from 'react-native';
import Orientation from 'react-native-orientation-locker';
import {SafeAreaView} from 'react-native-safe-area-context'; import {SafeAreaView} from 'react-native-safe-area-context';
import {RootStackParamList} from '../App'; import {RootStackParamList} from '../App';
import DeckButton from '../components/DeckButton'; import ButtonLayout from '../components/ButtonLayout';
import {Layout} from '../types/ButtonLayout';
interface Props { interface Props {
remoteAddress: string; remoteAddress: string;
route: RouteProp<RootStackParamList, 'Deck'>; route: RouteProp<RootStackParamList, 'Deck'>;
} }
function requestLayout(ws: WebSocket) {
ws.send(
JSON.stringify({
messageType: 'layout',
}),
);
}
export default function Deck(props: Props) { export default function Deck(props: Props) {
useEffect(() => { const remoteAddress = props.route.params.remoteAddress;
console.log('Lock to portait');
Orientation.lockToPortrait(); const [layout, setLayout] = useState<Layout>({buttons: []});
return () => {
console.log('Unlock orientation');
Orientation.unlockAllOrientations();
};
});
const ws = useRef<WebSocket | null>(null); const ws = useRef<WebSocket | null>(null);
const remoteAddress = props.route.params.remoteAddress;
useEffect(() => { useEffect(() => {
console.log('Connect ws at: ' + remoteAddress);
// TODO check if remote address is valid // TODO check if remote address is valid
ws.current = new WebSocket(`ws://${remoteAddress}:8069/ws`); ws.current = new WebSocket(`ws://${remoteAddress}:8069/ws`);
ws.current.onopen = () => { ws.current.onopen = () => {
console.log('connectet ws'); requestLayout(ws.current!);
}; };
ws.current.onerror = (e) => { ws.current.onerror = (e) => {
// TODO tell the user something is wrong // 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 () => { return () => {
console.log('close websocket');
ws.current?.close(); ws.current?.close();
}; };
}); }, [remoteAddress]);
const buttons = []; function buttonPress(index: number) {
ws.current?.send(
for (let i = 0; i < 21; i++) { JSON.stringify({
let text = 'btn' + i; messageType: 'buttonPress',
buttons.push( buttonIndex: index,
<DeckButton long: false,
text={text} }),
key={i}
onPress={() => {
ws.current?.send(i + '');
}}
/>,
); );
} }
return ( return (
<> <>
<StatusBar hidden={true} /> <StatusBar hidden={true} />
<SafeAreaView style={style.view}>{buttons}</SafeAreaView> <SafeAreaView style={style.view}>
<ButtonLayout layout={layout} onButtonPress={buttonPress} />
</SafeAreaView>
</> </>
); );
} }

View File

@ -12,21 +12,16 @@ export default function Home(props: Props) {
let [remoteAddress, setRemoteAddress] = useState(''); let [remoteAddress, setRemoteAddress] = useState('');
return ( return (
<> <>
{/* <Button
title={'Start demo'}
onPress={() => props.navigation.navigate('Deck')}
/> */}
<Text>Remote address:</Text> <Text>Remote address:</Text>
<TextInput <TextInput
style={style.textInput} style={style.textInput}
autoCorrect={false} autoCorrect={false}
value={remoteAddress} value={remoteAddress}
onChangeText={(value) => setRemoteAddress(value)} onChangeText={(value) => setRemoteAddress(value.trim())}
/> />
<Button <Button
title={'Connect'} title={'Connect'}
onPress={() => { onPress={() => {
console.log('connect');
props.navigation.navigate('Deck', { props.navigation.navigate('Deck', {
remoteAddress: remoteAddress, remoteAddress: remoteAddress,
}); });

View File

@ -0,0 +1,8 @@
export interface Layout {
buttons: DeckButtonLayout[];
}
export interface DeckButtonLayout {
title: string;
pos: number;
}