diff --git a/lib/components/settings/screens/timetable_period_screen.dart b/lib/components/settings/screens/timetable_period_screen.dart index b949a93..4db3664 100644 --- a/lib/components/settings/screens/timetable_period_screen.dart +++ b/lib/components/settings/screens/timetable_period_screen.dart @@ -2,6 +2,7 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:timetable/constants/custom_times.dart'; +import 'package:timetable/extensions/time_of_day.dart'; import 'package:timetable/helpers/time_management.dart'; import 'package:timetable/provider/settings.dart'; @@ -108,7 +109,7 @@ class TimetablePeriodScreen extends ConsumerWidget { return; } - if (isAfter(selectedTime, customEndTime)) { + if (selectedTime.isAfter(customEndTime)) { switchStartWithEndTime(selectedTime); return; } @@ -134,7 +135,7 @@ class TimetablePeriodScreen extends ConsumerWidget { return; } - if (isBefore(selectedTime, customStartTime)) { + if (selectedTime.isBefore(customStartTime)) { switchEndWithStartTime(selectedTime); return; } diff --git a/lib/components/subject_management/subject_configs/time_config.dart b/lib/components/subject_management/subject_configs/time_config.dart index 2f54ed8..a866b19 100644 --- a/lib/components/subject_management/subject_configs/time_config.dart +++ b/lib/components/subject_management/subject_configs/time_config.dart @@ -2,6 +2,7 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:timetable/components/widgets/act_chip.dart'; +import 'package:timetable/extensions/time_of_day.dart'; import 'package:timetable/helpers/time_management.dart'; import 'package:timetable/constants/custom_times.dart'; import 'package:timetable/provider/settings.dart'; @@ -73,7 +74,7 @@ class TimeConfig extends ConsumerWidget { } void switchStartWithEndTime(TimeOfDay newTime) { - if (!isAfter(newTime, customEndTime)) { + if (!newTime.isAfter(customEndTime)) { final temp = endTime.value; endTime.value = newTime; startTime.value = temp; @@ -83,7 +84,7 @@ class TimeConfig extends ConsumerWidget { } void switchEndWithStartTime(TimeOfDay newTime) { - if (!isBefore(newTime, customStartTime)) { + if (!newTime.isBefore(customStartTime)) { final temp = startTime.value; startTime.value = newTime; endTime.value = temp; @@ -128,7 +129,7 @@ class TimeConfig extends ConsumerWidget { if (selectedTime == null) return; - if (isBefore(selectedTime, customStartTime)) { + if (selectedTime.isBefore(customStartTime)) { showInvalidTimePeriodDialog(); return; } @@ -138,7 +139,7 @@ class TimeConfig extends ConsumerWidget { return; } - if (isAfter(selectedTime, endTime.value)) { + if (selectedTime.isAfter(endTime.value)) { switchStartWithEndTime(selectedTime); return; } @@ -165,7 +166,7 @@ class TimeConfig extends ConsumerWidget { if (selectedTime == null) return; - if (isAfter(selectedTime, customEndTime)) { + if (selectedTime.isAfter(customEndTime)) { showInvalidTimePeriodDialog(); return; } @@ -174,7 +175,7 @@ class TimeConfig extends ConsumerWidget { showInvalidEqualTimeDialog(); return; } - if (isBefore(selectedTime, startTime.value)) { + if (selectedTime.isBefore(startTime.value)) { switchEndWithStartTime(selectedTime); return; } diff --git a/lib/extensions/time_of_day.dart b/lib/extensions/time_of_day.dart new file mode 100644 index 0000000..1095cae --- /dev/null +++ b/lib/extensions/time_of_day.dart @@ -0,0 +1,23 @@ +import 'package:flutter/material.dart'; + +extension TimeOfDayExtension on TimeOfDay { + bool isBefore(TimeOfDay other) { + if (hour < other.hour) { + return true; + } else if (hour == other.hour) { + return minute < other.minute; + } else { + return false; + } + } + + bool isAfter(TimeOfDay other) { + if (hour > other.hour) { + return true; + } else if (hour == other.hour) { + return minute > other.minute; + } else { + return false; + } + } +} diff --git a/lib/helpers/time_management.dart b/lib/helpers/time_management.dart index 00ac4e6..01a90d0 100644 --- a/lib/helpers/time_management.dart +++ b/lib/helpers/time_management.dart @@ -1,17 +1,5 @@ import 'package:flutter/material.dart'; -/// check if time 1 is before time2 -bool isBefore(TimeOfDay time1, TimeOfDay time2) { - return (time1.hour < time2.hour) || - (time1.hour == time2.hour && time1.minute < time2.minute); -} - -/// check if time 1 is after time2 -bool isAfter(TimeOfDay time1, TimeOfDay time2) { - return (time1.hour > time2.hour) || - (time1.hour == time2.hour && time1.minute > time2.minute); -} - /// returns a time picker /// /// initialEntryMode is set by default to [TimePickerEntryMode.input]