mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-25 22:39:22 +00:00
Load configuration at application startup
This commit is contained in:
parent
459757b292
commit
613ceadfaa
@ -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<void> 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<void> _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
|
||||
|
@ -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<bool> 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;
|
||||
|
@ -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<ComunicApplication> {
|
||||
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: AccountHelper.isUserIDLoaded ? InitializeWidget() : LoginRoute(),
|
||||
home: InitializeWidget(),
|
||||
theme: prefs.enableDarkMode ? ThemeData.dark() : null,
|
||||
showPerformanceOverlay: prefs.showPerformancesOverlay,
|
||||
);
|
||||
|
@ -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<MainRoute> {
|
||||
popUntilMainRoute();
|
||||
|
||||
await AccountHelper().signOut();
|
||||
|
||||
Navigator.pushReplacement(context, MaterialPageRoute(builder: (c) {
|
||||
return LoginRoute();
|
||||
}));
|
||||
}
|
||||
|
||||
/// Pop current page. Last page can not be popped
|
||||
|
@ -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<InitializeWidget> {
|
||||
/// Try to connect to server
|
||||
void _tryConnect() async {
|
||||
try {
|
||||
await ServerConfigurationHelper.ensureLoaded();
|
||||
|
||||
if (!AccountHelper.isUserIDLoaded) {
|
||||
_popToMainRoute();
|
||||
_openLoginPage();
|
||||
return;
|
||||
}
|
||||
|
||||
print("Attempting WebSocket connection...");
|
||||
|
||||
setState(() {
|
||||
|
Loading…
Reference in New Issue
Block a user