Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/alexandrim0/keyper
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrsosnin committed Dec 24, 2023
2 parents 07b0f6d + 862cf49 commit 98e2c65
Show file tree
Hide file tree
Showing 41 changed files with 688 additions and 700 deletions.
22 changes: 18 additions & 4 deletions lib/app/di.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ class DI {
GetIt.I.registerSingleton<AnalyticsService>(await AnalyticsService.init());

// Managers
GetIt.I.registerSingleton<AuthManager>(await AuthManager().init());
GetIt.I.registerSingleton<NetworkManager>(await NetworkManager().init());
GetIt.I.registerSingleton<AuthManager>(
await AuthManager().init(),
dispose: (s) => s.dispose(),
);
GetIt.I.registerSingleton<NetworkManager>(
await NetworkManager().init(),
dispose: (s) => s.dispose(),
);

// Repositories
final encryptionCipher = HiveAesCipher(
Expand All @@ -43,21 +49,29 @@ class DI {
Hive.init(preferences.pathDataDir);
GetIt.I.registerSingleton<SettingsRepository>(
await SettingsRepository().init(),
dispose: (s) => s.dispose(),
);
GetIt.I.registerSingleton<VaultRepository>(
await VaultRepository().init(
encryptionCipher: encryptionCipher,
),
dispose: (s) => s.dispose(),
);
GetIt.I.registerSingleton<MessageRepository>(
await MessageRepository().init(
encryptionCipher: encryptionCipher,
),
dispose: (s) => s.dispose(),
);

// Interactors
GetIt.I.registerSingleton<MessageInteractor>(MessageInteractor());
GetIt.I.registerSingleton<VaultInteractor>(VaultInteractor());
GetIt.I.registerSingleton<MessageInteractor>(
MessageInteractor(),
dispose: (s) => s.dispose(),
);
GetIt.I.registerSingleton<VaultInteractor>(
VaultInteractor(),
);

_isInited = true;
}
Expand Down
20 changes: 19 additions & 1 deletion lib/data/repositories/settings_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'package:hive/hive.dart';
import 'package:flutter/widgets.dart';

export 'package:get_it/get_it.dart';

enum SettingsRepositoryKeys {
keyIsDarkModeOn,
Expand All @@ -14,16 +17,31 @@ typedef SettingsRepositoryEvent<T extends Object> = ({
T? value,
});

class SettingsRepository {
class SettingsRepository with WidgetsBindingObserver {
final _events = StreamController<SettingsRepositoryEvent>.broadcast();

late final Box<String> _storage;

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.paused:
_storage.flush();
case _:
}
}

Future<SettingsRepository> init() async {
_storage = await Hive.openBox<String>('settings');
WidgetsBinding.instance.addObserver(this);
return this;
}

Future<void> dispose() async {
WidgetsBinding.instance.removeObserver(this);
}

T? get<T extends Object>(
SettingsRepositoryKeys key, {
T? defaultValue,
Expand Down
69 changes: 57 additions & 12 deletions lib/feature/auth/data/auth_manager.dart
Original file line number Diff line number Diff line change
@@ -1,40 +1,79 @@
import 'dart:async';

import 'package:get_it/get_it.dart';
import 'package:flutter/widgets.dart';

import 'package:guardian_keyper/data/services/preferences_service.dart';

import 'auth_service.dart';
import 'package:guardian_keyper/feature/auth/data/auth_service.dart';

export 'package:get_it/get_it.dart';

typedef AuthManagerState = ({
bool isBiometricsEnabled,
bool hasBiometrics,
});

/// Depends on [PreferencesService]
class AuthManager {
class AuthManager with WidgetsBindingObserver {
AuthManager({
AuthService? authService,
}) : _authService = authService ?? AuthService();
PreferencesService? prefService,
}) : _authService = authService ?? AuthService(),
_preferencesService = prefService ?? GetIt.I<PreferencesService>();

final AuthService _authService;

late final vibrate = _authService.vibrate;
late final getHasBiometrics = _authService.getHasBiometrics;
final PreferencesService _preferencesService;

final _stateStreamController = StreamController<AuthManagerState>.broadcast();

final _preferencesService = GetIt.I<PreferencesService>();
late final vibrate = _authService.vibrate;

late String _passCode;

late bool _hasBiometrics;

late bool _isBiometricsEnabled;

String get passCode => _passCode;

bool get hasBiometrics => _hasBiometrics;

bool get isBiometricsEnabled => _isBiometricsEnabled;

bool get useBiometrics => hasBiometrics && isBiometricsEnabled;

Stream<AuthManagerState> get state => _stateStreamController.stream;

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.resumed:
_authService.getHasBiometrics().then((v) => _hasBiometrics = v);
case AppLifecycleState.paused:
case _:
}
_updateState();
}

Future<AuthManager> init() async {
_passCode =
await _preferencesService.get<String>(PreferencesKeys.keyPassCode) ??
'';
_hasBiometrics = await _authService.getHasBiometrics();
_isBiometricsEnabled = await _preferencesService
.get<bool>(PreferencesKeys.keyIsBiometricsEnabled) ??
true;
_passCode =
await _preferencesService.get<String>(PreferencesKeys.keyPassCode) ??
'';
WidgetsBinding.instance.addObserver(this);
return this;
}

Future<bool> getUseBiometrics() async =>
await getHasBiometrics() && isBiometricsEnabled;
Future<void> dispose() async {
WidgetsBinding.instance.removeObserver(this);
await _stateStreamController.close();
}

Future<bool> localAuthenticate({
bool biometricOnly = true,
Expand All @@ -58,11 +97,17 @@ class AuthManager {
);
}

Future<void> setBiometrics({required bool isEnabled}) {
Future<void> setIsBiometricsEnabled(bool isEnabled) {
_isBiometricsEnabled = isEnabled;
_updateState();
return _preferencesService.set<bool>(
PreferencesKeys.keyIsBiometricsEnabled,
isEnabled,
);
}

void _updateState() => _stateStreamController.add((
isBiometricsEnabled: _isBiometricsEnabled,
hasBiometrics: _hasBiometrics,
));
}
3 changes: 1 addition & 2 deletions lib/feature/auth/ui/dialogs/on_ask_auth_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:get_it/get_it.dart';
import 'package:flutter_screen_lock/flutter_screen_lock.dart';

import 'package:guardian_keyper/ui/widgets/common.dart';
Expand All @@ -12,7 +11,7 @@ class OnAskAuthDialog {
required VoidCallback onUnlocked,
}) async {
final authManager = GetIt.I<AuthManager>();
final void Function()? authBio = await authManager.getUseBiometrics()
final void Function()? authBio = authManager.useBiometrics
? () async {
if (await authManager.localAuthenticate()) {
if (context.mounted) Navigator.of(context).pop();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:get_it/get_it.dart';
import 'package:flutter_screen_lock/flutter_screen_lock.dart';

import 'package:guardian_keyper/consts.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/feature/auth/ui/dialogs/on_create_pass_code.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:get_it/get_it.dart';
import 'package:flutter_screen_lock/flutter_screen_lock.dart';

import 'package:guardian_keyper/consts.dart';
Expand Down
3 changes: 1 addition & 2 deletions lib/feature/auth/ui/dialogs/on_demand_auth_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:get_it/get_it.dart';
import 'package:flutter_screen_lock/flutter_screen_lock.dart';

import 'package:guardian_keyper/ui/widgets/common.dart';
Expand All @@ -9,7 +8,7 @@ import 'auth_dialog_mixin.dart';
class OnDemandAuthDialog {
static Future<void> show(BuildContext context) async {
final authManager = GetIt.I<AuthManager>();
final void Function()? authBio = await authManager.getUseBiometrics()
final void Function()? authBio = authManager.useBiometrics
? () async {
if (await authManager.localAuthenticate()) {
if (context.mounted) Navigator.of(context).pop();
Expand Down
File renamed without changes.
Loading

0 comments on commit 98e2c65

Please sign in to comment.