Browse Source

refactoring + variable name changes

ashilkn 2 years ago
parent
commit
4bf751ea28

+ 40 - 4
lib/services/local_authentication_service.dart

@@ -3,6 +3,7 @@ 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';
+import 'package:photos/utils/dialog_util.dart';
 import 'package:photos/utils/toast_util.dart';
 
 class LocalAuthenticationService {
@@ -12,16 +13,16 @@ class LocalAuthenticationService {
 
   Future<bool> requestLocalAuthentication(
     BuildContext context,
-    String reason,
+    String infoMessage,
   ) async {
-    if (await LocalAuthentication().isDeviceSupported()) {
+    if (await _isLocalAuthSupportedOnDevice()) {
       AppLock.of(context).setEnabled(false);
-      final result = await requestAuthentication(reason);
+      final result = await requestAuthentication(infoMessage);
       AppLock.of(context).setEnabled(
         Configuration.instance.shouldShowLockScreen(),
       );
       if (!result) {
-        showToast(context, reason);
+        showToast(context, infoMessage);
         return false;
       } else {
         return true;
@@ -29,4 +30,39 @@ class LocalAuthenticationService {
     }
     return true;
   }
+
+  Future<bool> requestLocalAuthForLockScreen(
+    BuildContext context,
+    bool shouldEnableLockScreen,
+    String infoMessage,
+    String errorDialogContent, [
+    String errorDialogTitle = "",
+  ]) async {
+    if (await LocalAuthentication().isDeviceSupported()) {
+      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 {
+    return await LocalAuthentication().isDeviceSupported();
+  }
 }

+ 2 - 2
lib/ui/account/delete_account_page.dart

@@ -142,13 +142,13 @@ class DeleteAccountPage extends StatelessWidget {
     BuildContext context,
     DeleteChallengeResponse response,
   ) async {
-    final hasAuthenticatedOrNoLocalAuth =
+    final hasAuthenticated =
         await LocalAuthenticationService.instance.requestLocalAuthentication(
       context,
       "Please authenticate to initiate account deletion",
     );
 
-    if (hasAuthenticatedOrNoLocalAuth) {
+    if (hasAuthenticated) {
       final choice = await showChoiceDialog(
         context,
         'Are you sure you want to delete your account?',

+ 9 - 12
lib/ui/settings/account_section_widget.dart

@@ -36,13 +36,12 @@ class AccountSectionWidgetState extends State<AccountSectionWidget> {
         GestureDetector(
           behavior: HitTestBehavior.translucent,
           onTap: () async {
-            final hasAuthenticatedOrNoLocalAuth =
-                await LocalAuthenticationService.instance
-                    .requestLocalAuthentication(
+            final hasAuthenticated = await LocalAuthenticationService.instance
+                .requestLocalAuthentication(
               context,
               "Please authenticate to view your recovery key",
             );
-            if (hasAuthenticatedOrNoLocalAuth) {
+            if (hasAuthenticated) {
               String recoveryKey;
               try {
                 recoveryKey = await _getOrCreateRecoveryKey();
@@ -70,13 +69,12 @@ class AccountSectionWidgetState extends State<AccountSectionWidget> {
         GestureDetector(
           behavior: HitTestBehavior.translucent,
           onTap: () async {
-            final hasAuthenticatedOrNoLocalAuth =
-                await LocalAuthenticationService.instance
-                    .requestLocalAuthentication(
+            final hasAuthenticated = await LocalAuthenticationService.instance
+                .requestLocalAuthentication(
               context,
               "Please authenticate to change your email",
             );
-            if (hasAuthenticatedOrNoLocalAuth) {
+            if (hasAuthenticated) {
               showDialog(
                 context: context,
                 builder: (BuildContext context) {
@@ -96,13 +94,12 @@ class AccountSectionWidgetState extends State<AccountSectionWidget> {
         GestureDetector(
           behavior: HitTestBehavior.translucent,
           onTap: () async {
-            final hasAuthenticatedOrNoLocalAuth =
-                await LocalAuthenticationService.instance
-                    .requestLocalAuthentication(
+            final hasAuthenticated = await LocalAuthenticationService.instance
+                .requestLocalAuthentication(
               context,
               "Please authenticate to change your password",
             );
-            if (hasAuthenticatedOrNoLocalAuth) {
+            if (hasAuthenticated) {
               Navigator.of(context).push(
                 MaterialPageRoute(
                   builder: (BuildContext context) {

+ 14 - 28
lib/ui/settings/security_section_widget.dart

@@ -4,7 +4,6 @@ import 'dart:io';
 import 'package:expandable/expandable.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_windowmanager/flutter_windowmanager.dart';
-import 'package:local_auth/local_auth.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/core/event_bus.dart';
 import 'package:photos/ente_theme_data.dart';
@@ -16,9 +15,6 @@ import 'package:photos/ui/common/loading_widget.dart';
 import 'package:photos/ui/settings/common_settings.dart';
 import 'package:photos/ui/settings/settings_section_title.dart';
 import 'package:photos/ui/settings/settings_text_item.dart';
-import 'package:photos/ui/tools/app_lock.dart';
-import 'package:photos/utils/auth_util.dart';
-import 'package:photos/utils/dialog_util.dart';
 
 class SecuritySectionWidget extends StatefulWidget {
   const SecuritySectionWidget({Key key}) : super(key: key);
@@ -81,13 +77,13 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
                       return Switch.adaptive(
                         value: snapshot.data,
                         onChanged: (value) async {
-                          final hasAuthenticatedOrNoLocalAuth =
+                          final hasAuthenticated =
                               await LocalAuthenticationService.instance
                                   .requestLocalAuthentication(
                             context,
                             "Please authenticate to configure two-factor authentication",
                           );
-                          if (hasAuthenticatedOrNoLocalAuth) {
+                          if (hasAuthenticated) {
                             if (value) {
                               UserService.instance.setupTwoFactor(context);
                             } else {
@@ -125,25 +121,16 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
             Switch.adaptive(
               value: _config.shouldShowLockScreen(),
               onChanged: (value) async {
-                if (await LocalAuthentication().isDeviceSupported()) {
-                  AppLock.of(context).disable();
-                  final result = await requestAuthentication(
-                    "Please authenticate to change lockscreen setting",
-                  );
-                  if (result) {
-                    AppLock.of(context).setEnabled(value);
-                    _config.setShouldShowLockScreen(value);
-                    setState(() {});
-                  } else {
-                    AppLock.of(context)
-                        .setEnabled(_config.shouldShowLockScreen());
-                  }
-                } else {
-                  showErrorDialog(
-                    context,
-                    "",
-                    "To enable the ente lockscreen, please setup the device passcode or screen lock in the system settings.",
-                  );
+                final hasAuthenticated = await LocalAuthenticationService
+                    .instance
+                    .requestLocalAuthForLockScreen(
+                  context,
+                  value,
+                  "Please authenticate to change lockscreen setting",
+                  "To enable lockscreen, please setup device passcode or screen lock in your system settings.",
+                );
+                if (hasAuthenticated) {
+                  setState(() {});
                 }
               },
             ),
@@ -254,13 +241,12 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
       GestureDetector(
         behavior: HitTestBehavior.translucent,
         onTap: () async {
-          final hasAuthenticatedOrNoLocalAuth = await LocalAuthenticationService
-              .instance
+          final hasAuthenticated = await LocalAuthenticationService.instance
               .requestLocalAuthentication(
             context,
             "Please authenticate to view your active sessions",
           );
-          if (hasAuthenticatedOrNoLocalAuth) {
+          if (hasAuthenticated) {
             Navigator.of(context).push(
               MaterialPageRoute(
                 builder: (BuildContext context) {