Skip to content

Commit

Permalink
Only show time error message after pressing save.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas-Sander committed Feb 22, 2024
1 parent 7072545 commit 7c18b83
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ class AddEventDialogController extends ChangeNotifier {
notifyListeners();
}

bool showEndTimeNotAfterStartTimeError = false;
void maybeClearEndTimeNotAfterStartTimeError() {
if (endTime.isAfter(startTime)) {
showEndTimeNotAfterStartTimeError = false;
}
}

Time _startTime = Time(hour: 11, minute: 00);

Time get startTime => _startTime;

set startTime(Time value) {
_startTime = value;
maybeClearEndTimeNotAfterStartTimeError();
notifyListeners();
}

Expand All @@ -81,6 +89,7 @@ class AddEventDialogController extends ChangeNotifier {

set endTime(Time value) {
_endTime = value;
maybeClearEndTimeNotAfterStartTimeError();
notifyListeners();
}

Expand All @@ -94,6 +103,10 @@ class AddEventDialogController extends ChangeNotifier {
showEmptyCourseError = true;
hasError = true;
}
if (!endTime.isAfter(startTime)) {
showEndTimeNotAfterStartTimeError = true;
hasError = true;
}
if (hasError) {
notifyListeners();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class _DateAndTimePicker extends StatelessWidget {
timeFieldKey: EventDialogKeys.endTimeField,
time: controller.endTime,
showEndNotAfterBeginningError:
!controller.endTime.isAfter(controller.startTime),
controller.showEndTimeNotAfterStartTimeError,
isDatePickingEnabled: false,
onTimeChanged: (newTime) {
controller.endTime = newTime;
Expand Down
37 changes: 35 additions & 2 deletions app/test/timetable/timetable_dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,55 @@ void main() {
expect(find.text(EventDialogErrorStrings.emptyCourse), findsNothing);
});

testWidgets('shows error message if end time is not after start time',
testWidgets(
'shows "end time not after start time" error message when save was pressed',
(tester) async {
final dt = createDialogTester(tester);
await dt.pumpDialog(isExam: false);

await dt.selectStartTime(const TimeOfDay(hour: 13, minute: 15));
await dt.selectEndTime(const TimeOfDay(hour: 13, minute: 15));
await dt.tapSaveButton();
expect(find.text(EventDialogErrorStrings.endTimeMustBeAfterStartTime),
findsOneWidget);

await dt.tapSaveButton();
await dt.selectEndTime(const TimeOfDay(hour: 12, minute: 00));
expect(find.text(EventDialogErrorStrings.endTimeMustBeAfterStartTime),
findsOneWidget);
});
testWidgets(
'doesnt show "end time not after start time" error message when save was not pressed',
(tester) async {
final dt = createDialogTester(tester);
await dt.pumpDialog(isExam: false);

await dt.selectStartTime(const TimeOfDay(hour: 13, minute: 15));
await dt.selectEndTime(const TimeOfDay(hour: 13, minute: 15));
expect(find.text(EventDialogErrorStrings.endTimeMustBeAfterStartTime),
findsNothing);

await dt.selectEndTime(const TimeOfDay(hour: 12, minute: 00));
expect(find.text(EventDialogErrorStrings.endTimeMustBeAfterStartTime),
findsNothing);
});

testWidgets(
'removes "end time not after start time" error message when the error is fixed by the user',
(tester) async {
final dt = createDialogTester(tester);
await dt.pumpDialog(isExam: false);

testWidgets('doesnt shows error message if end time after start time',
await dt.selectStartTime(const TimeOfDay(hour: 13, minute: 15));
await dt.selectEndTime(const TimeOfDay(hour: 10, minute: 30));
await dt.tapSaveButton();
await dt.selectEndTime(const TimeOfDay(hour: 15, minute: 30));
expect(find.text(EventDialogErrorStrings.endTimeMustBeAfterStartTime),
findsNothing);
});

testWidgets(
'doesnt show "end time not after start time" error message when end time is after start time',
(tester) async {
final dt = createDialogTester(tester);
await dt.pumpDialog(isExam: false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ void main() {
theme: testConfig.theme.data,
);

// Set end time before start time to trigger the error message.
// Set end time before start time.
await dt.selectStartTime(const TimeOfDay(hour: 12, minute: 0));
await dt.selectEndTime(const TimeOfDay(hour: 10, minute: 0));

// Triggers empty title and no course chosen error messages.
// Triggers "empty title", "no course chosen" and "end time is before start time" error messages.
await dt.tapSaveButton();

await multiScreenGolden(
Expand Down

0 comments on commit 7c18b83

Please sign in to comment.