course-work/lib/logic/config.dart

131 lines
3.6 KiB
Dart
Raw Permalink Normal View History

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';
2022-12-27 05:45:05 +00:00
import 'package:selfprivacy/logic/courierscubit.dart';
import 'package:selfprivacy/logic/itemscubit.dart';
import 'package:selfprivacy/logic/orderscubit.dart';
final GetIt getIt = GetIt.instance;
Future<void> getItSetup() async {
getIt.registerSingleton<NavigationService>(NavigationService());
getIt.registerSingleton<TimerModel>(TimerModel());
getIt.registerSingleton<ApiConfigModel>(ApiConfigModel()..init());
await getIt.allReady();
}
class ApiConfigModel {
final Box _box = Hive.box(BNames.serverInstallationBox);
String? get serverKey => _serverKey;
String? _serverKey;
Future<void> 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<void> 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<Uint8List> getEncryptedKey(final String encKey) async {
const FlutterSecureStorage secureStorage = FlutterSecureStorage();
final bool hasEncryptionKey = await secureStorage.containsKey(key: encKey);
if (!hasEncryptionKey) {
final List<int> 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<ScaffoldMessengerState> scaffoldMessengerKey =
GlobalKey<ScaffoldMessengerState>();
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
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(),
),
2022-12-27 05:45:05 +00:00
BlocProvider(
create: (final _) => ItemsCubit()..load(),
),
BlocProvider(
create: (final _) => CouriersCubit()..load(),
),
BlocProvider(
create: (final _) => OrdersCubit()..load(),
),
],
child: child,
);
}