mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-26 06:49:22 +00:00
Updated application preferences logic
This commit is contained in:
parent
53f4240f05
commit
a998ce13a3
@ -1,33 +0,0 @@
|
|||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
import 'package:comunic/models/login_tokens.dart';
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
|
|
||||||
/// Accounts credentials helper
|
|
||||||
///
|
|
||||||
/// Stores current account tokens
|
|
||||||
///
|
|
||||||
/// @author Pierre HUBERT
|
|
||||||
|
|
||||||
class AccountCredentialsHelper {
|
|
||||||
|
|
||||||
/// Set new login tokens
|
|
||||||
Future<void> set(LoginTokens tokens) async {
|
|
||||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
||||||
await prefs.setString(
|
|
||||||
"login_tokens", tokens == null ? "null" : tokens.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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 || string == "null") return null;
|
|
||||||
return LoginTokens.fromJSON(jsonDecode(string));
|
|
||||||
} on Exception catch (e) {
|
|
||||||
print(e.toString());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
import 'package:comunic/helpers/account_credentials_helper.dart';
|
|
||||||
import 'package:comunic/helpers/api_helper.dart';
|
import 'package:comunic/helpers/api_helper.dart';
|
||||||
|
import 'package:comunic/helpers/preferences_helper.dart';
|
||||||
import 'package:comunic/models/api_request.dart';
|
import 'package:comunic/models/api_request.dart';
|
||||||
import 'package:comunic/models/authentication_details.dart';
|
import 'package:comunic/models/authentication_details.dart';
|
||||||
import 'package:comunic/models/login_tokens.dart';
|
import 'package:comunic/models/login_tokens.dart';
|
||||||
@ -26,7 +26,8 @@ class AccountHelper {
|
|||||||
///
|
///
|
||||||
/// Warning : This method MUST BE CALLED AT LEAST ONCE AFTER APP START !!!
|
/// Warning : This method MUST BE CALLED AT LEAST ONCE AFTER APP START !!!
|
||||||
Future<bool> signedIn() async {
|
Future<bool> signedIn() async {
|
||||||
bool signedIn = await AccountCredentialsHelper().get() != null;
|
bool signedIn =
|
||||||
|
(await PreferencesHelper.getInstance()).getLoginTokens() != null;
|
||||||
|
|
||||||
// Load current user ID for later use
|
// Load current user ID for later use
|
||||||
if (signedIn && _currentUserID == -1) await _loadCurrentUserID();
|
if (signedIn && _currentUserID == -1) await _loadCurrentUserID();
|
||||||
@ -51,8 +52,8 @@ class AccountHelper {
|
|||||||
|
|
||||||
// Save login tokens
|
// Save login tokens
|
||||||
final tokensObj = response.getObject()["tokens"];
|
final tokensObj = response.getObject()["tokens"];
|
||||||
await AccountCredentialsHelper()
|
await (await PreferencesHelper.getInstance())
|
||||||
.set(LoginTokens(tokensObj["token1"], tokensObj["token2"]));
|
.setLoginTokens(LoginTokens(tokensObj["token1"], tokensObj["token2"]));
|
||||||
|
|
||||||
// Get current user ID
|
// Get current user ID
|
||||||
final userID = await _downloadCurrentUserID();
|
final userID = await _downloadCurrentUserID();
|
||||||
@ -71,7 +72,7 @@ class AccountHelper {
|
|||||||
|
|
||||||
/// Sign out user
|
/// Sign out user
|
||||||
Future<void> signOut() async {
|
Future<void> signOut() async {
|
||||||
await AccountCredentialsHelper().set(null);
|
await (await PreferencesHelper.getInstance()).setLoginTokens(null);
|
||||||
_currentUserID = 0;
|
_currentUserID = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:comunic/helpers/account_credentials_helper.dart';
|
import 'package:comunic/helpers/preferences_helper.dart';
|
||||||
import 'package:comunic/models/api_request.dart';
|
import 'package:comunic/models/api_request.dart';
|
||||||
import 'package:comunic/models/api_response.dart';
|
import 'package:comunic/models/api_response.dart';
|
||||||
import 'package:comunic/models/config.dart';
|
import 'package:comunic/models/config.dart';
|
||||||
@ -23,7 +23,7 @@ class APIHelper {
|
|||||||
|
|
||||||
//Add user tokens (if required)
|
//Add user tokens (if required)
|
||||||
if (request.needLogin) {
|
if (request.needLogin) {
|
||||||
final tokens = await AccountCredentialsHelper().get();
|
final tokens = (await PreferencesHelper.getInstance()).getLoginTokens();
|
||||||
assert(tokens != null);
|
assert(tokens != null);
|
||||||
request.addString("userToken1", tokens.tokenOne);
|
request.addString("userToken1", tokens.tokenOne);
|
||||||
request.addString("userToken2", tokens.tokenTwo);
|
request.addString("userToken2", tokens.tokenTwo);
|
||||||
|
49
lib/helpers/preferences_helper.dart
Normal file
49
lib/helpers/preferences_helper.dart
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:comunic/models/login_tokens.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
/// Preferences helper
|
||||||
|
///
|
||||||
|
/// Stores current account tokens
|
||||||
|
///
|
||||||
|
/// @author Pierre HUBERT
|
||||||
|
|
||||||
|
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 {
|
||||||
|
await _sharedPreferences.setString(
|
||||||
|
"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");
|
||||||
|
if (string == null || string == "null") return null;
|
||||||
|
return LoginTokens.fromJSON(jsonDecode(string));
|
||||||
|
} on Exception catch (e) {
|
||||||
|
print(e.toString());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user