Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[auth] Reduce refresh rate for progress bar to lower CPU usage

Neeraj Guptabf9d0e3d6b72
diff --git a/auth/lib/ui/code_timer_progress.dart b/auth/lib/ui/code_timer_progress.dart
index e594e15fb1..2b29be0403 100644
--- a/auth/lib/ui/code_timer_progress.dart
+++ b/auth/lib/ui/code_timer_progress.dart
@@ -1,58 +1,59 @@
-import 'package:ente_auth/services/preference_service.dart';
+import 'dart:async';
+import 'dart:io';
+
import 'package:ente_auth/theme/ente_theme.dart';
import 'package:flutter/material.dart';
-import 'package:flutter/scheduler.dart';
-
-class CodeTimerProgressCache {
- static final Map<int, CodeTimerProgress> _cache = {};
-
- static CodeTimerProgress getCachedWidget(int period) {
- if (!_cache.containsKey(period)) {
- _cache[period] = CodeTimerProgress(period: period);
- }
- return _cache[period]!;
- }
-}
+import 'package:flutter_launcher_icons/logger.dart';
class CodeTimerProgress extends StatefulWidget {
final int period;
-
+ final bool isCompactMode;
const CodeTimerProgress({
super.key,
required this.period,
+ this.isCompactMode = false,
});
@override
State<CodeTimerProgress> createState() => _CodeTimerProgressState();
}
-class _CodeTimerProgressState extends State<CodeTimerProgress>
- with SingleTickerProviderStateMixin {
- late final Ticker _ticker;
+class _CodeTimerProgressState extends State<CodeTimerProgress> {
+ late final Timer _timer;
late final ValueNotifier<double> _progress;
- late final int _microSecondsInPeriod;
- late bool _isCompactMode=false;
+ late final int _periodInMicros;
+
+ // Cache the start time to avoid repeated system calls
+ late final int _startMicros;
+
+ // Reduce update frequency
+ final int _updateIntervalMs =
+ (Platform.isAndroid || Platform.isIOS) ? 16 : 500; // approximately 60 FPS
@override
void initState() {
super.initState();
- _microSecondsInPeriod = widget.period * 1000000;
+ _periodInMicros = widget.period * 1000000;
_progress = ValueNotifier<double>(0.0);
- _ticker = createTicker(_updateTimeRemaining);
- _ticker.start();
- _isCompactMode = PreferenceService.instance.isCompactMode();
- _updateTimeRemaining(Duration.zero);
+ _startMicros = DateTime.now().microsecondsSinceEpoch;
+
+ // Use a Timer instead of a Ticker
+ _timer = Timer.periodic(Duration(milliseconds: _updateIntervalMs), (timer) {
+ final now = DateTime.now().microsecondsSinceEpoch;
+ _updateTimeRemaining(now);
+ });
}
- void _updateTimeRemaining(Duration elapsed) {
- int timeRemaining = _microSecondsInPeriod -
- (DateTime.now().microsecondsSinceEpoch % _microSecondsInPeriod);
- _progress.value = timeRemaining / _microSecondsInPeriod;
+ void _updateTimeRemaining(int currentMicros) {
+ // More efficient time calculation using modulo
+ final elapsed = (currentMicros - _startMicros) % _periodInMicros;
+ final timeRemaining = _periodInMicros - elapsed;
+ _progress.value = timeRemaining / _periodInMicros;
}
@override
void dispose() {
- _ticker.dispose();
+ _timer.cancel();
_progress.dispose();
super.dispose();
}
@@ -60,18 +61,19 @@ class _CodeTimerProgressState extends State<CodeTimerProgress>
@override
Widget build(BuildContext context) {
return SizedBox(
- height: _isCompactMode ?1:3,
+ height: widget.isCompactMode ? 1 : 3,
child: ValueListenableBuilder<double>(
valueListenable: _progress,
builder: (context, progress, _) {
return CustomPaint(
+ key: Key(progress.toString()), // Add key here
painter: _ProgressPainter(
progress: progress,
color: progress > 0.4
? getEnteColorScheme(context).primary700
: Colors.orange,
),
- size: Size.infinite,
+ size: const Size.fromHeight(double.infinity),
);
},
),
@@ -83,7 +85,10 @@ class _ProgressPainter extends CustomPainter {
final double progress;
final Color color;
- _ProgressPainter({required this.progress, required this.color});
+ const _ProgressPainter({
+ required this.progress,
+ required this.color,
+ });
@override
void paint(Canvas canvas, Size size) {
diff --git a/auth/lib/ui/code_widget.dart b/auth/lib/ui/code_widget.dart
index c04feddfb0..509060f3cd 100644
--- a/auth/lib/ui/code_widget.dart
+++ b/auth/lib/ui/code_widget.dart
@@ -146,8 +146,10 @@ class _CodeWidgetState extends State<CodeWidget> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (widget.code.type.isTOTPCompatible)
- CodeTimerProgressCache.getCachedWidget(
- widget.code.period,
+ CodeTimerProgress(
+ key: ValueKey('period_${widget.code.period}'),
+ period: widget.code.period,
+ isCompactMode: widget.isCompactMode,
),
widget.isCompactMode
? const SizedBox(height: 4)
diff --git a/auth/pubspec.lock b/auth/pubspec.lock
index a19f886e22..0568bf396d 100644
--- a/auth/pubspec.lock
+++ b/auth/pubspec.lock
@@ -94,14 +94,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.11.0"
- attributed_text:
- dependency: transitive
- description:
- name: attributed_text
- sha256: fb65cf441784612544eda4d5df7a3caad56e7f673c68bf1d48d9048228375189
- url: "https://pub.dev"
- source: hosted
- version: "0.4.0"
auto_size_text:
dependency: "direct main"
description:
@@ -294,14 +286,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.1.1"
- coverage:
- dependency: transitive
- description:
- name: coverage
- sha256: e3493833ea012784c740e341952298f1cc77f1f01b1bbc3eb4eecf6984fb7f43
- url: "https://pub.dev"
- source: hosted
- version: "1.11.1"
cross_file:
dependency: transitive
description:
@@ -1093,14 +1077,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.5.0"
- node_preamble:
- dependency: transitive
- description:
- name: node_preamble
- sha256: "6e7eac89047ab8a8d26cf16127b5ed26de65209847630400f9aefd7cd5c730db"
- url: "https://pub.dev"
- source: hosted
- version: "2.0.2"
otp:
dependency: "direct main"
description:
@@ -1429,22 +1405,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.4.1"
- shelf_packages_handler:
- dependency: transitive
- description:
- name: shelf_packages_handler
- sha256: "89f967eca29607c933ba9571d838be31d67f53f6e4ee15147d5dc2934fee1b1e"
- url: "https://pub.dev"
- source: hosted
- version: "3.0.2"
- shelf_static:
- dependency: transitive
- description:
- name: shelf_static
- sha256: c87c3875f91262785dade62d135760c2c69cb217ac759485334c5857ad89f6e3
- url: "https://pub.dev"
- source: hosted
- version: "1.1.3"
shelf_web_socket:
dependency: transitive
description:
@@ -1498,22 +1458,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.4"
- source_map_stack_trace:
- dependency: transitive
- description:
- name: source_map_stack_trace
- sha256: c0713a43e323c3302c2abe2a1cc89aa057a387101ebd280371d6a6c9fa68516b
- url: "https://pub.dev"
- source: hosted
- version: "2.1.2"
- source_maps:
- dependency: transitive
- description:
- name: source_maps
- sha256: "190222579a448b03896e0ca6eca5998fa810fda630c1d65e2f78b3f638f54812"
- url: "https://pub.dev"
- source: hosted
- version: "0.10.13"
source_span:
dependency: transitive
description:
@@ -1627,14 +1571,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "8.1.0"
- super_text_layout:
- dependency: "direct main"
- description:
- name: super_text_layout
- sha256: "0c85c295164edaab175c477584f2b7abc295dfd2a7f81987a88e538cf75b7898"
- url: "https://pub.dev"
- source: hosted
- version: "0.1.17"
synchronized:
dependency: transitive
description:
@@ -1651,14 +1587,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.1"
- test:
- dependency: transitive
- description:
- name: test
- sha256: "7ee44229615f8f642b68120165ae4c2a75fe77ae2065b1e55ae4711f6cf0899e"
- url: "https://pub.dev"
- source: hosted
- version: "1.25.7"
test_api:
dependency: transitive
description:
@@ -1667,14 +1595,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.7.2"
- test_core:
- dependency: transitive
- description:
- name: test_core
- sha256: "55ea5a652e38a1dfb32943a7973f3681a60f872f8c3a05a14664ad54ef9c6696"
- url: "https://pub.dev"
- source: hosted
- version: "0.6.4"
timezone:
dependency: transitive
description:
@@ -1875,14 +1795,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.1"
- webkit_inspection_protocol:
- dependency: transitive
- description:
- name: webkit_inspection_protocol
- sha256: "87d3f2333bb240704cd3f1c6b5b7acd8a10e7f0bc28c28dcf14e782014f4a572"
- url: "https://pub.dev"
- source: hosted
- version: "1.2.1"
win32:
dependency: "direct main"
description:
diff --git a/auth/pubspec.yaml b/auth/pubspec.yaml
index 3356410238..6d2a932c40 100644
--- a/auth/pubspec.yaml
+++ b/auth/pubspec.yaml
@@ -86,7 +86,6 @@ dependencies:
sentry_flutter: ^8.7.0
share_plus: ^10.0.2
shared_preferences: ^2.0.5
- super_text_layout: ^0.1.17
sqflite:
git:
url: https://github.com/tekartik/sqflite