Files
ente/lib/ui/components/toggle_switch_widget.dart
Prateek Sunal 5279a134e0 [FEAT] Auth Desktop (#416)
<!--
  Thanks for contributing!

Provide a description of your changes below and a general summary in the
title

Please look at the following checklist to ensure that your PR can be
accepted quickly:
-->

## Description

This is a PR that makes the auth build runnable on the Desktop platform.
Below is a screenshot for the same:
![Screenshot 2024-01-10 at 10 35
14 PM](https://github.com/ente-io/auth/assets/41370460/9efe00f9-9a49-48f9-a822-d578ddbe82bf)

Some things to note:
- LocalAuth will be bypassed on unsupported platforms (desktop)
- Switched to sodium and sodium-libs from flutter_sodium.
- QR code library is incompatible with desktop, So I removed access to
those pages.
- Bumped some dependencies and done some lint fixes
- Also add save key option as send file may not help on desktop (related
#380)


https://github.com/ente-io/auth/assets/41370460/3f3471e8-45f6-4146-88ac-b763d4f38b32

- Update Recovery Key Card UI
![Screenshot 2024-01-19 at 12 31
31 AM](https://github.com/ente-io/auth/assets/41370460/19db35cd-f151-4587-a74b-6c53bca0f23f)

So this is a step towards more updated code and better multiplatform
support.

## Type of Change

<!--- Put an `x` in all the boxes that apply: -->

- [ ] 🖼️ New icon
- [x]  New feature (non-breaking change which adds functionality)
- [ ] 🛠️ Bug fix (non-breaking change which fixes an issue)
- [ ]  Breaking change (fix or feature that would cause existing
functionality to change)
- [x] 🧹 Code refactor
- [ ]  Build configuration change
- [ ] 📝 Documentation
- [ ] 🗑️ Chore

---------

Co-authored-by: Neeraj Gupta <254676+ua741@users.noreply.github.com>
2024-02-12 21:06:41 +05:30

137 lines
4.5 KiB
Dart

import 'package:ente_auth/ente_theme_data.dart';
import 'package:ente_auth/models/execution_states.dart';
import 'package:ente_auth/models/typedefs.dart';
import 'package:ente_auth/ui/common/loading_widget.dart';
import 'package:ente_auth/utils/debouncer.dart';
import 'package:flutter/material.dart';
typedef OnChangedCallBack = void Function(bool);
class ToggleSwitchWidget extends StatefulWidget {
final BoolCallBack value;
final FutureVoidCallback onChanged;
const ToggleSwitchWidget({
required this.value,
required this.onChanged,
super.key,
});
@override
State<ToggleSwitchWidget> createState() => _ToggleSwitchWidgetState();
}
class _ToggleSwitchWidgetState extends State<ToggleSwitchWidget> {
bool? toggleValue;
ExecutionState executionState = ExecutionState.idle;
final _debouncer = Debouncer(const Duration(milliseconds: 300));
@override
void initState() {
toggleValue = widget.value.call();
super.initState();
}
@override
Widget build(BuildContext context) {
final enteColorScheme = Theme.of(context).colorScheme.enteTheme.colorScheme;
final Widget stateIcon = _stateIcon(enteColorScheme);
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,
activeTrackColor: enteColorScheme.primary300,
inactiveTrackColor: enteColorScheme.fillMuted,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
value: toggleValue ?? false,
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().onError(
(error, stackTrace) => _debouncer.cancelDebounce(),
);
//for toggle feedback on short unsuccessful onChanged
await _feedbackOnUnsuccessfulToggle(stopwatch);
//debouncer gets canceled if onChanged takes less than debounce time
_debouncer.cancelDebounce();
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;
});
});
}
} else {
toggleValue = !toggleValue!;
executionState = ExecutionState.idle;
}
});
},
),
),
),
],
);
}
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 Padding(
padding: const EdgeInsets.symmetric(horizontal: 1),
child: Icon(
Icons.check_outlined,
size: 22,
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),
);
}
}
}