WIP
This commit is contained in:
parent
43f5839db0
commit
12535eb7e8
|
@ -132,7 +132,6 @@ class SignInTemplateComponentModel
|
||||||
await checkLocals(context: context, model: model).then((value) {
|
await checkLocals(context: context, model: model).then((value) {
|
||||||
SecureStorageHelper().haveLocal = value;
|
SecureStorageHelper().haveLocal = value;
|
||||||
SecureStorageHelper().isLogged = isLogged;
|
SecureStorageHelper().isLogged = isLogged;
|
||||||
SecureStorageHelper().update(() {});
|
|
||||||
toggleApp(context);
|
toggleApp(context);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|
185
lib/main.dart
185
lib/main.dart
|
@ -18,33 +18,63 @@ import 'package:hub/flutter_flow/nav/nav.dart';
|
||||||
import 'package:hub/shared/helpers/secure_storage_helper.dart';
|
import 'package:hub/shared/helpers/secure_storage_helper.dart';
|
||||||
import 'package:hub/shared/helpers/shared_preferences_storage_helper.dart';
|
import 'package:hub/shared/helpers/shared_preferences_storage_helper.dart';
|
||||||
import 'package:hub/shared/helpers/sqlite_storage_helper.dart';
|
import 'package:hub/shared/helpers/sqlite_storage_helper.dart';
|
||||||
import 'package:provider/provider.dart';
|
|
||||||
import 'package:responsive_framework/responsive_framework.dart';
|
import 'package:responsive_framework/responsive_framework.dart';
|
||||||
|
|
||||||
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||||
|
|
||||||
Future<void> initializeApp() async {
|
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
|
||||||
await SQLiteStorageHelper().database;
|
|
||||||
SQLiteStorageHelper().deleteDatabaseDB();
|
|
||||||
final SharedPreferencesStorageHelper sharedPreferencesHelper =
|
|
||||||
SharedPreferencesStorageHelper();
|
|
||||||
final SecureStorageHelper secureStorageHelper = SecureStorageHelper();
|
|
||||||
|
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
await _initializeApp();
|
||||||
|
runApp(const App());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Future<void> _initializeApp() async {
|
||||||
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
await _initializeStorageHelpers();
|
||||||
|
await _initializeTracking();
|
||||||
|
await _initializeFirebase();
|
||||||
|
await _initializeNotificationService();
|
||||||
|
_initializeUrlStrategy();
|
||||||
|
_initializeSystemSettings();
|
||||||
|
await _initializeFlutterFlow();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Future<void> _initializeStorageHelpers() async {
|
||||||
|
final SharedPreferencesStorageHelper sharedPreferencesHelper = SharedPreferencesStorageHelper();
|
||||||
|
final SecureStorageHelper secureStorageHelper = SecureStorageHelper();
|
||||||
|
final SQLiteStorageHelper sqLiteStorageHelper = SQLiteStorageHelper();
|
||||||
|
|
||||||
|
await sqLiteStorageHelper.ensureInitialization();
|
||||||
await sharedPreferencesHelper.ensureInitialization();
|
await sharedPreferencesHelper.ensureInitialization();
|
||||||
await secureStorageHelper.ensureInitilization();
|
await secureStorageHelper.ensureInitialization();
|
||||||
|
|
||||||
if (sharedPreferencesHelper.isFirstRun) {
|
if (sharedPreferencesHelper.isFirstRun) {
|
||||||
sharedPreferencesHelper.isFirstRun = false;
|
sharedPreferencesHelper.isFirstRun = false;
|
||||||
secureStorageHelper.purge();
|
secureStorageHelper.purge();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _initializeTracking() async {
|
||||||
await AppTrackingTransparency.requestTrackingAuthorization();
|
await AppTrackingTransparency.requestTrackingAuthorization();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _initializeFirebase() async {
|
||||||
await Firebase.initializeApp();
|
await Firebase.initializeApp();
|
||||||
await NotificationService.initialize();
|
}
|
||||||
|
|
||||||
|
Future<void> _initializeNotificationService() async {
|
||||||
|
await NotificationService.initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _initializeUrlStrategy() {
|
||||||
setUrlStrategy(const PathUrlStrategy());
|
setUrlStrategy(const PathUrlStrategy());
|
||||||
|
}
|
||||||
|
|
||||||
|
void _initializeSystemSettings() {
|
||||||
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
|
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
log("Aplicativo em Debug Mode, crashlytics desabilitado!");
|
log("Aplicativo em Debug Mode, crashlytics desabilitado!");
|
||||||
|
@ -54,14 +84,16 @@ Future<void> initializeApp() async {
|
||||||
FlutterError.onError = crashlyticsInstance.recordFlutterError;
|
FlutterError.onError = crashlyticsInstance.recordFlutterError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _initializeFlutterFlow() async {
|
||||||
await FlutterFlowTheme.initialize();
|
await FlutterFlowTheme.initialize();
|
||||||
await FFLocalizations.initialize();
|
await FFLocalizations.initialize();
|
||||||
GoRouter.optionURLReflectsImperativeAPIs = true;
|
GoRouter.optionURLReflectsImperativeAPIs = true;
|
||||||
usePathUrlStrategy();
|
usePathUrlStrategy();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> foregroundHandleMessage(RemoteMessage message) async {
|
Future<void> _foregroundHandleMessage(RemoteMessage message) async {
|
||||||
if (message.data['click_action'] == 'enroll_cond') {
|
if (message.data['click_action'] == 'enroll_cond') {
|
||||||
SecureStorageHelper().haveLocal = true;
|
SecureStorageHelper().haveLocal = true;
|
||||||
SecureStorageHelper().context?.go('/homePage');
|
SecureStorageHelper().context?.go('/homePage');
|
||||||
|
@ -81,15 +113,12 @@ Future<void> _backgroundHandleMessage(RemoteMessage message) async {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() async {
|
|
||||||
await initializeApp();
|
|
||||||
runApp(const App());
|
|
||||||
}
|
|
||||||
|
|
||||||
class App extends StatefulWidget {
|
class App extends StatefulWidget {
|
||||||
const App({super.key});
|
const App({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<App> createState() => _AppState();
|
State<App> createState() => _AppState();
|
||||||
|
|
||||||
static _AppState of(BuildContext context) =>
|
static _AppState of(BuildContext context) =>
|
||||||
context.findAncestorStateOfType<_AppState>()!;
|
context.findAncestorStateOfType<_AppState>()!;
|
||||||
}
|
}
|
||||||
|
@ -101,32 +130,86 @@ class _AppState extends State<App> {
|
||||||
late GoRouter _router;
|
late GoRouter _router;
|
||||||
bool displaySplashImage = true;
|
bool displaySplashImage = true;
|
||||||
|
|
||||||
|
final ThemeData _darkTheme = ThemeData(
|
||||||
|
brightness: Brightness.dark,
|
||||||
|
scrollbarTheme: ScrollbarThemeData(
|
||||||
|
thumbVisibility: WidgetStateProperty.all(false),
|
||||||
|
interactive: false,
|
||||||
|
thumbColor: WidgetStateProperty.resolveWith((states) {
|
||||||
|
if (states.contains(WidgetState.dragged)) {
|
||||||
|
return const Color(0xff1aab5f);
|
||||||
|
}
|
||||||
|
if (states.contains(WidgetState.hovered)) {
|
||||||
|
return const Color(0xff1aab5f);
|
||||||
|
}
|
||||||
|
return const Color(0xff1aab5f);
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final ThemeData _theme = ThemeData(
|
||||||
|
brightness: Brightness.light,
|
||||||
|
scrollbarTheme: ScrollbarThemeData(
|
||||||
|
thumbVisibility: WidgetStateProperty.all(false),
|
||||||
|
interactive: false,
|
||||||
|
thumbColor: WidgetStateProperty.resolveWith((states) {
|
||||||
|
if (states.contains(WidgetState.dragged)) {
|
||||||
|
return const Color(0xff1aab5f);
|
||||||
|
}
|
||||||
|
if (states.contains(WidgetState.hovered)) {
|
||||||
|
return const Color(0xff1aab5f);
|
||||||
|
}
|
||||||
|
return const Color(0xff1aab5f);
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates = const [
|
||||||
|
FFLocalizationsDelegate(),
|
||||||
|
GlobalMaterialLocalizations.delegate,
|
||||||
|
GlobalWidgetsLocalizations.delegate,
|
||||||
|
GlobalCupertinoLocalizations.delegate,
|
||||||
|
];
|
||||||
|
|
||||||
|
final Iterable<Locale> supportedLocales = const [
|
||||||
|
Locale('pt'),
|
||||||
|
Locale('en'),
|
||||||
|
];
|
||||||
|
|
||||||
|
Widget Function(BuildContext, Widget?)? builder = (context, widget) {
|
||||||
|
final breakpoints = [
|
||||||
|
const Breakpoint(start: 0, end: 450, name: MOBILE),
|
||||||
|
const Breakpoint(start: 451, end: 800, name: TABLET),
|
||||||
|
const Breakpoint(start: 801, end: 1920, name: DESKTOP),
|
||||||
|
const Breakpoint(start: 1921, end: double.infinity, name: '4K'),
|
||||||
|
];
|
||||||
|
return ResponsiveBreakpoints.builder(
|
||||||
|
child: BouncingScrollWrapper.builder(context, widget!),
|
||||||
|
breakpoints: breakpoints,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
|
FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
|
||||||
// WidgetsFlutterBinding.ensureInitialized().addPostFrameCallback((_) => initializeTracking());
|
|
||||||
|
|
||||||
_appStateNotifier = AppStateNotifier.instance;
|
_appStateNotifier = AppStateNotifier.instance;
|
||||||
_router = createRouter(_appStateNotifier);
|
_router = createRouter(_appStateNotifier);
|
||||||
Future.delayed(const Duration(milliseconds: 1000),
|
Future.delayed(const Duration(milliseconds: 1000),
|
||||||
() => setState(() => _appStateNotifier.stopShowingSplashImage()));
|
() => setState(() => _appStateNotifier.stopShowingSplashImage()));
|
||||||
|
FirebaseMessaging.onMessage.listen(_foregroundHandleMessage);
|
||||||
FirebaseMessaging.onMessage.listen(foregroundHandleMessage);
|
|
||||||
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
|
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
|
||||||
onMessageReceived(message.data, message.notification!.body,
|
onMessageReceived(message.data, message.notification!.body,
|
||||||
message.data['click_action']);
|
message.data['click_action']);
|
||||||
});
|
});
|
||||||
FirebaseMessaging.onBackgroundMessage(_backgroundHandleMessage);
|
FirebaseMessaging.onBackgroundMessage(_backgroundHandleMessage);
|
||||||
|
|
||||||
FirebaseMessaging.instance.getInitialMessage().then((message) {
|
FirebaseMessaging.instance.getInitialMessage().then((message) {
|
||||||
if (message != null) {
|
if (message != null) {
|
||||||
_backgroundHandleMessage(message);
|
_backgroundHandleMessage(message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// SecureStorageHelper().isLogged = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLocale(String language) {
|
void setLocale(String language) {
|
||||||
|
@ -145,58 +228,12 @@ class _AppState extends State<App> {
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp.router(
|
return MaterialApp.router(
|
||||||
title: 'FRE ACCESS HUB',
|
title: 'FRE ACCESS HUB',
|
||||||
builder: (context, widget) => ResponsiveBreakpoints.builder(
|
builder: builder,
|
||||||
child: BouncingScrollWrapper.builder(context, widget!),
|
localizationsDelegates: localizationsDelegates,
|
||||||
breakpoints: [
|
|
||||||
const Breakpoint(start: 0, end: 450, name: MOBILE),
|
|
||||||
const Breakpoint(start: 451, end: 800, name: TABLET),
|
|
||||||
const Breakpoint(start: 801, end: 1920, name: DESKTOP),
|
|
||||||
const Breakpoint(start: 1921, end: double.infinity, name: '4K'),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
localizationsDelegates: const [
|
|
||||||
FFLocalizationsDelegate(),
|
|
||||||
GlobalMaterialLocalizations.delegate,
|
|
||||||
GlobalWidgetsLocalizations.delegate,
|
|
||||||
GlobalCupertinoLocalizations.delegate,
|
|
||||||
],
|
|
||||||
locale: _locale,
|
locale: _locale,
|
||||||
supportedLocales: const [
|
supportedLocales: supportedLocales,
|
||||||
Locale('pt'),
|
theme: _theme,
|
||||||
Locale('en'),
|
darkTheme: _darkTheme,
|
||||||
],
|
|
||||||
theme: ThemeData(
|
|
||||||
brightness: Brightness.light,
|
|
||||||
scrollbarTheme: ScrollbarThemeData(
|
|
||||||
thumbVisibility: WidgetStateProperty.all(false),
|
|
||||||
interactive: false,
|
|
||||||
thumbColor: WidgetStateProperty.resolveWith((states) {
|
|
||||||
if (states.contains(WidgetState.dragged)) {
|
|
||||||
return const Color(0xff1aab5f);
|
|
||||||
}
|
|
||||||
if (states.contains(WidgetState.hovered)) {
|
|
||||||
return const Color(0xff1aab5f);
|
|
||||||
}
|
|
||||||
return const Color(0xff1aab5f);
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
darkTheme: ThemeData(
|
|
||||||
brightness: Brightness.dark,
|
|
||||||
scrollbarTheme: ScrollbarThemeData(
|
|
||||||
thumbVisibility: WidgetStateProperty.all(false),
|
|
||||||
interactive: false,
|
|
||||||
thumbColor: WidgetStateProperty.resolveWith((states) {
|
|
||||||
if (states.contains(WidgetState.dragged)) {
|
|
||||||
return const Color(0xff1aab5f);
|
|
||||||
}
|
|
||||||
if (states.contains(WidgetState.hovered)) {
|
|
||||||
return const Color(0xff1aab5f);
|
|
||||||
}
|
|
||||||
return const Color(0xff1aab5f);
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
themeMode: _themeMode,
|
themeMode: _themeMode,
|
||||||
routerConfig: _router,
|
routerConfig: _router,
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:developer';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
import 'package:hub/flutter_flow/flutter_flow_util.dart';
|
|
||||||
import 'package:hub/shared/utils/storage_util.dart';
|
import 'package:hub/shared/utils/storage_util.dart';
|
||||||
|
import 'package:hub/shared/utils/cache_util.dart';
|
||||||
import 'package:synchronized/synchronized.dart';
|
import 'package:synchronized/synchronized.dart';
|
||||||
|
|
||||||
class SecureStorageHelper extends ChangeNotifier implements Storage {
|
class SecureStorageHelper extends ChangeNotifier implements Storage {
|
||||||
|
@ -13,165 +15,185 @@ class SecureStorageHelper extends ChangeNotifier implements Storage {
|
||||||
|
|
||||||
SecureStorageHelper._internal();
|
SecureStorageHelper._internal();
|
||||||
|
|
||||||
String _deviceDescription = '';
|
static SecureStorageHelper get instance => _instance;
|
||||||
String get deviceDescription => _deviceDescription;
|
|
||||||
set deviceDescription(String value) {
|
String? _deviceDescription;
|
||||||
_setString('deviceDescription', value);
|
String get deviceDescription => _deviceDescription ?? '';
|
||||||
_deviceDescription = value;
|
set deviceDescription(String value) => _setAndCacheString('deviceDescription', value, (v) => _deviceDescription = v);
|
||||||
}
|
|
||||||
|
|
||||||
BuildContext? _context;
|
BuildContext? _context;
|
||||||
BuildContext? get context => _context;
|
BuildContext? get context => _context;
|
||||||
set context(BuildContext? value) {
|
set context(BuildContext? value) => _setAndCacheObject('ff_context', value.toString(), (v) => _context = value);
|
||||||
_setString('ff_context', value.toString());
|
|
||||||
_context = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool? _haveLocal;
|
bool? _haveLocal;
|
||||||
bool? get haveLocal => _haveLocal;
|
bool? get haveLocal => _haveLocal;
|
||||||
set haveLocal(bool? value) {
|
set haveLocal(bool? value) => _setAndCacheBool('ff_have_local', value ?? false, (v) => _haveLocal = value);
|
||||||
_setBool('ff_have_local', value);
|
|
||||||
_haveLocal = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
String _fingerprintPass = '';
|
String? _fingerprintPass;
|
||||||
String get fingerprintPass => _fingerprintPass;
|
String get fingerprintPass => _fingerprintPass ?? '';
|
||||||
set fingerprintPass(String value) {
|
set fingerprintPass(String value) => _setAndCacheString('fingerprintPass', value, (v) => _fingerprintPass = v);
|
||||||
_setString('fingerprintPass', value);
|
|
||||||
_fingerprintPass = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
String _accessPass = '';
|
String? _accessPass;
|
||||||
String get accessPass => _accessPass;
|
String get accessPass => _accessPass ?? '';
|
||||||
set accessPass(String value) {
|
set accessPass(String value) => _setAndCacheString('accessPass', value, (v) => _accessPass = v);
|
||||||
_setString('accessPass', value);
|
|
||||||
_accessPass = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
String _panicPass = '';
|
String? _panicPass;
|
||||||
String get panicPass => _panicPass;
|
String get panicPass => _panicPass ?? '';
|
||||||
set panicPass(String value) {
|
set panicPass(String value) => _setAndCacheString('panicPass', value, (v) => _panicPass = v);
|
||||||
_setString('panicPass', value);
|
|
||||||
_panicPass = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
String? _tokenAPNS = '';
|
String? _tokenAPNS;
|
||||||
String? get tokenAPNS => _tokenAPNS;
|
String? get tokenAPNS => _tokenAPNS;
|
||||||
set tokenAPNS(String? value) {
|
set tokenAPNS(String? value) => _setAndCacheString('ff_tokenAPNS', value ?? '', (v) => _tokenAPNS = v);
|
||||||
_setString('ff_tokenAPNS', value ?? '');
|
|
||||||
_tokenAPNS = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
String _email = '';
|
String? _email;
|
||||||
String get email => _email;
|
String get email => _email ?? '';
|
||||||
set email(String value) {
|
set email(String value) => _setAndCacheString('ff_email', value, (v) => _email = v);
|
||||||
_setString('ff_email', value);
|
|
||||||
_email = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
String _passwd = '';
|
String? _passwd;
|
||||||
String get passwd => _passwd;
|
String get passwd => _passwd ?? '';
|
||||||
set passwd(String value) {
|
set passwd(String value) => _setAndCacheString('ff_passwd', value, (v) => _passwd = v);
|
||||||
_setString('ff_passwd', value);
|
|
||||||
_passwd = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
String _deviceType = '';
|
String? _deviceType;
|
||||||
String get deviceType => _deviceType;
|
String get deviceType => _deviceType ?? '';
|
||||||
set deviceType(String value) {
|
set deviceType(String value) => _setAndCacheString('ff_deviceType', value, (v) => _deviceType = v);
|
||||||
_setString('ff_deviceType', value);
|
|
||||||
_deviceType = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool _isLogged = false;
|
bool? _isLogged;
|
||||||
bool get isLogged => _isLogged;
|
bool get isLogged => _isLogged ?? false;
|
||||||
set isLogged(bool value) {
|
set isLogged(bool value) => _setAndCacheBool('ff_isLogged', value, (v) => _isLogged = v);
|
||||||
_setBool('ff_isLogged', value);
|
|
||||||
_isLogged = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
String _token = '';
|
String? _token;
|
||||||
String get token => _token;
|
String get token => _token ?? '';
|
||||||
set token(String value) {
|
set token(String value) => _setAndCacheString('ff_token', value, (v) => _token = v);
|
||||||
_setString('ff_token', value);
|
|
||||||
_token = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> ensureInitilization() async {
|
Future<void> ensureInitialization() async {
|
||||||
await _safeInitAsync(() async {
|
log('SecureStorageHelper: Starting initialization');
|
||||||
_email = await _getString('ff_email');
|
|
||||||
_passwd = await _getString('ff_passwd');
|
|
||||||
_deviceType = await _getString('ff_deviceType');
|
|
||||||
_isLogged = await _getBool('ff_isLogged');
|
|
||||||
_tokenAPNS = await _getString('ff_tokenAPNS');
|
|
||||||
_accessPass = await _getString('accessPass');
|
|
||||||
_panicPass = await _getString('panicPass');
|
|
||||||
_fingerprintPass = await _getString('fingerprintPass');
|
|
||||||
_context = await _getObject('ff_context');
|
|
||||||
_haveLocal = await _getBool('ff_have_local');
|
|
||||||
_deviceDescription = await _getString('deviceDescription');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _safeInitAsync(Future<void> Function() initFunction) async {
|
|
||||||
await _lock.synchronized(() async {
|
await _lock.synchronized(() async {
|
||||||
try {
|
try {
|
||||||
await initFunction();
|
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) {
|
} catch (e) {
|
||||||
// Log or handle the error
|
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<String> _getString(String key) async {
|
Future<void> _setAndCacheBool(String key, bool value, Function(bool) cacheSetter) async {
|
||||||
return await _secureStorage.read(key: key) ?? '';
|
await _lock.synchronized(() async {
|
||||||
|
await _secureStorage.write(key: key, value: value.toString());
|
||||||
|
CacheUtil.instance.set(key, value);
|
||||||
|
cacheSetter(value);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> _getBool(String key) async {
|
Future<void> _setAndCacheObject(String key, String value, Function(String) cacheSetter) async {
|
||||||
return (await _secureStorage.read(key: key))?.toLowerCase() == 'true';
|
await _lock.synchronized(() async {
|
||||||
|
await _secureStorage.write(key: key, value: value);
|
||||||
|
CacheUtil.instance.set(key, value);
|
||||||
|
cacheSetter(value);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<dynamic> _getObject(String key) async {
|
Future<String?> _getString(String key) async {
|
||||||
// Implement your object retrieval logic here
|
return await _lock.synchronized(() async {
|
||||||
return null;
|
var value = CacheUtil.instance.get(key);
|
||||||
|
if (value == null) {
|
||||||
|
value = await _secureStorage.read(key: key);
|
||||||
|
CacheUtil.instance.set(key, value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void update(VoidCallback callback) {
|
Future<bool?> _getBool(String key) async {
|
||||||
callback();
|
return await _lock.synchronized(() async {
|
||||||
notifyListeners();
|
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<void> _setString(String key, String value) async {
|
Future<BuildContext?> _getObject(String key) async {
|
||||||
await _secureStorage.write(key: key, value: value);
|
return await _lock.synchronized(() async {
|
||||||
}
|
var value = CacheUtil.instance.get(key);
|
||||||
|
if (value == null) {
|
||||||
Future<void> _setBool(String key, bool? value) async {
|
value = await _secureStorage.read(key: key);
|
||||||
await _secureStorage.write(key: key, value: value.toString());
|
CacheUtil.instance.set(key, value);
|
||||||
}
|
}
|
||||||
|
return value as BuildContext?;
|
||||||
Future<void> purge() async {
|
});
|
||||||
_secureStorage.deleteAll();
|
|
||||||
await ensureInitilization();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> set(String key, dynamic value) async {
|
Future<void> set(String key, dynamic value) async {
|
||||||
if (value is String) {
|
if (value is String) {
|
||||||
await _setString(key, value);
|
await _setAndCacheString(key, value, (v) {});
|
||||||
} else if (value is bool) {
|
} else if (value is bool) {
|
||||||
await _setBool(key, value);
|
await _setAndCacheBool(key, value, (v) {});
|
||||||
} else if (value is int || value is double) {
|
} else if (value is BuildContext) {
|
||||||
await _setString(key, value.toString());
|
await _setAndCacheObject(key, value.toString(), (v) {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<dynamic> get(String key) async {
|
Future<dynamic> get(String key) async {
|
||||||
return await _secureStorage.read(key: key);
|
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
|
@override
|
||||||
Future<void> delete(String key) async {
|
Future<void> delete(String key) async {
|
||||||
await _secureStorage.delete(key: key);
|
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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,66 +1,103 @@
|
||||||
import 'package:hub/shared/utils/storage_util.dart';
|
import 'package:hub/shared/utils/storage_util.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
import 'package:hub/shared/utils/cache_util.dart';
|
||||||
|
import 'package:synchronized/synchronized.dart';
|
||||||
|
|
||||||
class SharedPreferencesStorageHelper implements Storage {
|
class SharedPreferencesStorageHelper implements Storage {
|
||||||
late SharedPreferences _prefs;
|
static final SharedPreferencesStorageHelper _instance = SharedPreferencesStorageHelper._internal();
|
||||||
|
static final _lock = Lock();
|
||||||
|
SharedPreferences? _prefs;
|
||||||
|
|
||||||
static final SharedPreferencesStorageHelper _instance =
|
factory SharedPreferencesStorageHelper() => _instance;
|
||||||
SharedPreferencesStorageHelper._internal();
|
|
||||||
|
|
||||||
factory SharedPreferencesStorageHelper() {
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
SharedPreferencesStorageHelper._internal();
|
SharedPreferencesStorageHelper._internal();
|
||||||
|
|
||||||
Future<void> ensureInitialization() async {
|
static SharedPreferencesStorageHelper get instance => _instance;
|
||||||
_prefs = await SharedPreferences.getInstance();
|
|
||||||
_isFirstRun = _prefs.getBool('first_run') ?? true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool _isFirstRun = true;
|
bool _isFirstRun = true;
|
||||||
bool get isFirstRun => _isFirstRun;
|
bool get isFirstRun => _isFirstRun;
|
||||||
set isFirstRun(bool value) {
|
set isFirstRun(bool value) => _setAndCacheBool('first_run', value, (v) => _isFirstRun = v);
|
||||||
_isFirstRun = value;
|
|
||||||
set('first_run', value);
|
Future<void> ensureInitialization() async {
|
||||||
|
await _lock.synchronized(() async {
|
||||||
|
if (_prefs == null) {
|
||||||
|
_prefs = await SharedPreferences.getInstance();
|
||||||
|
_isFirstRun = _prefs?.getBool('first_run') ?? true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _setAndCache<T>(String key, T value, Function(T) cacheSetter, Future<void> Function(String, T) setFunc) async {
|
||||||
|
await _lock.synchronized(() async {
|
||||||
|
await setFunc(key, value);
|
||||||
|
CacheUtil.instance.set(key, value);
|
||||||
|
cacheSetter(value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _setAndCacheString(String key, String value, Function(String) cacheSetter) async {
|
||||||
|
await _setAndCache(key, value, cacheSetter, _prefs!.setString);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _setAndCacheBool(String key, bool value, Function(bool) cacheSetter) async {
|
||||||
|
await _setAndCache(key, value, cacheSetter, _prefs!.setBool);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _setAndCacheInt(String key, int value, Function(int) cacheSetter) async {
|
||||||
|
await _setAndCache(key, value, cacheSetter, _prefs!.setInt);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _setAndCacheDouble(String key, double value, Function(double) cacheSetter) async {
|
||||||
|
await _setAndCache(key, value, cacheSetter, _prefs!.setDouble);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _setAndCacheStringList(String key, List<String> value, Function(List<String>) cacheSetter) async {
|
||||||
|
await _setAndCache(key, value, cacheSetter, _prefs!.setStringList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> set(String key, dynamic value) async {
|
Future<void> set(String key, dynamic value) async {
|
||||||
if (value is bool) {
|
await _lock.synchronized(() async {
|
||||||
await _prefs.setBool(key, value);
|
if (value is bool) {
|
||||||
} else if (value is String) {
|
await _prefs?.setBool(key, value);
|
||||||
await _prefs.setString(key, value);
|
} else if (value is String) {
|
||||||
} else if (value is int) {
|
await _prefs?.setString(key, value);
|
||||||
await _prefs.setInt(key, value);
|
} else if (value is int) {
|
||||||
} else if (value is double) {
|
await _prefs?.setInt(key, value);
|
||||||
await _prefs.setDouble(key, value);
|
} else if (value is double) {
|
||||||
} else if (value is List<String>) {
|
await _prefs?.setDouble(key, value);
|
||||||
await _prefs.setStringList(key, value);
|
} else if (value is List<String>) {
|
||||||
}
|
await _prefs?.setStringList(key, value);
|
||||||
|
}
|
||||||
|
CacheUtil.instance.set(key, value);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<dynamic> get(String key) async {
|
Future<dynamic> get(String key) async {
|
||||||
try {
|
return await _lock.synchronized(() async {
|
||||||
return _prefs.get(key);
|
var value = CacheUtil.instance.get(key);
|
||||||
} catch (e) {
|
if (value == null) {
|
||||||
// Log or handle the error
|
value = _prefs?.get(key);
|
||||||
return null;
|
CacheUtil.instance.set(key, value);
|
||||||
}
|
}
|
||||||
|
return value;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> delete(String key) async {
|
Future<void> delete(String key) async {
|
||||||
try {
|
await _lock.synchronized(() async {
|
||||||
await _prefs.remove(key);
|
await _prefs?.remove(key);
|
||||||
} catch (e) {
|
CacheUtil.instance.delete(key);
|
||||||
// Log or handle the error
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> purge() async {
|
Future<void> purge() async {
|
||||||
await _prefs.clear();
|
await _lock.synchronized(() async {
|
||||||
await ensureInitialization();
|
await _prefs?.clear();
|
||||||
|
CacheUtil.instance.clear();
|
||||||
|
await ensureInitialization();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,13 +1,7 @@
|
||||||
import 'package:hub/shared/utils/cache_util.dart';
|
import 'package:hub/shared/utils/cache_util.dart';
|
||||||
import 'package:sqflite/sqflite.dart';
|
import 'package:sqflite/sqflite.dart';
|
||||||
import 'dart:developer';
|
import 'dart:developer';
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:sqflite/sqflite.dart';
|
|
||||||
import 'dart:developer';
|
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
import 'package:path/path.dart';
|
|
||||||
import 'package:sqflite/sqflite.dart';
|
|
||||||
|
|
||||||
class DatabaseConfig {
|
class DatabaseConfig {
|
||||||
static const String dbName = 'database.db';
|
static const String dbName = 'database.db';
|
||||||
|
@ -53,13 +47,19 @@ class SQLiteStorageHelper {
|
||||||
|
|
||||||
SQLiteStorageHelper._internal();
|
SQLiteStorageHelper._internal();
|
||||||
|
|
||||||
Future<Database> get database async {
|
static SQLiteStorageHelper get instance => _instance;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Future<Database> ensureInitialization() async {
|
||||||
|
log('Getting database instance');
|
||||||
if (_database != null) return _database!;
|
if (_database != null) return _database!;
|
||||||
_database = await _initDatabase();
|
_database = await _initDatabase();
|
||||||
return _database!;
|
return _database!;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String> _getDatabasePath() async {
|
Future<String> _getDatabasePath() async {
|
||||||
|
log('Getting database path');
|
||||||
if (_databasePath != null) return _databasePath!;
|
if (_databasePath != null) return _databasePath!;
|
||||||
final databasesPath = await getDatabasesPath();
|
final databasesPath = await getDatabasesPath();
|
||||||
_databasePath = join(databasesPath, DatabaseConfig.dbName);
|
_databasePath = join(databasesPath, DatabaseConfig.dbName);
|
||||||
|
@ -68,6 +68,7 @@ class SQLiteStorageHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Database> _initDatabase() async {
|
Future<Database> _initDatabase() async {
|
||||||
|
log('Initializing database');
|
||||||
final path = await _getDatabasePath();
|
final path = await _getDatabasePath();
|
||||||
return await openDatabase(
|
return await openDatabase(
|
||||||
path,
|
path,
|
||||||
|
@ -80,7 +81,10 @@ class SQLiteStorageHelper {
|
||||||
).catchError((error) {
|
).catchError((error) {
|
||||||
log('Error initializing database: $error');
|
log('Error initializing database: $error');
|
||||||
throw error;
|
throw error;
|
||||||
}).whenComplete(() async => await setupLocalVariables());
|
}).whenComplete(() async {
|
||||||
|
log('Database initialization complete');
|
||||||
|
await setupLocalVariables();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onCreate(Database db, int version) async {
|
Future<void> _onCreate(Database db, int version) async {
|
||||||
|
@ -100,15 +104,17 @@ class SQLiteStorageHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _insertInitialData(Database db) async {
|
Future<void> _insertInitialData(Database db) async {
|
||||||
|
log('Inserting initial data');
|
||||||
final batch = db.batch();
|
final batch = db.batch();
|
||||||
for (var data in DatabaseConfig.initialData) {
|
for (var data in DatabaseConfig.initialData) {
|
||||||
batch.insert(DatabaseConfig.tableKeychain, data);
|
batch.insert(DatabaseConfig.tableKeychain, data);
|
||||||
}
|
}
|
||||||
await batch.commit(noResult: true);
|
await batch.commit(noResult: true);
|
||||||
|
log('Initial data inserted');
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onOpen(Database db) async {
|
Future<void> _onOpen(Database db) async {
|
||||||
log('Checking existing data...');
|
log('Opening database');
|
||||||
await _checkExistingData(db);
|
await _checkExistingData(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,10 +127,11 @@ class SQLiteStorageHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onConfigure(Database db) async {
|
Future<void> _onConfigure(Database db) async {
|
||||||
log('Configuring database...');
|
log('Configuring database');
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _checkExistingData(Database db) async {
|
Future<void> _checkExistingData(Database db) async {
|
||||||
|
log('Checking existing data');
|
||||||
try {
|
try {
|
||||||
final maps = await db.query(DatabaseConfig.tableKeychain);
|
final maps = await db.query(DatabaseConfig.tableKeychain);
|
||||||
log('Existing data: $maps');
|
log('Existing data: $maps');
|
||||||
|
@ -153,107 +160,259 @@ class SQLiteStorageHelper {
|
||||||
bool _person = false;
|
bool _person = false;
|
||||||
bool _requestOSnotification = false;
|
bool _requestOSnotification = false;
|
||||||
|
|
||||||
String get createdAt => '0000-00-00 00:00:00';
|
String get createdAt {
|
||||||
String get devUUID => _devUUID;
|
log('Getting createdAt');
|
||||||
String get userUUID => _userUUID;
|
return '0000-00-00 00:00:00';
|
||||||
String get userDevUUID => _userDevUUID;
|
}
|
||||||
String get status => _status;
|
|
||||||
String get userName => _userName;
|
|
||||||
String get cliUUID => _cliUUID;
|
|
||||||
String get ownerUUID => _ownerUUID;
|
|
||||||
String get cliName => _cliName;
|
|
||||||
String get petAmountRegister => _petAmountRegister;
|
|
||||||
bool get whatsapp => _whatsapp;
|
|
||||||
bool get provisional => _provisional;
|
|
||||||
bool get pets => _pets;
|
|
||||||
bool get local => _local;
|
|
||||||
bool get notify => _notify;
|
|
||||||
bool get fingerprint => _fingerprint;
|
|
||||||
bool get access => _access;
|
|
||||||
bool get panic => _panic;
|
|
||||||
bool get person => _person;
|
|
||||||
bool get requestOSnotification => _requestOSnotification;
|
|
||||||
|
|
||||||
set devUUID(String value) => set('devUUID', value);
|
String get devUUID {
|
||||||
set userUUID(String value) => set('userUUID', value);
|
log('Getting devUUID');
|
||||||
set userDevUUID(String value) => set('userDevUUID', value);
|
return _devUUID;
|
||||||
set status(String value) => set('status', value);
|
}
|
||||||
set userName(String value) => set('userName', value);
|
|
||||||
set cliUUID(String value) => set('cliUUID', value);
|
String get userUUID {
|
||||||
set ownerUUID(String value) => set('ownerUUID', value);
|
log('Getting userUUID');
|
||||||
set cliName(String value) => set('cliName', value);
|
return _userUUID;
|
||||||
set petAmountRegister(String value) => set('petAmountRegister', value);
|
}
|
||||||
set whatsapp(bool value) => set('whatsapp', value);
|
|
||||||
set provisional(bool value) => set('provisional', value);
|
String get userDevUUID {
|
||||||
set pets(bool value) => set('pets', value);
|
log('Getting userDevUUID');
|
||||||
set local(bool value) => set('local', value);
|
return _userDevUUID;
|
||||||
set notify(bool value) => set('notify', value);
|
}
|
||||||
set fingerprint(bool value) => set('fingerprint', value);
|
|
||||||
set access(bool value) => set('access', value);
|
String get status {
|
||||||
set panic(bool value) => set('panic', value);
|
log('Getting status');
|
||||||
set person(bool value) => set('person', value);
|
return _status;
|
||||||
set requestOSnotification(bool value) => set('requestOSnotification', value);
|
}
|
||||||
|
|
||||||
|
String get userName {
|
||||||
|
log('Getting userName');
|
||||||
|
return _userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
String get cliUUID {
|
||||||
|
log('Getting cliUUID');
|
||||||
|
return _cliUUID;
|
||||||
|
}
|
||||||
|
|
||||||
|
String get ownerUUID {
|
||||||
|
log('Getting ownerUUID');
|
||||||
|
return _ownerUUID;
|
||||||
|
}
|
||||||
|
|
||||||
|
String get cliName {
|
||||||
|
log('Getting cliName');
|
||||||
|
return _cliName;
|
||||||
|
}
|
||||||
|
|
||||||
|
String get petAmountRegister {
|
||||||
|
log('Getting petAmountRegister');
|
||||||
|
return _petAmountRegister;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get whatsapp {
|
||||||
|
log('Getting whatsapp');
|
||||||
|
return _whatsapp;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get provisional {
|
||||||
|
log('Getting provisional');
|
||||||
|
return _provisional;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get pets {
|
||||||
|
log('Getting pets');
|
||||||
|
return _pets;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get local {
|
||||||
|
log('Getting local');
|
||||||
|
return _local;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get notify {
|
||||||
|
log('Getting notify');
|
||||||
|
return _notify;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get fingerprint {
|
||||||
|
log('Getting fingerprint');
|
||||||
|
return _fingerprint;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get access {
|
||||||
|
log('Getting access');
|
||||||
|
return _access;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get panic {
|
||||||
|
log('Getting panic');
|
||||||
|
return _panic;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get person {
|
||||||
|
log('Getting person');
|
||||||
|
return _person;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get requestOSnotification {
|
||||||
|
log('Getting requestOSnotification');
|
||||||
|
return _requestOSnotification;
|
||||||
|
}
|
||||||
|
|
||||||
|
set devUUID(String value) {
|
||||||
|
log('Setting devUUID to $value');
|
||||||
|
set('devUUID', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set userUUID(String value) {
|
||||||
|
log('Setting userUUID to $value');
|
||||||
|
set('userUUID', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set userDevUUID(String value) {
|
||||||
|
log('Setting userDevUUID to $value');
|
||||||
|
set('userDevUUID', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set status(String value) {
|
||||||
|
log('Setting status to $value');
|
||||||
|
set('status', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set userName(String value) {
|
||||||
|
log('Setting userName to $value');
|
||||||
|
set('userName', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set cliUUID(String value) {
|
||||||
|
log('Setting cliUUID to $value');
|
||||||
|
set('cliUUID', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set ownerUUID(String value) {
|
||||||
|
log('Setting ownerUUID to $value');
|
||||||
|
set('ownerUUID', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set cliName(String value) {
|
||||||
|
log('Setting cliName to $value');
|
||||||
|
set('cliName', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set petAmountRegister(String value) {
|
||||||
|
log('Setting petAmountRegister to $value');
|
||||||
|
set('petAmountRegister', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set whatsapp(bool value) {
|
||||||
|
log('Setting whatsapp to $value');
|
||||||
|
set('whatsapp', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set provisional(bool value) {
|
||||||
|
log('Setting provisional to $value');
|
||||||
|
set('provisional', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set pets(bool value) {
|
||||||
|
log('Setting pets to $value');
|
||||||
|
set('pets', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set local(bool value) {
|
||||||
|
log('Setting local to $value');
|
||||||
|
set('local', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set notify(bool value) {
|
||||||
|
log('Setting notify to $value');
|
||||||
|
set('notify', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set fingerprint(bool value) {
|
||||||
|
log('Setting fingerprint to $value');
|
||||||
|
set('fingerprint', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set access(bool value) {
|
||||||
|
log('Setting access to $value');
|
||||||
|
set('access', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set panic(bool value) {
|
||||||
|
log('Setting panic to $value');
|
||||||
|
set('panic', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set person(bool value) {
|
||||||
|
log('Setting person to $value');
|
||||||
|
set('person', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
set requestOSnotification(bool value) {
|
||||||
|
log('Setting requestOSnotification to $value');
|
||||||
|
set('requestOSnotification', value);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> setupLocalVariables() async {
|
Future<void> setupLocalVariables() async {
|
||||||
log('Setting up local variables...');
|
log('Setting up local variables');
|
||||||
await _database?.transaction((txn) async {
|
try {
|
||||||
final keys = [
|
await _database?.transaction((txn) async {
|
||||||
'devUUID',
|
final keys = [
|
||||||
'userUUID',
|
'devUUID',
|
||||||
'userDevUUID',
|
'userUUID',
|
||||||
'status',
|
'userDevUUID',
|
||||||
'userName',
|
'status',
|
||||||
'cliUUID',
|
'userName',
|
||||||
'ownerUUID',
|
'cliUUID',
|
||||||
'cliName',
|
'ownerUUID',
|
||||||
'whatsapp',
|
'cliName',
|
||||||
'provisional',
|
'petAmountRegister',
|
||||||
'pets',
|
'whatsapp',
|
||||||
'local',
|
'provisional',
|
||||||
'notify',
|
'pets',
|
||||||
'fingerprint',
|
'local',
|
||||||
'access',
|
'notify',
|
||||||
'panic',
|
'fingerprint',
|
||||||
'person',
|
'access',
|
||||||
'requestOSnotification'
|
'panic',
|
||||||
];
|
'person',
|
||||||
|
'requestOSnotification'
|
||||||
|
];
|
||||||
|
|
||||||
final results = await Future.wait(keys.map((key) => get(key)));
|
for (var key in keys) {
|
||||||
|
log('Fetching value for key: $key');
|
||||||
_devUUID = results[0]?.toString() ?? '';
|
final result = await txn.query(
|
||||||
_userUUID = results[1]?.toString() ?? '';
|
DatabaseConfig.tableKeychain,
|
||||||
_userDevUUID = results[2]?.toString() ?? '';
|
where: '${DatabaseConfig.columnKey} = ?',
|
||||||
_status = results[3]?.toString() ?? '';
|
whereArgs: [key],
|
||||||
_userName = results[4]?.toString() ?? '';
|
);
|
||||||
_cliUUID = results[5]?.toString() ?? '';
|
log('Result for key $key: $result');
|
||||||
_ownerUUID = results[6]?.toString() ?? '';
|
}
|
||||||
_cliName = results[7]?.toString() ?? '';
|
});
|
||||||
_whatsapp = results[8] == 'true';
|
log('Local variables setup complete');
|
||||||
_provisional = results[9] == 'true';
|
} catch (error) {
|
||||||
_pets = results[10] == 'true';
|
log('Error setting up local variables: $error');
|
||||||
_local = results[11] == 'true';
|
}
|
||||||
_notify = results[12] == 'true';
|
|
||||||
_fingerprint = results[13] == 'true';
|
|
||||||
_access = results[14] == 'true';
|
|
||||||
_panic = results[15] == 'true';
|
|
||||||
_person = results[16] == 'true';
|
|
||||||
_requestOSnotification = results[17] == 'true';
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> getBoolean(String key) async {
|
Future<bool> getBoolean(String key) async {
|
||||||
|
log('Getting boolean value for key: $key');
|
||||||
final value = await get(key);
|
final value = await get(key);
|
||||||
return value == 'true';
|
return value == 'true';
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<dynamic> get(String key) async {
|
Future<dynamic> get(String key) async {
|
||||||
final cachedValue = CacheUtil.get(key);
|
log('Getting value for key: $key');
|
||||||
|
final cachedValue = CacheUtil().get(key);
|
||||||
if (cachedValue != null) {
|
if (cachedValue != null) {
|
||||||
|
log('Found cached value for key: $key');
|
||||||
return cachedValue;
|
return cachedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final db = await database;
|
final db = await ensureInitialization();
|
||||||
final result = await db.query(
|
final result = await db.query(
|
||||||
DatabaseConfig.tableKeychain,
|
DatabaseConfig.tableKeychain,
|
||||||
columns: [DatabaseConfig.columnValue],
|
columns: [DatabaseConfig.columnValue],
|
||||||
|
@ -263,9 +422,11 @@ class SQLiteStorageHelper {
|
||||||
|
|
||||||
if (result.isNotEmpty) {
|
if (result.isNotEmpty) {
|
||||||
final value = result.first[DatabaseConfig.columnValue];
|
final value = result.first[DatabaseConfig.columnValue];
|
||||||
CacheUtil.set(key, value);
|
CacheUtil().set(key, value);
|
||||||
|
log('Value for key $key: $value');
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
log('No value found for key: $key');
|
||||||
return null;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log('Error getting value for key $key: $error');
|
log('Error getting value for key $key: $error');
|
||||||
|
@ -274,8 +435,9 @@ class SQLiteStorageHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<int> set(String key, dynamic value) async {
|
Future<int> set(String key, dynamic value) async {
|
||||||
CacheUtil.set(key, value);
|
log('Setting value for key: $key to $value');
|
||||||
final db = await database;
|
CacheUtil().set(key, value);
|
||||||
|
final db = await ensureInitialization();
|
||||||
final data = {
|
final data = {
|
||||||
DatabaseConfig.columnKey: key,
|
DatabaseConfig.columnKey: key,
|
||||||
DatabaseConfig.columnValue: value.toString(),
|
DatabaseConfig.columnValue: value.toString(),
|
||||||
|
@ -283,34 +445,53 @@ class SQLiteStorageHelper {
|
||||||
DatabaseConfig.columnCreatedAt: DateTime.now().toIso8601String(),
|
DatabaseConfig.columnCreatedAt: DateTime.now().toIso8601String(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return await db.insert(
|
final result = await db.insert(
|
||||||
DatabaseConfig.tableKeychain,
|
DatabaseConfig.tableKeychain,
|
||||||
data,
|
data,
|
||||||
conflictAlgorithm: ConflictAlgorithm.replace,
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
||||||
);
|
);
|
||||||
|
log('Value set for key: $key');
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<int> delete(String key) async {
|
Future<int> delete(String key) async {
|
||||||
final db = await database;
|
log('Deleting value for key: $key');
|
||||||
return await db.transaction((txn) async {
|
final db = await ensureInitialization();
|
||||||
|
final result = await db.transaction((txn) async {
|
||||||
return await txn.delete(
|
return await txn.delete(
|
||||||
DatabaseConfig.tableKeychain,
|
DatabaseConfig.tableKeychain,
|
||||||
where: '${DatabaseConfig.columnKey} = ?',
|
where: '${DatabaseConfig.columnKey} = ?',
|
||||||
whereArgs: [key],
|
whereArgs: [key],
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
log('Value deleted for key: $key');
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> purge() async {
|
Future<void> purge() async {
|
||||||
|
log('Purging database');
|
||||||
await deleteDatabaseDB();
|
await deleteDatabaseDB();
|
||||||
await database;
|
await ensureInitialization();
|
||||||
log('Database purged');
|
log('Database purged');
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> deleteDatabaseDB() async {
|
Future<void> deleteDatabaseDB() async {
|
||||||
|
log('Deleting database');
|
||||||
final path = await _getDatabasePath();
|
final path = await _getDatabasePath();
|
||||||
await deleteDatabase(path);
|
await deleteDatabase(path);
|
||||||
log('Database deleted');
|
log('Database deleted');
|
||||||
_database = null;
|
_database = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,34 @@
|
||||||
class CacheUtil {
|
class CacheUtil {
|
||||||
|
static final CacheUtil _instance = CacheUtil._internal();
|
||||||
static final Map<String, dynamic> _cache = {};
|
static final Map<String, dynamic> _cache = {};
|
||||||
|
|
||||||
static void set(String key, dynamic value) {
|
CacheUtil._internal();
|
||||||
|
|
||||||
|
factory CacheUtil() => _instance;
|
||||||
|
|
||||||
|
static CacheUtil get instance => _instance;
|
||||||
|
|
||||||
|
void set(String key, dynamic value) {
|
||||||
_cache[key] = value;
|
_cache[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static dynamic get(String key) {
|
dynamic get(String key) {
|
||||||
return _cache[key];
|
return _cache[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void delete(String key) {
|
bool containsKey(String key) {
|
||||||
|
return _cache.containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic getOrElse(String key, dynamic fallback) {
|
||||||
|
return _cache.containsKey(key) ? _cache[key] : fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete(String key) {
|
||||||
_cache.remove(key);
|
_cache.remove(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void clear() {
|
void clear() {
|
||||||
_cache.clear();
|
_cache.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:developer';
|
||||||
|
|
||||||
import 'package:hub/shared/helpers/secure_storage_helper.dart';
|
import 'package:hub/shared/helpers/secure_storage_helper.dart';
|
||||||
import 'package:hub/shared/helpers/shared_preferences_storage_helper.dart';
|
import 'package:hub/shared/helpers/shared_preferences_storage_helper.dart';
|
||||||
import 'package:hub/shared/helpers/sqlite_storage_helper.dart';
|
import 'package:hub/shared/helpers/sqlite_storage_helper.dart';
|
||||||
|
@ -21,65 +23,80 @@ enum StorageType { SecureStorage, SharedPreferences, SQLite3 }
|
||||||
|
|
||||||
class StorageManager {
|
class StorageManager {
|
||||||
static final SecureStorageHelper _secureStorage = SecureStorageHelper();
|
static final SecureStorageHelper _secureStorage = SecureStorageHelper();
|
||||||
static final SharedPreferencesStorageHelper _sharedPreferences =
|
static final SharedPreferencesStorageHelper _sharedPreferences = SharedPreferencesStorageHelper();
|
||||||
SharedPreferencesStorageHelper();
|
|
||||||
static final SQLiteStorageHelper _sqliteStorage = SQLiteStorageHelper();
|
static final SQLiteStorageHelper _sqliteStorage = SQLiteStorageHelper();
|
||||||
|
|
||||||
static Future<void> set(StorageData data) async {
|
static Future<void> set(StorageData data) async {
|
||||||
switch (data.type) {
|
try {
|
||||||
case StorageType.SecureStorage:
|
switch (data.type) {
|
||||||
CacheUtil.set(data.key, data.value);
|
case StorageType.SecureStorage:
|
||||||
await _secureStorage.set(data.key, data.value);
|
await _secureStorage.set(data.key, data.value);
|
||||||
break;
|
break;
|
||||||
case StorageType.SharedPreferences:
|
case StorageType.SharedPreferences:
|
||||||
await _sharedPreferences.set(data.key, data.value);
|
await _sharedPreferences.set(data.key, data.value);
|
||||||
break;
|
break;
|
||||||
case StorageType.SQLite3:
|
case StorageType.SQLite3:
|
||||||
await _sqliteStorage.set(data.key, data.value);
|
await _sqliteStorage.set(data.key, data.value);
|
||||||
break;
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log('Error setting data: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<dynamic> get(StorageData data) async {
|
static Future<dynamic> get(StorageData data) async {
|
||||||
var value = CacheUtil.get(data.key);
|
try {
|
||||||
if (value == null) {
|
var value = CacheUtil().get(data.key);
|
||||||
switch (data.type) {
|
if (value == null) {
|
||||||
case StorageType.SecureStorage:
|
switch (data.type) {
|
||||||
value = await _secureStorage.get(data.key);
|
case StorageType.SecureStorage:
|
||||||
break;
|
value = await _secureStorage.get(data.key);
|
||||||
case StorageType.SharedPreferences:
|
break;
|
||||||
value = await _sharedPreferences.get(data.key);
|
case StorageType.SharedPreferences:
|
||||||
break;
|
value = await _sharedPreferences.get(data.key);
|
||||||
case StorageType.SQLite3:
|
break;
|
||||||
value = await _sqliteStorage.get(data.key);
|
case StorageType.SQLite3:
|
||||||
break;
|
value = await _sqliteStorage.get(data.key);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
CacheUtil().set(data.key, value);
|
||||||
}
|
}
|
||||||
CacheUtil.set(data.key, value);
|
return value;
|
||||||
|
} catch (e) {
|
||||||
|
log('Error getting data: $e');
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<void> delete(StorageData data) async {
|
static Future<void> delete(StorageData data) async {
|
||||||
switch (data.type) {
|
try {
|
||||||
case StorageType.SecureStorage:
|
switch (data.type) {
|
||||||
CacheUtil.delete(data.key);
|
case StorageType.SecureStorage:
|
||||||
await _secureStorage.delete(data.key);
|
CacheUtil().delete(data.key);
|
||||||
break;
|
await _secureStorage.delete(data.key);
|
||||||
case StorageType.SharedPreferences:
|
break;
|
||||||
await _sharedPreferences.delete(data.key);
|
case StorageType.SharedPreferences:
|
||||||
break;
|
await _sharedPreferences.delete(data.key);
|
||||||
case StorageType.SQLite3:
|
break;
|
||||||
await _sqliteStorage.delete(data.key);
|
case StorageType.SQLite3:
|
||||||
break;
|
await _sqliteStorage.delete(data.key);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log('Error deleting data: $e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<void> purge() async {
|
static Future<void> purge() async {
|
||||||
// CacheUtil.purge();
|
try {
|
||||||
await _secureStorage.purge();
|
await _secureStorage.purge();
|
||||||
await _sharedPreferences.purge();
|
await _sharedPreferences.purge();
|
||||||
await _sqliteStorage.purge();
|
await _sqliteStorage.purge();
|
||||||
|
} catch (e) {
|
||||||
|
log('Error purging data: $e');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class StorageUtil {}
|
class StorageUtil {}
|
Loading…
Reference in New Issue