Jelajahi Sumber

contain rebuilds upon changing ToggleSwitchWidget inside itself

ashilkn 2 tahun lalu
induk
melakukan
c24bddd11b

+ 25 - 22
lib/ui/backup_settings_screen.dart

@@ -12,14 +12,9 @@ import 'package:photos/ui/components/title_bar_title_widget.dart';
 import 'package:photos/ui/components/title_bar_widget.dart';
 import 'package:photos/ui/components/title_bar_widget.dart';
 import 'package:photos/ui/components/toggle_switch_widget.dart';
 import 'package:photos/ui/components/toggle_switch_widget.dart';
 
 
-class BackupSettingsScreen extends StatefulWidget {
+class BackupSettingsScreen extends StatelessWidget {
   const BackupSettingsScreen({super.key});
   const BackupSettingsScreen({super.key});
 
 
-  @override
-  State<BackupSettingsScreen> createState() => _BackupSettingsScreenState();
-}
-
-class _BackupSettingsScreenState extends State<BackupSettingsScreen> {
   @override
   @override
   Widget build(BuildContext context) {
   Widget build(BuildContext context) {
     final colorScheme = getEnteColorScheme(context);
     final colorScheme = getEnteColorScheme(context);
@@ -60,12 +55,16 @@ class _BackupSettingsScreenState extends State<BackupSettingsScreen> {
                               ),
                               ),
                               menuItemColor: colorScheme.fillFaint,
                               menuItemColor: colorScheme.fillFaint,
                               trailingSwitch: ToggleSwitchWidget(
                               trailingSwitch: ToggleSwitchWidget(
-                                value: Configuration.instance
-                                    .shouldBackupOverMobileData(),
-                                onChanged: (value) async {
-                                  Configuration.instance
-                                      .setBackupOverMobileData(value);
-                                  setState(() {});
+                                value: () {
+                                  return Configuration.instance
+                                      .shouldBackupOverMobileData();
+                                },
+                                onChanged: () async {
+                                  await Configuration.instance
+                                      .setBackupOverMobileData(
+                                    !Configuration.instance
+                                        .shouldBackupOverMobileData(),
+                                  );
                                 },
                                 },
                               ),
                               ),
                               borderRadius: 8,
                               borderRadius: 8,
@@ -80,13 +79,12 @@ class _BackupSettingsScreenState extends State<BackupSettingsScreen> {
                               ),
                               ),
                               menuItemColor: colorScheme.fillFaint,
                               menuItemColor: colorScheme.fillFaint,
                               trailingSwitch: ToggleSwitchWidget(
                               trailingSwitch: ToggleSwitchWidget(
-                                value:
+                                value: () =>
                                     Configuration.instance.shouldBackupVideos(),
                                     Configuration.instance.shouldBackupVideos(),
-                                onChanged: (value) async {
-                                  Configuration.instance
-                                      .setShouldBackupVideos(value);
-                                  setState(() {});
-                                },
+                                onChanged: () => Configuration.instance
+                                    .setShouldBackupVideos(
+                                  !Configuration.instance.shouldBackupVideos(),
+                                ),
                               ),
                               ),
                               borderRadius: 8,
                               borderRadius: 8,
                               alignCaptionedTextToLeft: true,
                               alignCaptionedTextToLeft: true,
@@ -106,9 +104,15 @@ class _BackupSettingsScreenState extends State<BackupSettingsScreen> {
                                     ),
                                     ),
                                     menuItemColor: colorScheme.fillFaint,
                                     menuItemColor: colorScheme.fillFaint,
                                     trailingSwitch: ToggleSwitchWidget(
                                     trailingSwitch: ToggleSwitchWidget(
-                                      value: Configuration.instance
+                                      value: () => Configuration.instance
                                           .shouldKeepDeviceAwake(),
                                           .shouldKeepDeviceAwake(),
-                                      onChanged: _autoLockOnChanged,
+                                      onChanged: () {
+                                        return _autoLockOnChanged(
+                                          !Configuration.instance
+                                              .shouldKeepDeviceAwake(),
+                                          context,
+                                        );
+                                      },
                                     ),
                                     ),
                                     borderRadius: 8,
                                     borderRadius: 8,
                                     alignCaptionedTextToLeft: true,
                                     alignCaptionedTextToLeft: true,
@@ -134,7 +138,7 @@ class _BackupSettingsScreenState extends State<BackupSettingsScreen> {
     );
     );
   }
   }
 
 
-  void _autoLockOnChanged(value) async {
+  Future<void> _autoLockOnChanged(value, context) async {
     if (value) {
     if (value) {
       final choice = await showChoiceDialog(
       final choice = await showChoiceDialog(
         context,
         context,
@@ -148,6 +152,5 @@ class _BackupSettingsScreenState extends State<BackupSettingsScreen> {
       }
       }
     }
     }
     await Configuration.instance.setShouldKeepDeviceAwake(value);
     await Configuration.instance.setShouldKeepDeviceAwake(value);
-    setState(() {});
   }
   }
 }
 }

+ 8 - 4
lib/ui/components/toggle_switch_widget.dart

@@ -1,10 +1,11 @@
 import 'package:flutter/material.dart';
 import 'package:flutter/material.dart';
 import 'package:photos/ente_theme_data.dart';
 import 'package:photos/ente_theme_data.dart';
 
 
-typedef OnChangedCallBack = void Function(bool);
+typedef OnChangedCallBack = Future<void> Function();
+typedef ValueCallBack = bool Function();
 
 
 class ToggleSwitchWidget extends StatefulWidget {
 class ToggleSwitchWidget extends StatefulWidget {
-  final bool value;
+  final ValueCallBack value;
   final OnChangedCallBack onChanged;
   final OnChangedCallBack onChanged;
   const ToggleSwitchWidget({
   const ToggleSwitchWidget({
     required this.value,
     required this.value,
@@ -30,8 +31,11 @@ class _ToggleSwitchWidgetState extends State<ToggleSwitchWidget> {
             activeColor: enteColorScheme.primary400,
             activeColor: enteColorScheme.primary400,
             inactiveTrackColor: enteColorScheme.fillMuted,
             inactiveTrackColor: enteColorScheme.fillMuted,
             materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
             materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
-            value: widget.value,
-            onChanged: widget.onChanged,
+            value: widget.value.call(),
+            onChanged: (value) async {
+              await widget.onChanged.call();
+              setState(() {});
+            },
           ),
           ),
         ),
         ),
       ),
       ),

+ 79 - 85
lib/ui/settings/security_section_widget.dart

@@ -73,8 +73,8 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
                 ),
                 ),
                 trailingSwitch: snapshot.hasData
                 trailingSwitch: snapshot.hasData
                     ? ToggleSwitchWidget(
                     ? ToggleSwitchWidget(
-                        value: snapshot.data,
-                        onChanged: (value) async {
+                        value: () => snapshot.data,
+                        onChanged: () async {
                           final hasAuthenticated =
                           final hasAuthenticated =
                               await LocalAuthenticationService.instance
                               await LocalAuthenticationService.instance
                                   .requestLocalAuthentication(
                                   .requestLocalAuthentication(
@@ -82,7 +82,7 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
                             "Please authenticate to configure two-factor authentication",
                             "Please authenticate to configure two-factor authentication",
                           );
                           );
                           if (hasAuthenticated) {
                           if (hasAuthenticated) {
-                            if (value) {
+                            if (!snapshot.data) {
                               UserService.instance.setupTwoFactor(context);
                               UserService.instance.setupTwoFactor(context);
                             } else {
                             } else {
                               _disableTwoFactor();
                               _disableTwoFactor();
@@ -106,18 +106,15 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
           title: "Lockscreen",
           title: "Lockscreen",
         ),
         ),
         trailingSwitch: ToggleSwitchWidget(
         trailingSwitch: ToggleSwitchWidget(
-          value: _config.shouldShowLockScreen(),
-          onChanged: (value) async {
-            final hasAuthenticated = await LocalAuthenticationService.instance
+          value: () => _config.shouldShowLockScreen(),
+          onChanged: () async {
+            await LocalAuthenticationService.instance
                 .requestLocalAuthForLockScreen(
                 .requestLocalAuthForLockScreen(
               context,
               context,
-              value,
+              !_config.shouldShowLockScreen(),
               "Please authenticate to change lockscreen setting",
               "Please authenticate to change lockscreen setting",
               "To enable lockscreen, please setup device passcode or screen lock in your system settings.",
               "To enable lockscreen, please setup device passcode or screen lock in your system settings.",
             );
             );
-            if (hasAuthenticated) {
-              setState(() {});
-            }
           },
           },
         ),
         ),
       ),
       ),
@@ -131,81 +128,8 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
               title: "Hide from recents",
               title: "Hide from recents",
             ),
             ),
             trailingSwitch: ToggleSwitchWidget(
             trailingSwitch: ToggleSwitchWidget(
-              value: _config.shouldHideFromRecents(),
-              onChanged: (value) async {
-                if (value) {
-                  final AlertDialog alert = AlertDialog(
-                    title: const Text("Hide from recents?"),
-                    content: SingleChildScrollView(
-                      child: Column(
-                        mainAxisAlignment: MainAxisAlignment.start,
-                        crossAxisAlignment: CrossAxisAlignment.start,
-                        children: const [
-                          Text(
-                            "Hiding from the task switcher will prevent you from taking screenshots in this app.",
-                            style: TextStyle(
-                              height: 1.5,
-                            ),
-                          ),
-                          Padding(padding: EdgeInsets.all(8)),
-                          Text(
-                            "Are you sure?",
-                            style: TextStyle(
-                              height: 1.5,
-                            ),
-                          ),
-                        ],
-                      ),
-                    ),
-                    actions: [
-                      TextButton(
-                        child: Text(
-                          "No",
-                          style: TextStyle(
-                            color:
-                                Theme.of(context).colorScheme.defaultTextColor,
-                          ),
-                        ),
-                        onPressed: () {
-                          Navigator.of(context, rootNavigator: true)
-                              .pop('dialog');
-                        },
-                      ),
-                      TextButton(
-                        child: Text(
-                          "Yes",
-                          style: TextStyle(
-                            color:
-                                Theme.of(context).colorScheme.defaultTextColor,
-                          ),
-                        ),
-                        onPressed: () async {
-                          Navigator.of(context, rootNavigator: true)
-                              .pop('dialog');
-                          await _config.setShouldHideFromRecents(true);
-                          await FlutterWindowManager.addFlags(
-                            FlutterWindowManager.FLAG_SECURE,
-                          );
-                          setState(() {});
-                        },
-                      ),
-                    ],
-                  );
-
-                  showDialog(
-                    context: context,
-                    builder: (BuildContext context) {
-                      return alert;
-                    },
-                  );
-                } else {
-                  await _config.setShouldHideFromRecents(false);
-                  await FlutterWindowManager.clearFlags(
-                    FlutterWindowManager.FLAG_SECURE,
-                  );
-                  setState(() {});
-                }
-              },
+              value: () => _config.shouldHideFromRecents(),
+              onChanged: _hideFromRecentsOnChanged,
             ),
             ),
           ),
           ),
           sectionOptionSpacing,
           sectionOptionSpacing,
@@ -284,4 +208,74 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
       },
       },
     );
     );
   }
   }
+
+  Future<void> _hideFromRecentsOnChanged() async {
+    if (!_config.shouldHideFromRecents()) {
+      final AlertDialog alert = AlertDialog(
+        title: const Text("Hide from recents?"),
+        content: SingleChildScrollView(
+          child: Column(
+            mainAxisAlignment: MainAxisAlignment.start,
+            crossAxisAlignment: CrossAxisAlignment.start,
+            children: const [
+              Text(
+                "Hiding from the task switcher will prevent you from taking screenshots in this app.",
+                style: TextStyle(
+                  height: 1.5,
+                ),
+              ),
+              Padding(padding: EdgeInsets.all(8)),
+              Text(
+                "Are you sure?",
+                style: TextStyle(
+                  height: 1.5,
+                ),
+              ),
+            ],
+          ),
+        ),
+        actions: [
+          TextButton(
+            child: Text(
+              "No",
+              style: TextStyle(
+                color: Theme.of(context).colorScheme.defaultTextColor,
+              ),
+            ),
+            onPressed: () {
+              Navigator.of(context, rootNavigator: true).pop('dialog');
+            },
+          ),
+          TextButton(
+            child: Text(
+              "Yes",
+              style: TextStyle(
+                color: Theme.of(context).colorScheme.defaultTextColor,
+              ),
+            ),
+            onPressed: () async {
+              Navigator.of(context, rootNavigator: true).pop('dialog');
+              await _config.setShouldHideFromRecents(true);
+              await FlutterWindowManager.addFlags(
+                FlutterWindowManager.FLAG_SECURE,
+              );
+              setState(() {});
+            },
+          ),
+        ],
+      );
+
+      showDialog(
+        context: context,
+        builder: (BuildContext context) {
+          return alert;
+        },
+      );
+    } else {
+      await _config.setShouldHideFromRecents(false);
+      await FlutterWindowManager.clearFlags(
+        FlutterWindowManager.FLAG_SECURE,
+      );
+    }
+  }
 }
 }