flutter-freaccess-hub/lib/shared/helpers/secure_storage_helper.dart

199 lines
7.1 KiB
Dart

import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hub/shared/utils/storage_util.dart';
import 'package:hub/shared/utils/cache_util.dart';
import 'package:synchronized/synchronized.dart';
class SecureStorageHelper extends ChangeNotifier implements Storage {
static final SecureStorageHelper _instance = SecureStorageHelper._internal();
final FlutterSecureStorage _secureStorage = const FlutterSecureStorage();
static final _lock = Lock();
factory SecureStorageHelper() => _instance;
SecureStorageHelper._internal();
static SecureStorageHelper get instance => _instance;
String? _deviceDescription;
String get deviceDescription => _deviceDescription ?? '';
set deviceDescription(String value) => _setAndCacheString('deviceDescription', value, (v) => _deviceDescription = v);
BuildContext? _context;
BuildContext? get context => _context;
set context(BuildContext? value) => _setAndCacheObject('ff_context', value.toString(), (v) => _context = value);
bool? _haveLocal;
bool? get haveLocal => _haveLocal;
set haveLocal(bool? value) => _setAndCacheBool('ff_have_local', value ?? false, (v) => _haveLocal = value);
String? _fingerprintPass;
String get fingerprintPass => _fingerprintPass ?? '';
set fingerprintPass(String value) => _setAndCacheString('fingerprintPass', value, (v) => _fingerprintPass = v);
String? _accessPass;
String get accessPass => _accessPass ?? '';
set accessPass(String value) => _setAndCacheString('accessPass', value, (v) => _accessPass = v);
String? _panicPass;
String get panicPass => _panicPass ?? '';
set panicPass(String value) => _setAndCacheString('panicPass', value, (v) => _panicPass = v);
String? _tokenAPNS;
String? get tokenAPNS => _tokenAPNS;
set tokenAPNS(String? value) => _setAndCacheString('ff_tokenAPNS', value ?? '', (v) => _tokenAPNS = v);
String? _email;
String get email => _email ?? '';
set email(String value) => _setAndCacheString('ff_email', value, (v) => _email = v);
String? _passwd;
String get passwd => _passwd ?? '';
set passwd(String value) => _setAndCacheString('ff_passwd', value, (v) => _passwd = v);
String? _deviceType;
String get deviceType => _deviceType ?? '';
set deviceType(String value) => _setAndCacheString('ff_deviceType', value, (v) => _deviceType = v);
bool? _isLogged;
bool get isLogged => _isLogged ?? false;
set isLogged(bool value) => _setAndCacheBool('ff_isLogged', value, (v) => _isLogged = v);
String? _token;
String get token => _token ?? '';
set token(String value) => _setAndCacheString('ff_token', value, (v) => _token = v);
Future<void> ensureInitialization() async {
log('SecureStorageHelper: Starting initialization');
await _lock.synchronized(() async {
try {
log('SecureStorageHelper: Calling initFunction');
_email = await _getString('ff_email');
log('SecureStorageHelper: _email = $_email');
_passwd = await _getString('ff_passwd');
log('SecureStorageHelper: _passwd = $_passwd');
_deviceType = await _getString('ff_deviceType');
log('SecureStorageHelper: _deviceType = $_deviceType');
_isLogged = await _getBool('ff_isLogged');
log('SecureStorageHelper: _isLogged = $_isLogged');
_tokenAPNS = await _getString('ff_tokenAPNS');
log('SecureStorageHelper: _tokenAPNS = $_tokenAPNS');
_accessPass = await _getString('accessPass');
log('SecureStorageHelper: _accessPass = $_accessPass');
_panicPass = await _getString('panicPass');
log('SecureStorageHelper: _panicPass = $_panicPass');
_fingerprintPass = await _getString('fingerprintPass');
log('SecureStorageHelper: _fingerprintPass = $_fingerprintPass');
_context = await _getObject('ff_context');
log('SecureStorageHelper: _context = $_context');
_haveLocal = await _getBool('ff_have_local');
log('SecureStorageHelper: _haveLocal = $_haveLocal');
_deviceDescription = await _getString('deviceDescription');
log('SecureStorageHelper: _deviceDescription = $_deviceDescription');
log('SecureStorageHelper: initFunction completed successfully');
} catch (e) {
log('SecureStorageHelper: Error during initialization: $e');
}
});
log('SecureStorageHelper: Initialization complete');
}
Future<void> _setAndCacheString(String key, String value, Function(String) cacheSetter) async {
await _lock.synchronized(() async {
await _secureStorage.write(key: key, value: value);
CacheUtil.instance.set(key, value);
cacheSetter(value);
});
}
Future<void> _setAndCacheBool(String key, bool value, Function(bool) cacheSetter) async {
await _lock.synchronized(() async {
await _secureStorage.write(key: key, value: value.toString());
CacheUtil.instance.set(key, value);
cacheSetter(value);
});
}
Future<void> _setAndCacheObject(String key, String value, Function(String) cacheSetter) async {
await _lock.synchronized(() async {
await _secureStorage.write(key: key, value: value);
CacheUtil.instance.set(key, value);
cacheSetter(value);
});
}
Future<String?> _getString(String key) async {
return await _lock.synchronized(() async {
var value = CacheUtil.instance.get(key);
if (value == null) {
value = await _secureStorage.read(key: key);
CacheUtil.instance.set(key, value);
}
return value;
});
}
Future<bool?> _getBool(String key) async {
return await _lock.synchronized(() async {
var value = CacheUtil.instance.get(key);
if (value == null) {
value = await _secureStorage.read(key: key);
CacheUtil.instance.set(key, value == 'true');
}
return value == 'true';
});
}
Future<BuildContext?> _getObject(String key) async {
return await _lock.synchronized(() async {
var value = CacheUtil.instance.get(key);
if (value == null) {
value = await _secureStorage.read(key: key);
CacheUtil.instance.set(key, value);
}
return value as BuildContext?;
});
}
@override
Future<void> set(String key, dynamic value) async {
if (value is String) {
await _setAndCacheString(key, value, (v) {});
} else if (value is bool) {
await _setAndCacheBool(key, value, (v) {});
} else if (value is BuildContext) {
await _setAndCacheObject(key, value.toString(), (v) {});
}
}
@override
Future<dynamic> get(String key) async {
var stringValue = await _getString(key);
if (stringValue != null) return stringValue;
var boolValue = await _getBool(key);
if (boolValue != null) return boolValue;
var objectValue = await _getObject(key);
if (objectValue != null) return objectValue;
return null;
}
@override
Future<void> delete(String key) async {
await _lock.synchronized(() async {
await _secureStorage.delete(key: key);
CacheUtil.instance.delete(key);
});
}
Future<void> purge() async {
await _lock.synchronized(() async {
await _secureStorage.deleteAll();
CacheUtil.instance.clear();
});
}
}