local_authentication_service.dart 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import "dart:async";
  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(context, 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 _isLocalAuthSupportedOnDevice()) {
  40. AppLock.of(context)!.disable();
  41. final result = await requestAuthentication(
  42. context,
  43. infoMessage,
  44. );
  45. if (result) {
  46. AppLock.of(context)!.setEnabled(shouldEnableLockScreen);
  47. await Configuration.instance
  48. .setShouldShowLockScreen(shouldEnableLockScreen);
  49. return true;
  50. } else {
  51. AppLock.of(context)!
  52. .setEnabled(Configuration.instance.shouldShowLockScreen());
  53. }
  54. } else {
  55. unawaited(
  56. showErrorDialog(
  57. context,
  58. errorDialogTitle,
  59. errorDialogContent,
  60. ),
  61. );
  62. }
  63. return false;
  64. }
  65. Future<bool> _isLocalAuthSupportedOnDevice() async {
  66. return LocalAuthentication().isDeviceSupported();
  67. }
  68. }