From d21b9df2cce7d3b2fefb1f35958f90cde4434b0a Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Sun, 31 Mar 2024 17:29:52 +0300 Subject: [PATCH] show connection state --- mobile/src/App.tsx | 19 +++++++++++++++---- mobile/src/screens/LobyScreen.tsx | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/mobile/src/App.tsx b/mobile/src/App.tsx index 89c19a0..35f2003 100644 --- a/mobile/src/App.tsx +++ b/mobile/src/App.tsx @@ -5,7 +5,7 @@ import { createNativeStackNavigator } from '@react-navigation/native-stack'; import { RootStackParamList } from './types'; import { HomeScreen } from './screens/HomeScreen'; import LobyScreen from './screens/LobyScreen'; -import { initContext } from './context/context'; +import { Context, initContext } from './context/context'; import { GameScreen } from './screens/GameScreen'; import { RTMTWS } from './rtmt/rtmt_websocket'; import { ConnectionState } from './rtmt/rtmt'; @@ -32,8 +32,6 @@ function App() { const { t, i18n } = useTranslation(); React.useEffect(() => { - console.log(deviceLanguage); - i18n.changeLanguage(deviceLanguage === "tr_TR" ? "tr" : "en") }, []); @@ -46,7 +44,10 @@ function App() { const onConnectionError = (event: Event) => console.error("(RTMT) Connection Error: ", event); - const onConnectionChange = (_connectionState: ConnectionState) => setConnetionState(_connectionState); + const onConnectionChange = (_connectionState: ConnectionState) => { + Snackbar.show({text: getTextByConnectionState(context, _connectionState)}) + return setConnetionState(_connectionState); + }; const onThemeChange = (theme: Theme) => setTheme(theme); @@ -57,6 +58,16 @@ function App() { rtmt.connectWebSocket(userKey) return rtmt; } + 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]; +} const loadUserKeyAndConnectServer = () => { context.userKeyStore.getUserKey().then((userKey: string) => { diff --git a/mobile/src/screens/LobyScreen.tsx b/mobile/src/screens/LobyScreen.tsx index 9141323..031cf62 100644 --- a/mobile/src/screens/LobyScreen.tsx +++ b/mobile/src/screens/LobyScreen.tsx @@ -27,7 +27,7 @@ export default function LobyScreen({ navigation, route }: LobyScreenProps) { return ( - {t('SearchingOpponent')} + {t('SearchingOpponent')} ); } \ No newline at end of file