2024-03-24 14:23:08 +03:00
|
|
|
|
|
|
|
|
import * as React from 'react';
|
2024-03-25 10:04:03 +03:00
|
|
|
import { NavigationContainer, Theme } from '@react-navigation/native';
|
2024-03-24 14:23:08 +03:00
|
|
|
import { createNativeStackNavigator } from '@react-navigation/native-stack';
|
|
|
|
|
import { RootStackParamList } from './types';
|
|
|
|
|
import { HomeScreen } from './screens/HomeScreen';
|
|
|
|
|
import LobyScreen from './screens/LobyScreen';
|
2024-03-25 10:04:03 +03:00
|
|
|
import { initContext } from './context/context';
|
|
|
|
|
import { GameScreen } from './screens/GameScreen';
|
|
|
|
|
import { RTMTWS } from './rtmt/rtmt_websocket';
|
|
|
|
|
import { ConnectionState } from './rtmt/rtmt';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import Snackbar from 'react-native-snackbar';
|
2024-03-24 14:23:08 +03:00
|
|
|
|
2024-03-27 21:48:48 +03:00
|
|
|
// https://github.com/uuidjs/uuid/issues/514#issuecomment-691475020
|
|
|
|
|
import 'react-native-get-random-values';
|
|
|
|
|
|
2024-03-24 14:23:08 +03:00
|
|
|
const Stack = createNativeStackNavigator<RootStackParamList>();
|
|
|
|
|
|
2024-03-25 10:04:03 +03:00
|
|
|
const context = initContext();
|
|
|
|
|
|
2024-03-24 14:23:08 +03:00
|
|
|
function App() {
|
2024-03-25 10:04:03 +03:00
|
|
|
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const [userKey, setUserKey] = React.useState<string | undefined>(undefined);
|
|
|
|
|
|
|
|
|
|
const [connectionState, setConnetionState] = React.useState<ConnectionState>("connecting");
|
|
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
|
const [theme, setTheme] = React.useState<Theme>(context.themeManager.theme);
|
|
|
|
|
|
|
|
|
|
const onConnectionError = (event: Event) => console.error("(RTMT) Connection Error: ", event);
|
|
|
|
|
|
|
|
|
|
const onConnectionChange = (_connectionState: ConnectionState) => setConnetionState(_connectionState);
|
|
|
|
|
|
|
|
|
|
const onThemeChange = (theme: Theme) => setTheme(theme);
|
|
|
|
|
|
|
|
|
|
const connectRTMT = (userKey: string) => {
|
|
|
|
|
const rtmt = context.rtmt as RTMTWS;
|
|
|
|
|
rtmt.on("error", onConnectionError);
|
|
|
|
|
rtmt.on("connectionchange", onConnectionChange)
|
|
|
|
|
rtmt.connectWebSocket(userKey)
|
|
|
|
|
return rtmt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const loadUserKeyAndConnectServer = () => {
|
|
|
|
|
context.userKeyStore.getUserKey().then((userKey: string) => {
|
|
|
|
|
setUserKey(userKey);
|
|
|
|
|
connectRTMT(userKey);
|
|
|
|
|
}).catch((error) => {
|
|
|
|
|
//TODO: check if it is network error!
|
|
|
|
|
Snackbar.show({text: t("ErrorWhenRetrievingInformation")})
|
|
|
|
|
console.error(error);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const disposeApp = () => {
|
|
|
|
|
context.rtmt?.dispose();
|
|
|
|
|
context.themeManager.off("themechange", onThemeChange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
loadUserKeyAndConnectServer();
|
|
|
|
|
context.themeManager.on("themechange", onThemeChange);
|
|
|
|
|
return () => disposeApp();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
//const textColorOnBoard = getColorByBrightness(
|
|
|
|
|
// context.themeManager.theme.boardColor,
|
|
|
|
|
// context.themeManager.theme.textColor,
|
|
|
|
|
// context.themeManager.theme.textLightColor
|
|
|
|
|
//);
|
|
|
|
|
|
2024-03-24 14:23:08 +03:00
|
|
|
return (
|
|
|
|
|
<NavigationContainer>
|
|
|
|
|
<Stack.Navigator>
|
2024-03-25 10:04:03 +03:00
|
|
|
<Stack.Screen name="Home" component={HomeScreen} initialParams={{context}} />
|
2024-03-24 14:23:08 +03:00
|
|
|
<Stack.Screen name="Loby" component={LobyScreen} />
|
2024-03-25 10:04:03 +03:00
|
|
|
<Stack.Screen name="Game" component={GameScreen} />
|
2024-03-24 14:23:08 +03:00
|
|
|
</Stack.Navigator>
|
|
|
|
|
</NavigationContainer>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|