119 lines
3.2 KiB
Dart
119 lines
3.2 KiB
Dart
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';
|
|
|
|
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(),
|
|
),
|
|
],
|
|
child: child,
|
|
);
|
|
}
|