Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/exams page #818

Closed
wants to merge 9 commits into from
57 changes: 31 additions & 26 deletions uni/lib/view/exams/widgets/exam_filter_form.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni/generated/l10n.dart';
import 'package:uni/model/entities/exam.dart';
import 'package:uni/model/providers/lazy/exam_provider.dart';

class ExamFilterForm extends StatefulWidget {
const ExamFilterForm(this.filteredExamsTypes, {super.key});
class ExamFilterForm extends StatelessWidget {
const ExamFilterForm(
this.filteredExamsTypes,
this.setExamsTypesCallback, {
super.key,
});

final Map<String, bool> filteredExamsTypes;
final void Function(Map<String, bool>) setExamsTypesCallback;

@override
ExamFilterFormState createState() => ExamFilterFormState();
}

class ExamFilterFormState extends State<ExamFilterForm> {
@override
Widget build(BuildContext context) {
return AlertDialog(
Expand All @@ -29,28 +28,34 @@ class ExamFilterFormState extends State<ExamFilterForm> {
),
onPressed: () => Navigator.pop(context),
),
ElevatedButton(
child: Text(S.of(context).confirm),
onPressed: () {
Provider.of<ExamProvider>(context, listen: false)
.setFilteredExams(widget.filteredExamsTypes);
Navigator.pop(context);
},
),
],
content: SizedBox(
height: 230,
width: 200,
child: getExamCheckboxes(widget.filteredExamsTypes, context),
child: FilteredExamList(
UnmodifiableMapView(
Map<String, bool>.from(filteredExamsTypes)
..removeWhere((key, value) => !Exam.types.containsKey(key)),
),
setExamsTypesCallback,
),
),
);
}
}

class FilteredExamList extends StatelessWidget {
const FilteredExamList(
this.filteredExams,
this.changeFilteredExamList, {
super.key,
});

Widget getExamCheckboxes(
Map<String, bool> filteredExams,
BuildContext context,
) {
filteredExams.removeWhere((key, value) => !Exam.types.containsKey(key));
final UnmodifiableMapView<String, bool> filteredExams;
final void Function(Map<String, bool>) changeFilteredExamList;

@override
Widget build(BuildContext context) {
return ListView(
children: List.generate(filteredExams.length, (i) {
final key = filteredExams.keys.elementAt(i);
Expand All @@ -66,9 +71,9 @@ class ExamFilterFormState extends State<ExamFilterForm> {
key: Key('ExamCheck$key'),
value: filteredExams[key],
onChanged: (value) {
setState(() {
filteredExams[key] = value!;
});
final newFilteredExams = Map<String, bool>.from(filteredExams);
newFilteredExams[key] = value!;
changeFilteredExamList(newFilteredExams);
},
);
}),
Expand Down
18 changes: 9 additions & 9 deletions uni/lib/view/exams/widgets/exam_filter_menu.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uni/model/providers/lazy/exam_provider.dart';
import 'package:uni/view/exams/widgets/exam_filter_form.dart';
import 'package:uni/view/lazy_consumer.dart';

class ExamFilterMenu extends StatefulWidget {
const ExamFilterMenu({super.key});
Expand All @@ -19,14 +19,14 @@ class ExamFilterMenuState extends State<ExamFilterMenu> {
showDialog<void>(
context: context,
builder: (_) {
final examProvider =
Provider.of<ExamProvider>(context, listen: false);
final filteredExamsTypes = examProvider.filteredExamsTypes;
return ChangeNotifierProvider.value(
value: examProvider,
child: ExamFilterForm(
Map<String, bool>.from(filteredExamsTypes),
),
return LazyConsumer<ExamProvider>(
builder: (context, examProvider) {
return ExamFilterForm(
Map<String, bool>.from(examProvider.filteredExamsTypes),
(newFilteredExams) {
examProvider.setFilteredExams(newFilteredExams);
});
},
);
},
);
Expand Down
5 changes: 0 additions & 5 deletions uni/test/integration/src/exams_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ void main() {
await tester.tap(find.byWidget(mtCheckboxTile));
await tester.pumpAndSettle();

final okButton = find.widgetWithText(ElevatedButton, 'Confirmar');
expect(okButton, findsOneWidget);

await tester.tap(okButton);

await tester.pumpAndSettle();

expect(find.byKey(Key('$sdisExam-exam')), findsNothing);
Expand Down
Loading