2019-11-01 08:59:05 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
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:comunic/models/login_tokens.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
/// Preferences helper
|
|
|
|
///
|
|
|
|
/// Stores current account tokens
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
2019-11-01 12:48:40 +00:00
|
|
|
enum PreferencesKeyList { LOGIN_TOKENS, ENABLE_DARK_THEME }
|
2019-11-01 12:20:33 +00:00
|
|
|
|
2019-11-01 12:48:40 +00:00
|
|
|
const _PreferenceKeysName = {
|
|
|
|
PreferencesKeyList.LOGIN_TOKENS: "login_tokens",
|
|
|
|
PreferencesKeyList.ENABLE_DARK_THEME: "dark_theme",
|
|
|
|
};
|
2019-11-01 12:20:33 +00:00
|
|
|
|
2019-11-01 08:59:05 +00:00
|
|
|
class PreferencesHelper {
|
|
|
|
static PreferencesHelper _instance;
|
|
|
|
|
|
|
|
static Future<PreferencesHelper> getInstance() async {
|
|
|
|
if (_instance == null) {
|
|
|
|
_instance = PreferencesHelper._();
|
|
|
|
await _init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SharedPreferences _sharedPreferences;
|
|
|
|
|
|
|
|
PreferencesHelper._();
|
|
|
|
|
|
|
|
static Future<void> _init() async {
|
|
|
|
_sharedPreferences = await SharedPreferences.getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set new login tokens
|
|
|
|
Future<void> setLoginTokens(LoginTokens tokens) async {
|
2019-11-01 12:20:33 +00:00
|
|
|
await setString(PreferencesKeyList.LOGIN_TOKENS,
|
|
|
|
tokens == null ? "null" : tokens.toString());
|
2019-11-01 08:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get current [LoginTokens]. Returns null if none or in case of failure
|
|
|
|
LoginTokens getLoginTokens() {
|
|
|
|
try {
|
2019-11-01 12:20:33 +00:00
|
|
|
final string = getString(PreferencesKeyList.LOGIN_TOKENS);
|
2019-11-01 08:59:05 +00:00
|
|
|
if (string == null || string == "null") return null;
|
|
|
|
return LoginTokens.fromJSON(jsonDecode(string));
|
|
|
|
} on Exception catch (e) {
|
|
|
|
print(e.toString());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-11-01 12:20:33 +00:00
|
|
|
|
2019-11-01 12:48:40 +00:00
|
|
|
Future<bool> setString(PreferencesKeyList key, String value) async {
|
|
|
|
return await _sharedPreferences.setString(_PreferenceKeysName[key], value);
|
2019-11-01 12:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String getString(PreferencesKeyList key) {
|
|
|
|
return _sharedPreferences.getString(_PreferenceKeysName[key]);
|
|
|
|
}
|
2019-11-01 12:48:40 +00:00
|
|
|
|
|
|
|
Future<bool> setBool(PreferencesKeyList key, bool value) async {
|
|
|
|
return await _sharedPreferences.setBool(_PreferenceKeysName[key], value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool getBool(PreferencesKeyList key, {bool alternative = false}) {
|
|
|
|
final v = _sharedPreferences.getBool(_PreferenceKeysName[key]);
|
|
|
|
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-12 17:08:08 +00:00
|
|
|
enableDarkMode: getBool(PreferencesKeyList.ENABLE_DARK_THEME),
|
2020-05-12 17:00:35 +00:00
|
|
|
);
|
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
|
|
|
|
|
|
|
PreferencesHelper preferences() {
|
|
|
|
if (PreferencesHelper._instance == null)
|
|
|
|
throw Exception("Try to get preference before their initialization!");
|
|
|
|
|
|
|
|
return PreferencesHelper._instance;
|
|
|
|
}
|