Skip to content

Commit

Permalink
✨ Improve the LiveTimeIndicator to set the state only when current ho…
Browse files Browse the repository at this point in the history
…ur and minutes changes.
  • Loading branch information
PRBaraiya committed Nov 22, 2023
1 parent 2ef1d21 commit b6a9507
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
18 changes: 8 additions & 10 deletions lib/src/components/_internal_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ class LiveTimeIndicator extends StatefulWidget {

class _LiveTimeIndicatorState extends State<LiveTimeIndicator> {
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
Expand All @@ -70,12 +69,11 @@ class _LiveTimeIndicatorState extends State<LiveTimeIndicator> {
/// 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(() {});
}
}

Expand All @@ -88,7 +86,7 @@ class _LiveTimeIndicatorState extends State<LiveTimeIndicator> {
height: widget.liveTimeIndicatorSettings.height,
offset: Offset(
widget.timeLineWidth + widget.liveTimeIndicatorSettings.offset,
_currentDate.getTotalMinutes * widget.heightPerMinute,
_currentTime.getTotalMinutes * widget.heightPerMinute,
),
),
);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,7 @@ extension MyList on List<CalendarEventData> {
}
}
}

extension TimerOfDayExtension on TimeOfDay {
int get getTotalMinutes => hour * 60 + minute;
}
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
test: ^1.22.0

flutter:
2 changes: 1 addition & 1 deletion test/extensions_test.dart
Original file line number Diff line number Diff line change
@@ -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', () {
Expand Down

0 comments on commit b6a9507

Please sign in to comment.