Browse Source

remove use of future in value of ToggleSwitchWidget

ashilkn 2 years ago
parent
commit
abe868cbf8

+ 6 - 19
lib/ui/backup_settings_screen.dart

@@ -55,12 +55,8 @@ class BackupSettingsScreen extends StatelessWidget {
                               ),
                               ),
                               menuItemColor: colorScheme.fillFaint,
                               menuItemColor: colorScheme.fillFaint,
                               trailingSwitch: ToggleSwitchWidget(
                               trailingSwitch: ToggleSwitchWidget(
-                                value: () {
-                                  return Future.value(
-                                    Configuration.instance
-                                        .shouldBackupOverMobileData(),
-                                  );
-                                },
+                                value: () => Configuration.instance
+                                    .shouldBackupOverMobileData(),
                                 onChanged: () async {
                                 onChanged: () async {
                                   await Configuration.instance
                                   await Configuration.instance
                                       .setBackupOverMobileData(
                                       .setBackupOverMobileData(
@@ -68,8 +64,6 @@ class BackupSettingsScreen extends StatelessWidget {
                                         .shouldBackupOverMobileData(),
                                         .shouldBackupOverMobileData(),
                                   );
                                   );
                                 },
                                 },
-                                initialValue: Configuration.instance
-                                    .shouldBackupOverMobileData(),
                               ),
                               ),
                               borderRadius: 8,
                               borderRadius: 8,
                               alignCaptionedTextToLeft: true,
                               alignCaptionedTextToLeft: true,
@@ -86,15 +80,12 @@ class BackupSettingsScreen extends StatelessWidget {
                               ),
                               ),
                               menuItemColor: colorScheme.fillFaint,
                               menuItemColor: colorScheme.fillFaint,
                               trailingSwitch: ToggleSwitchWidget(
                               trailingSwitch: ToggleSwitchWidget(
-                                value: () => Future.value(
-                                  Configuration.instance.shouldBackupVideos(),
-                                ),
+                                value: () =>
+                                    Configuration.instance.shouldBackupVideos(),
                                 onChanged: () => Configuration.instance
                                 onChanged: () => Configuration.instance
                                     .setShouldBackupVideos(
                                     .setShouldBackupVideos(
                                   !Configuration.instance.shouldBackupVideos(),
                                   !Configuration.instance.shouldBackupVideos(),
                                 ),
                                 ),
-                                initialValue:
-                                    Configuration.instance.shouldBackupVideos(),
                               ),
                               ),
                               borderRadius: 8,
                               borderRadius: 8,
                               alignCaptionedTextToLeft: true,
                               alignCaptionedTextToLeft: true,
@@ -114,10 +105,8 @@ class BackupSettingsScreen extends StatelessWidget {
                                     ),
                                     ),
                                     menuItemColor: colorScheme.fillFaint,
                                     menuItemColor: colorScheme.fillFaint,
                                     trailingSwitch: ToggleSwitchWidget(
                                     trailingSwitch: ToggleSwitchWidget(
-                                      value: () => Future.value(
-                                        Configuration.instance
-                                            .shouldKeepDeviceAwake(),
-                                      ),
+                                      value: () => Configuration.instance
+                                          .shouldKeepDeviceAwake(),
                                       onChanged: () {
                                       onChanged: () {
                                         return Configuration.instance
                                         return Configuration.instance
                                             .setShouldKeepDeviceAwake(
                                             .setShouldKeepDeviceAwake(
@@ -125,8 +114,6 @@ class BackupSettingsScreen extends StatelessWidget {
                                               .shouldKeepDeviceAwake(),
                                               .shouldKeepDeviceAwake(),
                                         );
                                         );
                                       },
                                       },
-                                      initialValue: Configuration.instance
-                                          .shouldKeepDeviceAwake(),
                                     ),
                                     ),
                                     borderRadius: 8,
                                     borderRadius: 8,
                                     alignCaptionedTextToLeft: true,
                                     alignCaptionedTextToLeft: true,

+ 61 - 73
lib/ui/components/toggle_switch_widget.dart

@@ -10,18 +10,14 @@ enum ExecutionState {
 }
 }
 
 
 typedef FutureVoidCallBack = Future<void> Function();
 typedef FutureVoidCallBack = Future<void> Function();
-typedef FutureBoolCallBack = Future<bool> Function();
+typedef BoolCallBack = bool Function();
 
 
 class ToggleSwitchWidget extends StatefulWidget {
 class ToggleSwitchWidget extends StatefulWidget {
-  final FutureBoolCallBack value;
-
-  ///Make sure to use completer if onChanged callback has other async functions inside
+  final BoolCallBack value;
   final FutureVoidCallBack onChanged;
   final FutureVoidCallBack onChanged;
-  final bool initialValue;
   const ToggleSwitchWidget({
   const ToggleSwitchWidget({
     required this.value,
     required this.value,
     required this.onChanged,
     required this.onChanged,
-    required this.initialValue,
     Key? key,
     Key? key,
   }) : super(key: key);
   }) : super(key: key);
 
 
@@ -30,15 +26,13 @@ class ToggleSwitchWidget extends StatefulWidget {
 }
 }
 
 
 class _ToggleSwitchWidgetState extends State<ToggleSwitchWidget> {
 class _ToggleSwitchWidgetState extends State<ToggleSwitchWidget> {
-  late Future<bool> futureToggleValue;
   late bool toggleValue;
   late bool toggleValue;
   ExecutionState executionState = ExecutionState.idle;
   ExecutionState executionState = ExecutionState.idle;
   final _debouncer = Debouncer(const Duration(milliseconds: 300));
   final _debouncer = Debouncer(const Duration(milliseconds: 300));
 
 
   @override
   @override
   void initState() {
   void initState() {
-    futureToggleValue = widget.value.call();
-    toggleValue = widget.initialValue;
+    toggleValue = widget.value.call();
     super.initState();
     super.initState();
   }
   }
 
 
@@ -47,74 +41,68 @@ class _ToggleSwitchWidgetState extends State<ToggleSwitchWidget> {
     final enteColorScheme = Theme.of(context).colorScheme.enteTheme.colorScheme;
     final enteColorScheme = Theme.of(context).colorScheme.enteTheme.colorScheme;
     final Widget stateIcon = _stateIcon(enteColorScheme);
     final Widget stateIcon = _stateIcon(enteColorScheme);
 
 
-    return FutureBuilder(
-      future: futureToggleValue,
-      builder: (context, snapshot) {
-        return Row(
-          children: [
-            Padding(
-              padding: const EdgeInsets.only(right: 2),
-              child: AnimatedSwitcher(
-                duration: const Duration(milliseconds: 175),
-                switchInCurve: Curves.easeInExpo,
-                switchOutCurve: Curves.easeOutExpo,
-                child: stateIcon,
-              ),
-            ),
-            SizedBox(
-              height: 31,
-              child: FittedBox(
-                fit: BoxFit.contain,
-                child: Switch.adaptive(
-                  activeColor: enteColorScheme.primary400,
-                  inactiveTrackColor: enteColorScheme.fillMuted,
-                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
-                  value: toggleValue,
-                  onChanged: (negationOfToggleValue) async {
-                    setState(() {
-                      toggleValue = negationOfToggleValue;
-                      //start showing inProgress statu icons if toggle takes more than debounce time
-                      _debouncer.run(
-                        () => Future(
-                          () {
-                            setState(() {
-                              executionState = ExecutionState.inProgress;
-                            });
-                          },
-                        ),
-                      );
-                    });
-                    final Stopwatch stopwatch = Stopwatch()..start();
-                    await widget.onChanged.call();
-                    //for toggle feedback on short unsuccessful onChanged
-                    await _feedbackOnUnsuccessfulToggle(stopwatch);
-                    //debouncer gets canceled if onChanged takes less than debounce time
-                    _debouncer.cancelDebounce();
+    return Row(
+      children: [
+        Padding(
+          padding: const EdgeInsets.only(right: 2),
+          child: AnimatedSwitcher(
+            duration: const Duration(milliseconds: 175),
+            switchInCurve: Curves.easeInExpo,
+            switchOutCurve: Curves.easeOutExpo,
+            child: stateIcon,
+          ),
+        ),
+        SizedBox(
+          height: 31,
+          child: FittedBox(
+            fit: BoxFit.contain,
+            child: Switch.adaptive(
+              activeColor: enteColorScheme.primary400,
+              inactiveTrackColor: enteColorScheme.fillMuted,
+              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
+              value: toggleValue,
+              onChanged: (negationOfToggleValue) async {
+                setState(() {
+                  toggleValue = negationOfToggleValue;
+                  //start showing inProgress statu icons if toggle takes more than debounce time
+                  _debouncer.run(
+                    () => Future(
+                      () {
+                        setState(() {
+                          executionState = ExecutionState.inProgress;
+                        });
+                      },
+                    ),
+                  );
+                });
+                final Stopwatch stopwatch = Stopwatch()..start();
+                await widget.onChanged.call();
+                //for toggle feedback on short unsuccessful onChanged
+                await _feedbackOnUnsuccessfulToggle(stopwatch);
+                //debouncer gets canceled if onChanged takes less than debounce time
+                _debouncer.cancelDebounce();
 
 
-                    widget.value.call().then((newValue) {
-                      setState(() {
-                        if (toggleValue == newValue) {
-                          if (executionState == ExecutionState.inProgress) {
-                            executionState = ExecutionState.successful;
-                            Future.delayed(const Duration(seconds: 2), () {
-                              setState(() {
-                                executionState = ExecutionState.idle;
-                              });
-                            });
-                          }
-                        } else {
-                          toggleValue = !toggleValue;
+                final newValue = widget.value.call();
+                setState(() {
+                  if (toggleValue == newValue) {
+                    if (executionState == ExecutionState.inProgress) {
+                      executionState = ExecutionState.successful;
+                      Future.delayed(const Duration(seconds: 2), () {
+                        setState(() {
                           executionState = ExecutionState.idle;
                           executionState = ExecutionState.idle;
-                        }
+                        });
                       });
                       });
-                    });
-                  },
-                ),
-              ),
+                    }
+                  } else {
+                    toggleValue = !toggleValue;
+                    executionState = ExecutionState.idle;
+                  }
+                });
+              },
             ),
             ),
-          ],
-        );
-      },
+          ),
+        ),
+      ],
     );
     );
   }
   }
 
 

+ 3 - 6
lib/ui/settings/security_section_widget.dart

@@ -69,8 +69,7 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
               title: "Two-factor",
               title: "Two-factor",
             ),
             ),
             trailingSwitch: ToggleSwitchWidget(
             trailingSwitch: ToggleSwitchWidget(
-              value: () => UserService.instance.fetchTwoFactorStatus(),
-              initialValue: _config.hasEnabledTwoFactor(),
+              value: () => _config.hasEnabledTwoFactor(),
               onChanged: () async {
               onChanged: () async {
                 final hasAuthenticated = await LocalAuthenticationService
                 final hasAuthenticated = await LocalAuthenticationService
                     .instance
                     .instance
@@ -102,7 +101,7 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
           title: "Lockscreen",
           title: "Lockscreen",
         ),
         ),
         trailingSwitch: ToggleSwitchWidget(
         trailingSwitch: ToggleSwitchWidget(
-          value: () => Future.value(_config.shouldShowLockScreen()),
+          value: () => _config.shouldShowLockScreen(),
           onChanged: () async {
           onChanged: () async {
             await LocalAuthenticationService.instance
             await LocalAuthenticationService.instance
                 .requestLocalAuthForLockScreen(
                 .requestLocalAuthForLockScreen(
@@ -112,7 +111,6 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
               "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.",
             );
             );
           },
           },
-          initialValue: _config.shouldShowLockScreen(),
         ),
         ),
       ),
       ),
       sectionOptionSpacing,
       sectionOptionSpacing,
@@ -125,9 +123,8 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
               title: "Hide from recents",
               title: "Hide from recents",
             ),
             ),
             trailingSwitch: ToggleSwitchWidget(
             trailingSwitch: ToggleSwitchWidget(
-              value: () => Future.value(_config.shouldHideFromRecents()),
+              value: () => _config.shouldHideFromRecents(),
               onChanged: _hideFromRecentsOnChanged,
               onChanged: _hideFromRecentsOnChanged,
-              initialValue: _config.shouldHideFromRecents(),
             ),
             ),
           ),
           ),
           sectionOptionSpacing,
           sectionOptionSpacing,