2019-04-22 17:16:26 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:comunic/models/login_tokens.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
2019-04-21 08:34:27 +00:00
|
|
|
/// Accounts credentials helper
|
|
|
|
///
|
|
|
|
/// Stores current account tokens
|
|
|
|
///
|
|
|
|
/// @author Pierre HUBERT
|
|
|
|
|
|
|
|
class AccountCredentialsHelper {
|
|
|
|
|
|
|
|
/// Checkout whether current user is signed in or not
|
|
|
|
Future<bool> signedIn() async {
|
2019-04-22 17:16:26 +00:00
|
|
|
return await get() != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set new login tokens
|
|
|
|
Future<void> set(LoginTokens tokens) async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
await prefs.setString("login_tokens", tokens.toString());
|
2019-04-21 08:34:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 17:16:26 +00:00
|
|
|
/// Get current [LoginTokens]. Returns null if none or in case of failure
|
|
|
|
Future<LoginTokens> get() async {
|
|
|
|
try {
|
|
|
|
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
final string = prefs.getString("login_tokens");
|
|
|
|
if(string == null) return null;
|
|
|
|
return LoginTokens.fromJSON(jsonDecode(string));
|
|
|
|
|
|
|
|
} on Exception catch(e){
|
|
|
|
print(e.toString());
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-04-21 08:34:27 +00:00
|
|
|
}
|