local_authentication_service.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // @dart=2.9
  2. import 'package:flutter/material.dart';
  3. import 'package:local_auth/local_auth.dart';
  4. import 'package:photos/core/configuration.dart';
  5. import 'package:photos/ui/tools/app_lock.dart';
  6. import 'package:photos/utils/auth_util.dart';
  7. import 'package:photos/utils/dialog_util.dart';
  8. import 'package:photos/utils/toast_util.dart';
  9. class LocalAuthenticationService {
  10. LocalAuthenticationService._privateConstructor();
  11. static final LocalAuthenticationService instance =
  12. LocalAuthenticationService._privateConstructor();
  13. Future<bool> requestLocalAuthentication(
  14. BuildContext context,
  15. String infoMessage,
  16. ) async {
  17. if (await _isLocalAuthSupportedOnDevice()) {
  18. AppLock.of(context).setEnabled(false);
  19. final result = await requestAuthentication(infoMessage);
  20. AppLock.of(context).setEnabled(
  21. Configuration.instance.shouldShowLockScreen(),
  22. );
  23. if (!result) {
  24. showToast(context, infoMessage);
  25. return false;
  26. } else {
  27. return true;
  28. }
  29. }
  30. return true;
  31. }
  32. Future<bool> requestLocalAuthForLockScreen(
  33. BuildContext context,
  34. bool shouldEnableLockScreen,
  35. String infoMessage,
  36. String errorDialogContent, [
  37. String errorDialogTitle = "",
  38. ]) async {
  39. if (await LocalAuthentication().isDeviceSupported()) {
  40. AppLock.of(context).disable();
  41. final result = await requestAuthentication(
  42. infoMessage,
  43. );
  44. if (result) {
  45. AppLock.of(context).setEnabled(shouldEnableLockScreen);
  46. await Configuration.instance
  47. .setShouldShowLockScreen(shouldEnableLockScreen);
  48. return true;
  49. } else {
  50. AppLock.of(context)
  51. .setEnabled(Configuration.instance.shouldShowLockScreen());
  52. }
  53. } else {
  54. showErrorDialog(
  55. context,
  56. errorDialogTitle,
  57. errorDialogContent,
  58. );
  59. }
  60. return false;
  61. }
  62. Future<bool> _isLocalAuthSupportedOnDevice() async {
  63. return await LocalAuthentication().isDeviceSupported();
  64. }
  65. }