Skip to content

Commit

Permalink
Extract exam provider updates
Browse files Browse the repository at this point in the history
Co-authored-by: Rubem-Viscard <[email protected]>"
  • Loading branch information
bdmendes committed Dec 8, 2023
1 parent 039eb19 commit 49e8ee8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 42 deletions.
52 changes: 18 additions & 34 deletions uni/lib/view/exams/widgets/exam_filter_form.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
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});
final Map<String, bool> filteredExamsTypes;

@override
ExamFilterFormState createState() => ExamFilterFormState();
}
class ExamFilterForm extends StatelessWidget {
const ExamFilterForm(
this.filteredExamsTypes,
this.setExamsTypesCallback, {
super.key,
});

class ExamFilterFormState extends State<ExamFilterForm> {
void _changeFilteredExamList(String key, {bool? value}) {
setState(() {
widget.filteredExamsTypes[key] = value!;
Provider.of<ExamProvider>(context, listen: false)
.setFilteredExams(widget.filteredExamsTypes);
});
}
final Map<String, bool> filteredExamsTypes;
final void Function(Map<String, bool>) setExamsTypesCallback;

@override
Widget build(BuildContext context) {
Expand All @@ -38,25 +28,16 @@ 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: FilteredExamList(
UnmodifiableMapView(
Map<String, bool>.from(widget.filteredExamsTypes)
Map<String, bool>.from(filteredExamsTypes)
..removeWhere((key, value) => !Exam.types.containsKey(key)),
),
_changeFilteredExamList,
context,
setExamsTypesCallback,
),
),
);
Expand All @@ -66,13 +47,12 @@ class ExamFilterFormState extends State<ExamFilterForm> {
class FilteredExamList extends StatelessWidget {
const FilteredExamList(
this.filteredExams,
this.changeFilteredExamList,
this.context, {
this.changeFilteredExamList, {
super.key,
});

final UnmodifiableMapView<String, bool> filteredExams;
final void Function(String, {bool? value}) changeFilteredExamList;
final BuildContext context;
final void Function(Map<String, bool>) changeFilteredExamList;

@override
Widget build(BuildContext context) {
Expand All @@ -90,7 +70,11 @@ class FilteredExamList extends StatelessWidget {
),
key: Key('ExamCheck$key'),
value: filteredExams[key],
onChanged: (value) => {changeFilteredExamList(key, value: value)},
onChanged: (value) {
final newFilteredExams = Map<String, bool>.from(filteredExams);
newFilteredExams[key] = value!;
changeFilteredExamList(newFilteredExams);
},
);
}),
);
Expand Down
17 changes: 9 additions & 8 deletions uni/lib/view/exams/widgets/exam_filter_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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 +20,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

0 comments on commit 49e8ee8

Please sign in to comment.