From 37f5c2316958c243ca21209eab0da4b44b7f0646 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Sat, 11 Feb 2023 16:52:08 +0530 Subject: [PATCH 1/2] Reduce UI rebuilds --- lib/ui/code_timer_progress.dart | 60 +++++++++++++++++++++++++++++++ lib/ui/code_widget.dart | 63 ++++++++++++++++++--------------- 2 files changed, 94 insertions(+), 29 deletions(-) create mode 100644 lib/ui/code_timer_progress.dart diff --git a/lib/ui/code_timer_progress.dart b/lib/ui/code_timer_progress.dart new file mode 100644 index 0000000000..e34c56a3f4 --- /dev/null +++ b/lib/ui/code_timer_progress.dart @@ -0,0 +1,60 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; +import 'package:flutter_animation_progress_bar/flutter_animation_progress_bar.dart'; + +class CodeTimerProgress extends StatefulWidget { + final int period; + + CodeTimerProgress({ + Key? key, + required this.period, + }) : super(key: key); + + @override + _CodeTimerProgressState createState() => _CodeTimerProgressState(); +} + +class _CodeTimerProgressState extends State { + Timer? _everySecondTimer; + late int _timeRemaining; + + @override + void initState() { + super.initState(); + _timeRemaining = widget.period; + _updateTimeRemaining(); + _everySecondTimer = + Timer.periodic(const Duration(milliseconds: 200), (Timer t) { + _updateTimeRemaining(); + }); + } + + void _updateTimeRemaining() { + int newTimeRemaining = + widget.period - (DateTime.now().second % widget.period); + if (newTimeRemaining != _timeRemaining) { + setState(() { + _timeRemaining = newTimeRemaining; + }); + } + } + + @override + void dispose() { + _everySecondTimer?.cancel(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return FAProgressBar( + currentValue: _timeRemaining / widget.period * 100, + size: 4, + animatedDuration: const Duration(milliseconds: 200), + progressColor: Colors.orange, + changeColorValue: 40, + changeProgressColor: Colors.green, + ); + } +} diff --git a/lib/ui/code_widget.dart b/lib/ui/code_widget.dart index 02dc7993fb..1b7132c640 100644 --- a/lib/ui/code_widget.dart +++ b/lib/ui/code_widget.dart @@ -6,10 +6,10 @@ import 'package:ente_auth/l10n/l10n.dart'; import 'package:ente_auth/models/code.dart'; import 'package:ente_auth/onboarding/view/setup_enter_secret_key_page.dart'; import 'package:ente_auth/store/code_store.dart'; +import 'package:ente_auth/ui/code_timer_progress.dart'; import 'package:ente_auth/utils/toast_util.dart'; import 'package:ente_auth/utils/totp_util.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_animation_progress_bar/flutter_animation_progress_bar.dart'; import 'package:flutter_slidable/flutter_slidable.dart'; class CodeWidget extends StatefulWidget { @@ -23,28 +23,29 @@ class CodeWidget extends StatefulWidget { class _CodeWidgetState extends State { Timer? _everySecondTimer; - int _timeRemaining = 30; + final ValueNotifier _currentCode = ValueNotifier(""); + final ValueNotifier _nextCode = ValueNotifier(""); @override void initState() { super.initState(); - _updateTimeRemaining(); + _currentCode.value = _getTotp(); + _nextCode.value = _getNextTotp(); _everySecondTimer = - Timer.periodic(const Duration(milliseconds: 200), (Timer t) { - setState(() { - _updateTimeRemaining(); - }); + Timer.periodic(const Duration(milliseconds: 500), (Timer t) { + String newCode = _getTotp(); + if (newCode != _currentCode.value) { + _currentCode.value = newCode; + _nextCode.value = _getNextTotp(); + } }); } - void _updateTimeRemaining() { - _timeRemaining = - widget.code.period - (DateTime.now().second % widget.code.period); - } - @override void dispose() { _everySecondTimer?.cancel(); + _currentCode.dispose(); + _nextCode.dispose(); super.dispose(); } @@ -107,14 +108,8 @@ class _CodeWidgetState extends State { crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [ - FAProgressBar( - currentValue: - _timeRemaining / widget.code.period * 100, - size: 4, - animatedDuration: const Duration(milliseconds: 200), - progressColor: Colors.orange, - changeColorValue: 40, - changeProgressColor: Colors.green, + CodeTimerProgress( + period: widget.code.period, ), const SizedBox( height: 16, @@ -167,9 +162,14 @@ class _CodeWidgetState extends State { crossAxisAlignment: CrossAxisAlignment.end, children: [ Expanded( - child: Text( - _getTotp(), - style: const TextStyle(fontSize: 24), + child: ValueListenableBuilder( + valueListenable: _currentCode, + builder: (context, value, child) { + return Text( + value, + style: const TextStyle(fontSize: 24), + ); + }, ), ), Column( @@ -179,12 +179,17 @@ class _CodeWidgetState extends State { l10n.nextTotpTitle, style: Theme.of(context).textTheme.caption, ), - Text( - _getNextTotp(), - style: const TextStyle( - fontSize: 18, - color: Colors.grey, - ), + ValueListenableBuilder( + valueListenable: _nextCode, + builder: (context, value, child) { + return Text( + value, + style: const TextStyle( + fontSize: 18, + color: Colors.grey, + ), + ); + }, ), ], ), From d7d61f26997cb37f07f453fe43290548dee1512b Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Sat, 11 Feb 2023 16:53:07 +0530 Subject: [PATCH 2/2] bump version to 1.0.30+30 --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index c0a6e31e88..35d89c94d0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: ente_auth description: ente two-factor authenticator -version: 1.0.29+29 +version: 1.0.30+30 publish_to: none environment: