From cfe1ababc74782b8a9f5e9a28b2a68c3451f6489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Ma=C3=B1osa?= Date: Sat, 18 Sep 2021 17:11:58 +0200 Subject: [PATCH] refactor(time_of_day_extension): improve comparison implementation --- lib/utils/time_of_day_extension.dart | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/utils/time_of_day_extension.dart b/lib/utils/time_of_day_extension.dart index 9d8b9e8e..7d92f8f7 100644 --- a/lib/utils/time_of_day_extension.dart +++ b/lib/utils/time_of_day_extension.dart @@ -22,13 +22,18 @@ extension TimeOfDayExtension on TimeOfDay { static int compare(TimeOfDay a, TimeOfDay b) => a.compareTo(b); + int get inMinutes => hour * TimeOfDay.minutesPerHour + minute; + + int compareTo(TimeOfDay? other) { + if (other == null) return 1; + if (other == this) return 0; + return inMinutes.compareTo(other.inMinutes); + } + Duration durationBetween(TimeOfDay other) => Duration( hours: other.hour - hour, minutes: other.minute - minute, ); String format24Hour() => '${hour.padLeft2}:${minute.padLeft2}'; - - int compareTo(TimeOfDay other) => - (hour - other.hour) * 60 + minute - other.minute; }