Skip to content

Commit

Permalink
use TimeOfDay extension to add isAfter and isBefore instead of creati…
Browse files Browse the repository at this point in the history
…ng a function for each
  • Loading branch information
user5522 committed Jun 3, 2024
1 parent b5856c0 commit 74fd8da
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
5 changes: 3 additions & 2 deletions lib/components/settings/screens/timetable_period_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -108,7 +109,7 @@ class TimetablePeriodScreen extends ConsumerWidget {
return;
}

if (isAfter(selectedTime, customEndTime)) {
if (selectedTime.isAfter(customEndTime)) {
switchStartWithEndTime(selectedTime);
return;
}
Expand All @@ -134,7 +135,7 @@ class TimetablePeriodScreen extends ConsumerWidget {
return;
}

if (isBefore(selectedTime, customStartTime)) {
if (selectedTime.isBefore(customStartTime)) {
switchEndWithStartTime(selectedTime);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -128,7 +129,7 @@ class TimeConfig extends ConsumerWidget {

if (selectedTime == null) return;

if (isBefore(selectedTime, customStartTime)) {
if (selectedTime.isBefore(customStartTime)) {
showInvalidTimePeriodDialog();
return;
}
Expand All @@ -138,7 +139,7 @@ class TimeConfig extends ConsumerWidget {
return;
}

if (isAfter(selectedTime, endTime.value)) {
if (selectedTime.isAfter(endTime.value)) {
switchStartWithEndTime(selectedTime);
return;
}
Expand All @@ -165,7 +166,7 @@ class TimeConfig extends ConsumerWidget {

if (selectedTime == null) return;

if (isAfter(selectedTime, customEndTime)) {
if (selectedTime.isAfter(customEndTime)) {
showInvalidTimePeriodDialog();
return;
}
Expand All @@ -174,7 +175,7 @@ class TimeConfig extends ConsumerWidget {
showInvalidEqualTimeDialog();
return;
}
if (isBefore(selectedTime, startTime.value)) {
if (selectedTime.isBefore(startTime.value)) {
switchEndWithStartTime(selectedTime);
return;
}
Expand Down
23 changes: 23 additions & 0 deletions lib/extensions/time_of_day.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
12 changes: 0 additions & 12 deletions lib/helpers/time_management.dart
Original file line number Diff line number Diff line change
@@ -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]
Expand Down

0 comments on commit 74fd8da

Please sign in to comment.