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-31 17:29:52 +03:00
|
|
|
import { Context, initContext } from './context/context';
|
2024-03-25 10:04:03 +03:00
|
|
|
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-31 17:20:09 +03:00
|
|
|
import { NativeModules, I18nManager, Platform } from 'react-native'
|
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-04-05 01:41:42 +03:00
|
|
|
import HelpScreen from './screens/HelpScreen';
|
|
|
|
|
import { getColorByBrightness } from './util/ColorUtil';
|
2024-03-27 21:48:48 +03:00
|
|
|
|
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-31 17:20:09 +03:00
|
|
|
|
|
|
|
|
// Get device language
|
|
|
|
|
const deviceLanguage =
|
|
|
|
|
Platform.OS === 'ios'
|
|
|
|
|
? NativeModules.SettingsManager.settings.AppleLocale // iOS
|
|
|
|
|
: NativeModules.I18nManager.localeIdentifier; // Android
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { t, i18n } = useTranslation();
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
i18n.changeLanguage(deviceLanguage === "tr_TR" ? "tr" : "en")
|
|
|
|
|
}, []);
|
2024-03-25 10:04:03 +03:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2024-03-31 17:29:52 +03:00
|
|
|
const onConnectionChange = (_connectionState: ConnectionState) => {
|
|
|
|
|
Snackbar.show({text: getTextByConnectionState(context, _connectionState)})
|
|
|
|
|
return setConnetionState(_connectionState);
|
|
|
|
|
};
|
2024-03-25 10:04:03 +03:00
|
|
|
|
|
|
|
|
const onThemeChange = (theme: Theme) => setTheme(theme);
|
|
|
|
|
|
|
|
|
|
const connectRTMT = (userKey: string) => {
|
2024-03-31 17:20:09 +03:00
|
|
|
const rtmt = context.rtmt as RTMTWS;
|
|
|
|
|
rtmt.on("error", onConnectionError);
|
|
|
|
|
rtmt.on("connectionchange", onConnectionChange)
|
|
|
|
|
rtmt.connectWebSocket(userKey)
|
|
|
|
|
return rtmt;
|
2024-03-25 10:04:03 +03:00
|
|
|
}
|
2024-03-31 17:29:52 +03:00
|
|
|
const getTextByConnectionState = (context: Context, connectionState: ConnectionState): string => {
|
|
|
|
|
const map: { [key: string]: string } = {
|
|
|
|
|
connecting: t('Connecting'),
|
|
|
|
|
connected: t('Connected'),
|
|
|
|
|
error: t('CannotConnect'),
|
|
|
|
|
closed: t('ConnectingAgain'),
|
|
|
|
|
reconnecting: t('ConnectingAgain'),
|
|
|
|
|
};
|
|
|
|
|
return map[connectionState];
|
|
|
|
|
}
|
2024-03-25 10:04:03 +03:00
|
|
|
|
|
|
|
|
const loadUserKeyAndConnectServer = () => {
|
2024-03-31 17:20:09 +03:00
|
|
|
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);
|
|
|
|
|
});
|
2024-03-25 10:04:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const disposeApp = () => {
|
2024-03-31 17:20:09 +03:00
|
|
|
context.rtmt?.dispose();
|
|
|
|
|
context.themeManager.off("themechange", onThemeChange);
|
2024-03-25 10:04:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2024-03-31 17:20:09 +03:00
|
|
|
loadUserKeyAndConnectServer();
|
|
|
|
|
context.themeManager.on("themechange", onThemeChange);
|
|
|
|
|
return () => disposeApp();
|
2024-03-25 10:04:03 +03:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
//const textColorOnBoard = getColorByBrightness(
|
|
|
|
|
// context.themeManager.theme.boardColor,
|
|
|
|
|
// context.themeManager.theme.textColor,
|
|
|
|
|
// context.themeManager.theme.textLightColor
|
|
|
|
|
//);
|
2024-04-05 01:41:42 +03:00
|
|
|
const textColorOnAppBar = getColorByBrightness(
|
|
|
|
|
context.themeManager.theme.appBarBgColor,
|
|
|
|
|
context.themeManager.theme.textColor,
|
|
|
|
|
context.themeManager.theme.textLightColor
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const headerStyle = { backgroundColor: context.themeManager.theme.appBarBgColor };
|
|
|
|
|
const headerTintColor = textColorOnAppBar;
|
|
|
|
|
|
2024-03-24 14:23:08 +03:00
|
|
|
return (
|
|
|
|
|
<NavigationContainer>
|
|
|
|
|
<Stack.Navigator>
|
2024-03-31 17:20:09 +03:00
|
|
|
<Stack.Screen name="Home" component={HomeScreen} initialParams={{ context }}
|
2024-04-05 01:41:42 +03:00
|
|
|
options={{ title: t("Mancala"), headerStyle, headerTintColor }} />
|
2024-03-31 17:20:09 +03:00
|
|
|
<Stack.Screen name="Loby" component={LobyScreen}
|
2024-04-05 01:41:42 +03:00
|
|
|
options={{ title: t("Mancala"), headerStyle, headerTintColor }} />
|
2024-03-31 17:20:09 +03:00
|
|
|
<Stack.Screen name="Game" component={GameScreen}
|
2024-04-05 01:41:42 +03:00
|
|
|
options={{ title: t("Mancala"), headerStyle, headerTintColor }} />
|
|
|
|
|
<Stack.Screen name="Help" component={HelpScreen}
|
|
|
|
|
options={{ title: t("Help"), headerStyle, headerTintColor }} />
|
2024-03-24 14:23:08 +03:00
|
|
|
</Stack.Navigator>
|
|
|
|
|
</NavigationContainer>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App;
|