Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:fix: Improve the LiveTimeIndicator performance. #292

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading