mancala/mobile/src/screens/LobyScreen.tsx

71 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-03-24 14:23:08 +03:00
import * as React from 'react';
2024-03-31 17:54:07 +03:00
import { View, Text, Button, Pressable } from 'react-native';
2024-03-24 14:23:08 +03:00
import { useTranslation } from 'react-i18next';
import { LobyScreenProps } from '../types';
import { useEffect } from 'react';
import { CommonMancalaGame } from 'mancala.js';
2024-03-31 17:54:07 +03:00
import { channel_cancel_new_game, channel_on_game_start } from '../const/channel_names';
import CircularPanel from '../components/CircularPanel';
import { getColorByBrightness } from '../util/ColorUtil';
import { SvgXml } from 'react-native-svg';
import helpSvg from '../svg/help';
2024-03-24 14:23:08 +03:00
export default function LobyScreen({ navigation, route }: LobyScreenProps) {
const { context } = route.params;
2024-03-24 14:23:08 +03:00
const { t } = useTranslation();
const onGameStart = async (message: Object) => {
const newGame: CommonMancalaGame = message as CommonMancalaGame;
const userKey = await context.userKeyStore.getUserKey();
2024-03-31 16:39:39 +03:00
navigation.replace("Game", { context, gameId: newGame.id, userKey })
}
2024-03-31 17:54:07 +03:00
const onCancelNewGameClick = () => {
if (context.rtmt.connectionState === "connected") {
context.rtmt.sendMessage(channel_cancel_new_game, "");
}
navigation.replace("Home", { context })
}
useEffect(() => {
context.rtmt.addMessageListener(channel_on_game_start, onGameStart);
context.rtmt.sendMessage("new_game", {});
return () => {
context.rtmt.removeMessageListener(channel_on_game_start, onGameStart);
}
}, []);
const textColorOnAppBar = getColorByBrightness(
context.themeManager.theme.appBarBgColor,
context.themeManager.theme.textColor,
context.themeManager.theme.textLightColor
);
const updateHeaderButton = () => {
navigation.setOptions({
headerRight: () => (
<Pressable onPress={() => {
navigation.push("Help", { context })
}}>
<SvgXml xml={helpSvg.replaceAll("customColor", textColorOnAppBar)} width="24" height="24" />
</Pressable>
),
});
}
React.useEffect(() => {
updateHeaderButton();
}, []);
2024-03-24 14:23:08 +03:00
return (
2024-03-31 17:54:07 +03:00
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: context.themeManager.theme?.background }}>
<CircularPanel color={context.themeManager.theme?.boardColor} children={<Text style={{ color: "#000" }}>{t('SearchingOpponent') + " " + t('PleaseWait') + "..."}</Text>} />
<View style={{height: 10}}></View>
<Pressable onPress={onCancelNewGameClick}>
<CircularPanel color={context.themeManager.theme?.boardColor} children={<Text style={{ color: context.themeManager.theme.textColor }}>{t('Cancel')}</Text>} />
</Pressable>
2024-03-24 14:23:08 +03:00
</View>
);
}