diff --git a/lib/helpers/account_helper.dart b/lib/helpers/account_helper.dart index 3f1362a..10838f9 100644 --- a/lib/helpers/account_helper.dart +++ b/lib/helpers/account_helper.dart @@ -5,7 +5,6 @@ import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/authentication_details.dart'; import 'package:comunic/models/new_account.dart'; import 'package:comunic/models/res_check_password_reset_token.dart'; -import 'package:shared_preferences/shared_preferences.dart'; /// Account helper /// @@ -26,8 +25,6 @@ enum CreateAccountResult { } class AccountHelper { - static const _USER_ID_PREFERENCE_NAME = "user_id"; - // Current user ID static int _currentUserID = -1; @@ -71,8 +68,8 @@ class AccountHelper { } // Save current user ID - final preferences = await SharedPreferences.getInstance(); - await preferences.setInt(_USER_ID_PREFERENCE_NAME, userID); + final preferences = await PreferencesHelper.getInstance(); + await preferences.setInt(PreferencesKeyList.USER_ID, userID); _currentUserID = userID; return AuthResult.SUCCESS; @@ -82,7 +79,9 @@ class AccountHelper { Future signOut() async { await APIRequest.withLogin("account/logout").exec(); - await (await PreferencesHelper.getInstance()).setLoginToken(null); + final preferencesHelper = await PreferencesHelper.getInstance(); + await preferencesHelper.setLoginToken(null); + await preferencesHelper.setInt(PreferencesKeyList.USER_ID, -1); _currentUserID = 0; // Close current web socket @@ -202,8 +201,8 @@ class AccountHelper { /// Get the ID of the currently signed in user Future _loadCurrentUserID() async { - final preferences = await SharedPreferences.getInstance(); - _currentUserID = preferences.getInt(_USER_ID_PREFERENCE_NAME); + final preferences = await PreferencesHelper.getInstance(); + _currentUserID = preferences.getInt(PreferencesKeyList.USER_ID); } /// Check if current user ID is loaded or not diff --git a/lib/helpers/preferences_helper.dart b/lib/helpers/preferences_helper.dart index 5398248..d9a8648 100644 --- a/lib/helpers/preferences_helper.dart +++ b/lib/helpers/preferences_helper.dart @@ -8,6 +8,7 @@ import 'package:shared_preferences/shared_preferences.dart'; /// @author Pierre HUBERT enum PreferencesKeyList { + USER_ID, LOGIN_TOKEN, ENABLE_DARK_THEME, FORCE_MOBILE_MODE, @@ -15,6 +16,7 @@ enum PreferencesKeyList { } const _PreferenceKeysName = { + PreferencesKeyList.USER_ID: "user_id", PreferencesKeyList.LOGIN_TOKEN: "login_token", PreferencesKeyList.ENABLE_DARK_THEME: "dark_theme", PreferencesKeyList.FORCE_MOBILE_MODE: "force_mobile_mode", @@ -76,6 +78,14 @@ class PreferencesHelper { return await _sharedPreferences.setBool(_PreferenceKeysName[key], value); } + Future setInt(PreferencesKeyList key, int value) async { + return await _sharedPreferences.setInt(_PreferenceKeysName[key], value); + } + + int getInt(PreferencesKeyList key) { + return _sharedPreferences.getInt(_PreferenceKeysName[key]); + } + bool getBool(PreferencesKeyList key, {bool alternative = false}) { final v = _sharedPreferences.getBool(_PreferenceKeysName[key]); return v == null ? alternative : v; diff --git a/lib/main.dart b/lib/main.dart index 3868258..dd7ba4e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,6 @@ import 'package:comunic/helpers/account_helper.dart'; import 'package:comunic/helpers/database/database_helper.dart'; import 'package:comunic/helpers/preferences_helper.dart'; -import 'package:comunic/ui/routes/login_route.dart'; import 'package:comunic/ui/widgets/init_widget.dart'; import 'package:comunic/utils/intl_utils.dart'; import 'package:flutter/material.dart'; @@ -51,7 +50,7 @@ class ComunicApplicationState extends State { return MaterialApp( debugShowCheckedModeBanner: false, - home: AccountHelper.isUserIDLoaded ? InitializeWidget() : LoginRoute(), + home: InitializeWidget(), theme: prefs.enableDarkMode ? ThemeData.dark() : null, showPerformanceOverlay: prefs.showPerformancesOverlay, ); diff --git a/lib/ui/routes/main_route/main_route.dart b/lib/ui/routes/main_route/main_route.dart index 35d5f66..ea053d3 100644 --- a/lib/ui/routes/main_route/main_route.dart +++ b/lib/ui/routes/main_route/main_route.dart @@ -1,6 +1,5 @@ import 'package:comunic/helpers/account_helper.dart'; import 'package:comunic/ui/routes/conversation_route.dart'; -import 'package:comunic/ui/routes/login_route.dart'; import 'package:comunic/ui/routes/main_route/page_info.dart'; import 'package:comunic/ui/routes/settings/account_settings_route.dart'; import 'package:comunic/ui/screens/call_screen.dart'; @@ -170,10 +169,6 @@ abstract class MainController extends State { popUntilMainRoute(); await AccountHelper().signOut(); - - Navigator.pushReplacement(context, MaterialPageRoute(builder: (c) { - return LoginRoute(); - })); } /// Pop current page. Last page can not be popped diff --git a/lib/ui/widgets/init_widget.dart b/lib/ui/widgets/init_widget.dart index c83f5b0..b2e92a1 100644 --- a/lib/ui/widgets/init_widget.dart +++ b/lib/ui/widgets/init_widget.dart @@ -1,4 +1,6 @@ +import 'package:comunic/helpers/account_helper.dart'; import 'package:comunic/helpers/events_helper.dart'; +import 'package:comunic/helpers/server_config_helper.dart'; import 'package:comunic/helpers/websocket_helper.dart'; import 'package:comunic/ui/routes/login_route.dart'; import 'package:comunic/ui/routes/main_route/main_route.dart'; @@ -57,6 +59,14 @@ class _InitializeWidgetState extends SafeState { /// Try to connect to server void _tryConnect() async { try { + await ServerConfigurationHelper.ensureLoaded(); + + if (!AccountHelper.isUserIDLoaded) { + _popToMainRoute(); + _openLoginPage(); + return; + } + print("Attempting WebSocket connection..."); setState(() {