local_authentication_service.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(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. infoMessage,
  42. );
  43. if (result) {
  44. AppLock.of(context)!.setEnabled(shouldEnableLockScreen);
  45. await Configuration.instance
  46. .setShouldShowLockScreen(shouldEnableLockScreen);
  47. return true;
  48. } else {
  49. AppLock.of(context)!
  50. .setEnabled(Configuration.instance.shouldShowLockScreen());
  51. }
  52. } else {
  53. showErrorDialog(
  54. context,
  55. errorDialogTitle,
  56. errorDialogContent,
  57. );
  58. }
  59. return false;
  60. }
  61. Future<bool> _isLocalAuthSupportedOnDevice() async {
  62. return LocalAuthentication().isDeviceSupported();
  63. }
  64. }