import 'package:get_it/get_it.dart'; import 'dart:convert'; import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:hive_flutter/hive_flutter.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:provider/provider.dart'; import 'package:selfprivacy/logic/appsettingscubit.dart'; import 'package:selfprivacy/logic/courierscubit.dart'; import 'package:selfprivacy/logic/itemscubit.dart'; import 'package:selfprivacy/logic/orderscubit.dart'; final GetIt getIt = GetIt.instance; Future getItSetup() async { getIt.registerSingleton(NavigationService()); getIt.registerSingleton(TimerModel()); getIt.registerSingleton(ApiConfigModel()..init()); await getIt.allReady(); } class ApiConfigModel { final Box _box = Hive.box(BNames.serverInstallationBox); String? get serverKey => _serverKey; String? _serverKey; Future storeServerProviderKey(final String value) async { await _box.put(BNames.serverKey, value); _serverKey = value; } void clear() { _serverKey = null; } void init() { _serverKey = _box.get(BNames.serverKey); } } class HiveConfig { static Future init() async { await Hive.initFlutter(); await Hive.openBox(BNames.appSettingsBox); final HiveAesCipher cipher = HiveAesCipher( await getEncryptedKey(BNames.serverInstallationEncryptionKey), ); await Hive.openBox(BNames.serverInstallationBox, encryptionCipher: cipher); } static Future getEncryptedKey(final String encKey) async { const FlutterSecureStorage secureStorage = FlutterSecureStorage(); final bool hasEncryptionKey = await secureStorage.containsKey(key: encKey); if (!hasEncryptionKey) { final List key = Hive.generateSecureKey(); await secureStorage.write(key: encKey, value: base64UrlEncode(key)); } final String? string = await secureStorage.read(key: encKey); return base64Url.decode(string!); } } class BNames { static String appSettingsBox = 'appSettingsBox'; static String serverInstallationEncryptionKey = 'key'; static String serverInstallationBox = 'appConfig'; static String rootKeys = 'rootKeys'; static String serverKey = 'serverKey'; } class NavigationService { final GlobalKey scaffoldMessengerKey = GlobalKey(); final GlobalKey navigatorKey = GlobalKey(); NavigatorState? get navigator => navigatorKey.currentState; void showPopUpDialog(final AlertDialog dialog) { final BuildContext context = navigatorKey.currentState!.overlay!.context; showDialog( context: context, builder: (final _) => dialog, ); } } class TimerModel extends ChangeNotifier { DateTime _time = DateTime.now(); DateTime get time => _time; void restart() { _time = DateTime.now(); notifyListeners(); } } class BlocAndProviderConfig extends StatelessWidget { const BlocAndProviderConfig({super.key, this.child}); final Widget? child; @override Widget build(final BuildContext context) => MultiProvider( providers: [ BlocProvider( create: (final _) => AppSettingsCubit(key: '')..load(), ), BlocProvider( create: (final _) => ItemsCubit()..load(), ), BlocProvider( create: (final _) => CouriersCubit()..load(), ), BlocProvider( create: (final _) => OrdersCubit()..load(), ), ], child: child, ); }