1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +00:00

Create common preference methods

This commit is contained in:
Pierre HUBERT 2019-11-01 13:20:33 +01:00
parent c367939ab6
commit 9e151639c2

View File

@ -9,6 +9,10 @@ import 'package:shared_preferences/shared_preferences.dart';
///
/// @author Pierre HUBERT
enum PreferencesKeyList { LOGIN_TOKENS }
const _PreferenceKeysName = {PreferencesKeyList.LOGIN_TOKENS: "login_tokens"};
class PreferencesHelper {
static PreferencesHelper _instance;
@ -31,14 +35,14 @@ class PreferencesHelper {
/// Set new login tokens
Future<void> setLoginTokens(LoginTokens tokens) async {
await _sharedPreferences.setString(
"login_tokens", tokens == null ? "null" : tokens.toString());
await setString(PreferencesKeyList.LOGIN_TOKENS,
tokens == null ? "null" : tokens.toString());
}
/// Get current [LoginTokens]. Returns null if none or in case of failure
LoginTokens getLoginTokens() {
try {
final string = _sharedPreferences.getString("login_tokens");
final string = getString(PreferencesKeyList.LOGIN_TOKENS);
if (string == null || string == "null") return null;
return LoginTokens.fromJSON(jsonDecode(string));
} on Exception catch (e) {
@ -46,4 +50,12 @@ class PreferencesHelper {
return null;
}
}
Future<void> setString(PreferencesKeyList key, String value) async {
await _sharedPreferences.setString(_PreferenceKeysName[key], value);
}
String getString(PreferencesKeyList key) {
return _sharedPreferences.getString(_PreferenceKeysName[key]);
}
}