From 49e8ee80d388eb50337be842727f653cc045dba0 Mon Sep 17 00:00:00 2001 From: Bruno Mendes Date: Fri, 8 Dec 2023 15:12:01 +0000 Subject: [PATCH] Extract exam provider updates Co-authored-by: Rubem-Viscard " --- .../view/exams/widgets/exam_filter_form.dart | 52 +++++++------------ .../view/exams/widgets/exam_filter_menu.dart | 17 +++--- 2 files changed, 27 insertions(+), 42 deletions(-) diff --git a/uni/lib/view/exams/widgets/exam_filter_form.dart b/uni/lib/view/exams/widgets/exam_filter_form.dart index e38ca8821..a93a459b6 100644 --- a/uni/lib/view/exams/widgets/exam_filter_form.dart +++ b/uni/lib/view/exams/widgets/exam_filter_form.dart @@ -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 filteredExamsTypes; - @override - ExamFilterFormState createState() => ExamFilterFormState(); -} +class ExamFilterForm extends StatelessWidget { + const ExamFilterForm( + this.filteredExamsTypes, + this.setExamsTypesCallback, { + super.key, + }); -class ExamFilterFormState extends State { - void _changeFilteredExamList(String key, {bool? value}) { - setState(() { - widget.filteredExamsTypes[key] = value!; - Provider.of(context, listen: false) - .setFilteredExams(widget.filteredExamsTypes); - }); - } + final Map filteredExamsTypes; + final void Function(Map) setExamsTypesCallback; @override Widget build(BuildContext context) { @@ -38,25 +28,16 @@ class ExamFilterFormState extends State { ), onPressed: () => Navigator.pop(context), ), - ElevatedButton( - child: Text(S.of(context).confirm), - onPressed: () { - Provider.of(context, listen: false) - .setFilteredExams(widget.filteredExamsTypes); - Navigator.pop(context); - }, - ), ], content: SizedBox( height: 230, width: 200, child: FilteredExamList( UnmodifiableMapView( - Map.from(widget.filteredExamsTypes) + Map.from(filteredExamsTypes) ..removeWhere((key, value) => !Exam.types.containsKey(key)), ), - _changeFilteredExamList, - context, + setExamsTypesCallback, ), ), ); @@ -66,13 +47,12 @@ class ExamFilterFormState extends State { class FilteredExamList extends StatelessWidget { const FilteredExamList( this.filteredExams, - this.changeFilteredExamList, - this.context, { + this.changeFilteredExamList, { super.key, }); + final UnmodifiableMapView filteredExams; - final void Function(String, {bool? value}) changeFilteredExamList; - final BuildContext context; + final void Function(Map) changeFilteredExamList; @override Widget build(BuildContext context) { @@ -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.from(filteredExams); + newFilteredExams[key] = value!; + changeFilteredExamList(newFilteredExams); + }, ); }), ); diff --git a/uni/lib/view/exams/widgets/exam_filter_menu.dart b/uni/lib/view/exams/widgets/exam_filter_menu.dart index 1ea516678..c66fd6704 100644 --- a/uni/lib/view/exams/widgets/exam_filter_menu.dart +++ b/uni/lib/view/exams/widgets/exam_filter_menu.dart @@ -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}); @@ -19,14 +20,14 @@ class ExamFilterMenuState extends State { showDialog( context: context, builder: (_) { - final examProvider = - Provider.of(context, listen: false); - final filteredExamsTypes = examProvider.filteredExamsTypes; - return ChangeNotifierProvider.value( - value: examProvider, - child: ExamFilterForm( - Map.from(filteredExamsTypes), - ), + return LazyConsumer( + builder: (context, examProvider) { + return ExamFilterForm( + Map.from(examProvider.filteredExamsTypes), + (newFilteredExams) { + examProvider.setFilteredExams(newFilteredExams); + }); + }, ); }, );