2020-05-12 17:00:35 +00:00
|
|
|
import 'package:comunic/models/application_preferences.dart';
|
2019-11-01 08:59:05 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
/// Preferences helper
|
|
|
|
///
|
|
|
|
/// Stores current account tokens
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
2020-05-13 16:17:50 +00:00
|
|
|
enum PreferencesKeyList {
|
2021-02-19 16:32:38 +00:00
|
|
|
USER_ID,
|
2021-02-13 15:03:07 +00:00
|
|
|
LOGIN_TOKEN,
|
2020-05-13 16:17:50 +00:00
|
|
|
ENABLE_DARK_THEME,
|
|
|
|
FORCE_MOBILE_MODE,
|
2020-05-13 16:26:21 +00:00
|
|
|
SHOW_PERFORMANCE_OVERLAY,
|
2021-04-12 17:26:05 +00:00
|
|
|
PUSH_NOTIFICATIONS_STATUS,
|
2021-04-17 16:05:33 +00:00
|
|
|
IS_TOUR_SEEN,
|
2021-04-23 17:32:34 +00:00
|
|
|
FOREZ_GROUP,
|
2020-05-13 16:17:50 +00:00
|
|
|
}
|
2019-11-01 12:20:33 +00:00
|
|
|
|
2019-11-01 12:48:40 +00:00
|
|
|
const _PreferenceKeysName = {
|
2021-02-19 16:32:38 +00:00
|
|
|
PreferencesKeyList.USER_ID: "user_id",
|
2021-02-13 15:03:07 +00:00
|
|
|
PreferencesKeyList.LOGIN_TOKEN: "login_token",
|
2019-11-01 12:48:40 +00:00
|
|
|
PreferencesKeyList.ENABLE_DARK_THEME: "dark_theme",
|
2020-05-13 16:17:50 +00:00
|
|
|
PreferencesKeyList.FORCE_MOBILE_MODE: "force_mobile_mode",
|
2020-05-13 16:26:21 +00:00
|
|
|
PreferencesKeyList.SHOW_PERFORMANCE_OVERLAY: "perfs_overlay",
|
2021-04-12 17:34:06 +00:00
|
|
|
PreferencesKeyList.PUSH_NOTIFICATIONS_STATUS: "push_notifications_status",
|
2021-04-17 16:05:33 +00:00
|
|
|
PreferencesKeyList.IS_TOUR_SEEN: "is_tour_seen",
|
2021-04-23 17:32:34 +00:00
|
|
|
PreferencesKeyList.FOREZ_GROUP: "forez_group",
|
2019-11-01 12:48:40 +00:00
|
|
|
};
|
2019-11-01 12:20:33 +00:00
|
|
|
|
2019-11-01 08:59:05 +00:00
|
|
|
class PreferencesHelper {
|
2022-03-10 18:39:57 +00:00
|
|
|
static PreferencesHelper? _instance;
|
2019-11-01 08:59:05 +00:00
|
|
|
|
|
|
|
static Future<PreferencesHelper> getInstance() async {
|
|
|
|
if (_instance == null) {
|
|
|
|
_instance = PreferencesHelper._();
|
|
|
|
await _init();
|
|
|
|
}
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
return _instance!;
|
2019-11-01 08:59:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
static late SharedPreferences _sharedPreferences;
|
2019-11-01 08:59:05 +00:00
|
|
|
|
|
|
|
PreferencesHelper._();
|
|
|
|
|
|
|
|
static Future<void> _init() async {
|
|
|
|
_sharedPreferences = await SharedPreferences.getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set new login tokens
|
2022-03-10 18:39:57 +00:00
|
|
|
Future<void> setLoginToken(String? token) async {
|
2021-02-13 15:03:07 +00:00
|
|
|
if (token != null)
|
|
|
|
await setString(PreferencesKeyList.LOGIN_TOKEN, token);
|
|
|
|
else
|
2021-02-18 17:27:24 +00:00
|
|
|
await removeKey(PreferencesKeyList.LOGIN_TOKEN);
|
2019-11-01 08:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get current [LoginTokens]. Returns null if none or in case of failure
|
2022-03-10 18:39:57 +00:00
|
|
|
String? getLoginToken() {
|
2019-11-01 08:59:05 +00:00
|
|
|
try {
|
2021-02-13 15:03:07 +00:00
|
|
|
final string = getString(PreferencesKeyList.LOGIN_TOKEN);
|
|
|
|
return string;
|
2019-11-01 08:59:05 +00:00
|
|
|
} on Exception catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-11-01 12:20:33 +00:00
|
|
|
|
2021-04-12 17:26:05 +00:00
|
|
|
bool containsKey(PreferencesKeyList key) {
|
2022-03-10 18:39:57 +00:00
|
|
|
return _sharedPreferences.containsKey(_PreferenceKeysName[key]!);
|
2021-04-12 17:26:05 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 15:03:07 +00:00
|
|
|
Future<bool> removeKey(PreferencesKeyList key) async {
|
2022-03-10 18:39:57 +00:00
|
|
|
return await _sharedPreferences.remove(_PreferenceKeysName[key]!);
|
2021-02-13 15:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 12:48:40 +00:00
|
|
|
Future<bool> setString(PreferencesKeyList key, String value) async {
|
2022-03-10 18:39:57 +00:00
|
|
|
return await _sharedPreferences.setString(_PreferenceKeysName[key]!, value);
|
2019-11-01 12:20:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
String? getString(PreferencesKeyList key) {
|
|
|
|
return _sharedPreferences.getString(_PreferenceKeysName[key]!);
|
2019-11-01 12:20:33 +00:00
|
|
|
}
|
2019-11-01 12:48:40 +00:00
|
|
|
|
|
|
|
Future<bool> setBool(PreferencesKeyList key, bool value) async {
|
2022-03-10 18:39:57 +00:00
|
|
|
return await _sharedPreferences.setBool(_PreferenceKeysName[key]!, value);
|
2019-11-01 12:48:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 16:32:38 +00:00
|
|
|
Future<bool> setInt(PreferencesKeyList key, int value) async {
|
2022-03-10 18:39:57 +00:00
|
|
|
return await _sharedPreferences.setInt(_PreferenceKeysName[key]!, value);
|
2021-02-19 16:32:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
int? getInt(PreferencesKeyList key) {
|
|
|
|
return _sharedPreferences.getInt(_PreferenceKeysName[key]!);
|
2021-02-19 16:32:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 12:48:40 +00:00
|
|
|
bool getBool(PreferencesKeyList key, {bool alternative = false}) {
|
2022-03-10 18:39:57 +00:00
|
|
|
final v = _sharedPreferences.getBool(_PreferenceKeysName[key]!);
|
2019-11-01 12:48:40 +00:00
|
|
|
return v == null ? alternative : v;
|
|
|
|
}
|
2020-05-12 17:00:35 +00:00
|
|
|
|
|
|
|
/// Get all settings as an [ApplicationPreferences] object
|
|
|
|
ApplicationPreferences get preferences => ApplicationPreferences(
|
2020-05-13 16:26:21 +00:00
|
|
|
enableDarkMode: getBool(PreferencesKeyList.ENABLE_DARK_THEME),
|
|
|
|
forceMobileMode: getBool(PreferencesKeyList.FORCE_MOBILE_MODE),
|
|
|
|
showPerformancesOverlay:
|
|
|
|
getBool(PreferencesKeyList.SHOW_PERFORMANCE_OVERLAY));
|
2020-05-12 17:27:23 +00:00
|
|
|
|
|
|
|
/// Apply new preferences
|
|
|
|
Future<void> setPreferences(ApplicationPreferences preferences) async {
|
|
|
|
await setBool(
|
|
|
|
PreferencesKeyList.ENABLE_DARK_THEME, preferences.enableDarkMode);
|
|
|
|
}
|
2019-11-01 08:59:05 +00:00
|
|
|
}
|
2019-11-01 13:17:46 +00:00
|
|
|
|
2022-03-10 18:39:57 +00:00
|
|
|
PreferencesHelper? preferences() {
|
2019-11-01 13:17:46 +00:00
|
|
|
if (PreferencesHelper._instance == null)
|
|
|
|
throw Exception("Try to get preference before their initialization!");
|
|
|
|
|
|
|
|
return PreferencesHelper._instance;
|
|
|
|
}
|