ente/lib/services/local_authentication_service.dart

69 lines
2 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:local_auth/local_auth.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/ui/tools/app_lock.dart';
import 'package:photos/utils/auth_util.dart';
2022-09-06 08:35:34 +00:00
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/toast_util.dart';
class LocalAuthenticationService {
LocalAuthenticationService._privateConstructor();
static final LocalAuthenticationService instance =
LocalAuthenticationService._privateConstructor();
Future<bool> requestLocalAuthentication(
BuildContext context,
2022-09-06 08:35:34 +00:00
String infoMessage,
) async {
2022-09-06 08:35:34 +00:00
if (await _isLocalAuthSupportedOnDevice()) {
AppLock.of(context).setEnabled(false);
2022-09-06 08:35:34 +00:00
final result = await requestAuthentication(infoMessage);
AppLock.of(context).setEnabled(
Configuration.instance.shouldShowLockScreen(),
);
if (!result) {
2022-09-06 08:35:34 +00:00
showToast(context, infoMessage);
return false;
} else {
return true;
}
}
return true;
}
2022-09-06 08:35:34 +00:00
Future<bool> requestLocalAuthForLockScreen(
BuildContext context,
bool shouldEnableLockScreen,
String infoMessage,
String errorDialogContent, [
String errorDialogTitle = "",
]) async {
2022-12-27 09:54:17 +00:00
if (await _isLocalAuthSupportedOnDevice()) {
2022-09-06 08:35:34 +00:00
AppLock.of(context).disable();
final result = await requestAuthentication(
infoMessage,
);
if (result) {
AppLock.of(context).setEnabled(shouldEnableLockScreen);
await Configuration.instance
.setShouldShowLockScreen(shouldEnableLockScreen);
return true;
} else {
AppLock.of(context)
.setEnabled(Configuration.instance.shouldShowLockScreen());
}
} else {
showErrorDialog(
context,
errorDialogTitle,
errorDialogContent,
);
}
return false;
}
Future<bool> _isLocalAuthSupportedOnDevice() async {
2022-12-27 09:54:17 +00:00
return LocalAuthentication().isDeviceSupported();
2022-09-06 08:35:34 +00:00
}
}