local_authentication_service.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import 'package:flutter/material.dart';
  2. import 'package:local_auth/local_auth.dart';
  3. import 'package:photos/core/configuration.dart';
  4. import 'package:photos/ui/tools/app_lock.dart';
  5. import 'package:photos/utils/auth_util.dart';
  6. import 'package:photos/utils/dialog_util.dart';
  7. import 'package:photos/utils/toast_util.dart';
  8. class LocalAuthenticationService {
  9. LocalAuthenticationService._privateConstructor();
  10. static final LocalAuthenticationService instance =
  11. LocalAuthenticationService._privateConstructor();
  12. Future<bool> requestLocalAuthentication(
  13. BuildContext context,
  14. String infoMessage,
  15. ) async {
  16. if (await _isLocalAuthSupportedOnDevice()) {
  17. AppLock.of(context)!.setEnabled(false);
  18. final result = await requestAuthentication(context, infoMessage);
  19. AppLock.of(context)!.setEnabled(
  20. Configuration.instance.shouldShowLockScreen(),
  21. );
  22. if (!result) {
  23. showToast(context, infoMessage);
  24. return false;
  25. } else {
  26. return true;
  27. }
  28. }
  29. return true;
  30. }
  31. Future<bool> requestLocalAuthForLockScreen(
  32. BuildContext context,
  33. bool shouldEnableLockScreen,
  34. String infoMessage,
  35. String errorDialogContent, [
  36. String errorDialogTitle = "",
  37. ]) async {
  38. if (await _isLocalAuthSupportedOnDevice()) {
  39. AppLock.of(context)!.disable();
  40. final result = await requestAuthentication(
  41. context,
  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 LocalAuthentication().isDeviceSupported();
  64. }
  65. }