Skip to content

Commit

Permalink
Add and implement: 'when choosing and then deleting a custom lesson c…
Browse files Browse the repository at this point in the history
…hip then the date will stay the same when selecting another course'
  • Loading branch information
Jonas-Sander committed Dec 6, 2023
1 parent cc9107a commit 3b0250c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
27 changes: 23 additions & 4 deletions app/lib/homework/homework_dialog/homework_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class HwDialogKeys {
Key("custom-lesson-chip-dialog-text-field");
static const Key customLessonChipDialogOkButton =
Key("custom-lesson-chip-dialog-ok-button");
static const Key lessonChipDeleteIcon = Key("lesson-chip-delete-icon");
static const Key submissionTile = Key("submission-tile");
static const Key submissionTimeTile = Key("submission-time-tile");
static const Key descriptionField = Key("description-field");
Expand Down Expand Up @@ -433,7 +434,7 @@ class _DueDateChip {
}

class _DueDateChipsController extends ChangeNotifier {
final void Function(DueDateSelection) onChanged;
final void Function(DueDateSelection?) onChanged;
IList<_DueDateChip> chips = IList();

_DueDateChipsController({
Expand Down Expand Up @@ -506,7 +507,11 @@ class _DueDateChipsController extends ChangeNotifier {
}

void deleteInXLessonsChip(InXLessonsDueDateSelection inXLessons) {
final old = chips;
chips = chips.removeWhere((chip) => chip.dueDate == inXLessons);
if (old != chips) {
onChanged(null);
}
notifyListeners();
}
}
Expand Down Expand Up @@ -539,12 +544,22 @@ class _DueDateChipsState extends State<_DueDateChips> {
@override
void initState() {
super.initState();
final bloc =
bloc_lib.BlocProvider.of<HomeworkDialogBloc>(context, listen: false);
controller = _DueDateChipsController(
initialChips: widget.initialChips,
onChanged: (selection) {
final bloc = bloc_lib.BlocProvider.of<HomeworkDialogBloc>(context,
listen: false);
bloc.add(DueDateChanged(selection));
if (selection != null) {
bloc.add(DueDateChanged(selection));
return;
}
// If a selected chip was deleted then selection will be null and we
// change to manual date selection with the previous selected date.
final state = bloc.state;
if (state is Ready && state.dueDate.$1 != null) {
final oldDate = state.dueDate.$1!;
bloc.add(DueDateChanged(DueDateSelection.date(oldDate)));
}
},
);
}
Expand Down Expand Up @@ -586,6 +601,10 @@ class _DueDateChipsState extends State<_DueDateChips> {
child: InputChip(
label: Text(chip.label),
selected: chip.isSelected,
deleteIcon: const Icon(
Icons.clear,
key: HwDialogKeys.lessonChipDeleteIcon,
),
onSelected: chip.dueDate
is! InXLessonsDueDateSelection ||
lessonChipsSelectable
Expand Down
29 changes: 29 additions & 0 deletions app/test/homework/homework_dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,29 @@ void main() {
expect(controller.getSelectedLessonChips(), ['Übernächste Stunde']);
expect(controller.getSelectedDueDate(), Date('2023-10-12'));
});
testWidgets(
'when choosing and then deleting a custom lesson chip then the date will stay the same when selecting another course',
(tester) async {
final controller = createController(tester);
controller.addCourse(courseWith(id: 'foo_course', name: 'Foo course'));
controller.addNextLessonDates('foo_course',
[Date('2023-10-06'), Date('2023-10-08'), Date('2023-10-12')]);
controller.addCourse(courseWith(id: 'bar_course', name: 'Bar course'));
controller.addNextLessonDates('bar_course',
[Date('2023-10-08'), Date('2023-10-12'), Date('2023-10-14')]);
await pumpAndSettleHomeworkDialog(tester,
showDueDateSelectionChips: true);

await controller.selectCourse('foo_course');
await controller.createCustomChip(inXLessons: 3);
await controller.deleteCustomChip(inXLessons: 3);
await controller.selectCourse('bar_course');

expect(controller.getSelectedLessonChips(), []);
// If we in 3 lessons selection would still exist then the date would be
// 2023-10-14
expect(controller.getSelectedDueDate(), Date('2023-10-12'));
});
});
}

Expand Down Expand Up @@ -974,6 +997,12 @@ class _TestController {
.selectDate!(date.toDateTime);
await tester.pumpAndSettle();
}

Future<void> deleteCustomChip({required int inXLessons}) async {
// For now only works if only one custom chip exists.
await tester.tap(find.byKey(HwDialogKeys.lessonChipDeleteIcon));
await tester.pumpAndSettle();
}
}

// Used temporarily when testing so one can see what happens "on the screen" in
Expand Down

0 comments on commit 3b0250c

Please sign in to comment.