From e7768db444bb989d2654dbb04be14903ffd9be57 Mon Sep 17 00:00:00 2001 From: Parth Baraiya Date: Wed, 13 Dec 2023 15:57:18 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Improve=20the=20LiveTimeIndicator?= =?UTF-8?q?=20to=20set=20the=20state=20only=20when=20current=20hour=20and?= =?UTF-8?q?=20minutes=20changes.=20(#292)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/components/_internal_components.dart | 18 ++++++++---------- lib/src/extensions.dart | 4 ++++ pubspec.yaml | 1 - test/extensions_test.dart | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/src/components/_internal_components.dart b/lib/src/components/_internal_components.dart index 7efbb370..b4d63695 100644 --- a/lib/src/components/_internal_components.dart +++ b/lib/src/components/_internal_components.dart @@ -51,14 +51,13 @@ class LiveTimeIndicator extends StatefulWidget { class _LiveTimeIndicatorState extends State { late Timer _timer; - late DateTime _currentDate; + late TimeOfDay _currentTime = TimeOfDay.now(); @override void initState() { super.initState(); - _currentDate = DateTime.now(); - _timer = Timer(Duration(seconds: 1), setTimer); + _timer = Timer.periodic(Duration(seconds: 1), _onTick); } @override @@ -70,12 +69,11 @@ class _LiveTimeIndicatorState extends State { /// Creates an recursive call that runs every 1 seconds. /// This will rebuild TimeLineIndicator every second. This will allow us /// to indicate live time in Week and Day view. - void setTimer() { - if (mounted) { - setState(() { - _currentDate = DateTime.now(); - _timer = Timer(Duration(seconds: 1), setTimer); - }); + void _onTick(Timer? timer) { + final time = TimeOfDay.now(); + if (time != _currentTime && mounted) { + _currentTime = time; + setState(() {}); } } @@ -88,7 +86,7 @@ class _LiveTimeIndicatorState extends State { height: widget.liveTimeIndicatorSettings.height, offset: Offset( widget.timeLineWidth + widget.liveTimeIndicatorSettings.offset, - _currentDate.getTotalMinutes * widget.heightPerMinute, + _currentTime.getTotalMinutes * widget.heightPerMinute, ), ), ); diff --git a/lib/src/extensions.dart b/lib/src/extensions.dart index c852bbbe..6761e2bb 100644 --- a/lib/src/extensions.dart +++ b/lib/src/extensions.dart @@ -184,3 +184,7 @@ extension MyList on List { } } } + +extension TimerOfDayExtension on TimeOfDay { + int get getTotalMinutes => hour * 60 + minute; +} diff --git a/pubspec.yaml b/pubspec.yaml index 87539ae1..3a680b48 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,6 +15,5 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - test: ^1.22.0 flutter: diff --git a/test/extensions_test.dart b/test/extensions_test.dart index 0d478d84..61f23cc4 100644 --- a/test/extensions_test.dart +++ b/test/extensions_test.dart @@ -1,5 +1,5 @@ import 'package:calendar_view/calendar_view.dart'; -import 'package:test/test.dart'; +import 'package:flutter_test/flutter_test.dart'; void main() { group('DateTimeExtensions', () {