ente/lib/ui/components/toggle_switch_widget.dart

131 lines
4.2 KiB
Dart
Raw Normal View History

2022-09-28 11:07:12 +00:00
import 'package:flutter/material.dart';
import 'package:photos/ente_theme_data.dart';
import 'package:photos/ui/common/loading_widget.dart';
2022-11-01 02:14:29 +00:00
import 'package:photos/utils/debouncer.dart';
2022-09-28 11:07:12 +00:00
enum ExecutionState {
idle,
inProgress,
successful,
}
typedef OnChangedCallBack = Future<void> Function();
typedef ValueCallBack = bool Function();
2022-10-05 15:57:41 +00:00
class ToggleSwitchWidget extends StatefulWidget {
final ValueCallBack value;
2022-10-05 15:57:41 +00:00
final OnChangedCallBack onChanged;
const ToggleSwitchWidget({
required this.value,
required this.onChanged,
Key? key,
}) : super(key: key);
2022-09-28 11:07:12 +00:00
@override
2022-10-05 15:57:41 +00:00
State<ToggleSwitchWidget> createState() => _ToggleSwitchWidgetState();
2022-09-28 11:07:12 +00:00
}
2022-10-05 15:57:41 +00:00
class _ToggleSwitchWidgetState extends State<ToggleSwitchWidget> {
late bool toggleValue;
2022-11-01 02:14:29 +00:00
ExecutionState executionState = ExecutionState.idle;
final _debouncer = Debouncer(const Duration(milliseconds: 300));
@override
void initState() {
toggleValue = widget.value.call();
super.initState();
}
2022-09-28 11:07:12 +00:00
@override
Widget build(BuildContext context) {
2022-10-05 15:57:41 +00:00
final enteColorScheme = Theme.of(context).colorScheme.enteTheme.colorScheme;
final Widget stateIcon = _stateIcon(enteColorScheme);
return Row(
children: [
AnimatedSwitcher(
2022-11-01 02:14:29 +00:00
duration: const Duration(milliseconds: 175),
switchInCurve: Curves.easeInExpo,
switchOutCurve: Curves.easeOutExpo,
child: stateIcon,
),
SizedBox(
height: 32,
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;
2022-11-01 02:14:29 +00:00
//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);
2022-11-01 02:14:29 +00:00
//debouncer gets canceled if onChanged takes less than debounce time
_debouncer.cancelDebounce();
setState(() {
final newValue = widget.value.call();
2022-11-01 02:14:29 +00:00
//if onchanged on toggle is successful
if (toggleValue == newValue) {
2022-11-01 02:14:29 +00:00
if (executionState == ExecutionState.inProgress) {
executionState = ExecutionState.successful;
Future.delayed(const Duration(seconds: 2), () {
2022-11-01 02:14:29 +00:00
setState(() {
executionState = ExecutionState.idle;
});
});
2022-11-01 02:14:29 +00:00
}
} else {
2022-11-01 02:14:29 +00:00
toggleValue = !toggleValue;
executionState = ExecutionState.idle;
}
});
},
),
),
2022-09-28 11:07:12 +00:00
),
],
2022-09-28 11:07:12 +00:00
);
}
Widget _stateIcon(enteColorScheme) {
if (executionState == ExecutionState.idle) {
return const SizedBox(width: 24);
} else if (executionState == ExecutionState.inProgress) {
return EnteLoadingWidget(
color: enteColorScheme.strokeMuted,
);
} else if (executionState == ExecutionState.successful) {
return Icon(
Icons.check_outlined,
color: enteColorScheme.primary500,
);
} else {
return const SizedBox(width: 24);
}
}
Future<void> _feedbackOnUnsuccessfulToggle(Stopwatch stopwatch) async {
final timeElapsed = stopwatch.elapsedMilliseconds;
if (timeElapsed < 200) {
await Future.delayed(
Duration(milliseconds: 200 - timeElapsed),
);
}
}
2022-09-28 11:07:12 +00:00
}