mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 21:09:21 +00:00
Create common preference methods
This commit is contained in:
parent
c367939ab6
commit
9e151639c2
@ -9,6 +9,10 @@ import 'package:shared_preferences/shared_preferences.dart';
|
|||||||
///
|
///
|
||||||
/// @author Pierre HUBERT
|
/// @author Pierre HUBERT
|
||||||
|
|
||||||
|
enum PreferencesKeyList { LOGIN_TOKENS }
|
||||||
|
|
||||||
|
const _PreferenceKeysName = {PreferencesKeyList.LOGIN_TOKENS: "login_tokens"};
|
||||||
|
|
||||||
class PreferencesHelper {
|
class PreferencesHelper {
|
||||||
static PreferencesHelper _instance;
|
static PreferencesHelper _instance;
|
||||||
|
|
||||||
@ -31,14 +35,14 @@ class PreferencesHelper {
|
|||||||
|
|
||||||
/// Set new login tokens
|
/// Set new login tokens
|
||||||
Future<void> setLoginTokens(LoginTokens tokens) async {
|
Future<void> setLoginTokens(LoginTokens tokens) async {
|
||||||
await _sharedPreferences.setString(
|
await setString(PreferencesKeyList.LOGIN_TOKENS,
|
||||||
"login_tokens", tokens == null ? "null" : tokens.toString());
|
tokens == null ? "null" : tokens.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get current [LoginTokens]. Returns null if none or in case of failure
|
/// Get current [LoginTokens]. Returns null if none or in case of failure
|
||||||
LoginTokens getLoginTokens() {
|
LoginTokens getLoginTokens() {
|
||||||
try {
|
try {
|
||||||
final string = _sharedPreferences.getString("login_tokens");
|
final string = getString(PreferencesKeyList.LOGIN_TOKENS);
|
||||||
if (string == null || string == "null") return null;
|
if (string == null || string == "null") return null;
|
||||||
return LoginTokens.fromJSON(jsonDecode(string));
|
return LoginTokens.fromJSON(jsonDecode(string));
|
||||||
} on Exception catch (e) {
|
} on Exception catch (e) {
|
||||||
@ -46,4 +50,12 @@ class PreferencesHelper {
|
|||||||
return null;
|
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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user