From 740bbddf18150083e2be1a4079983c0698e165e3 Mon Sep 17 00:00:00 2001 From: Tamara Slosarek Date: Tue, 21 Nov 2023 17:54:18 +0100 Subject: [PATCH] feat(#668): add change notifier --- app/lib/common/models/userdata/userdata.dart | 21 ++++++++++ app/lib/common/pages/drug/cubit.dart | 22 ++++------ app/lib/common/pages/drug/drug.dart | 24 ++++++----- app/lib/drug_selection/pages/cubit.dart | 12 ++++-- .../drug_selection/pages/drug_selection.dart | 42 ++++++++++--------- app/lib/main.dart | 9 +++- app/pubspec.lock | 6 +-- app/pubspec.yaml | 1 + 8 files changed, 87 insertions(+), 50 deletions(-) diff --git a/app/lib/common/models/userdata/userdata.dart b/app/lib/common/models/userdata/userdata.dart index 34de155d1..25759266d 100644 --- a/app/lib/common/models/userdata/userdata.dart +++ b/app/lib/common/models/userdata/userdata.dart @@ -171,6 +171,27 @@ class UserData { } } +class ActiveDrugs extends ChangeNotifier { + List activeDrugs = []; + + Future _preserve() async { + UserData.instance.activeDrugNames = activeDrugs; + await UserData.save(); + } + + Future add(String drugName) async { + activeDrugs.add(drugName); + await _preserve(); + notifyListeners(); + } + + Future remove(String drugName) async { + activeDrugs = activeDrugs.filter((name) => name != drugName).toList(); + await _preserve(); + notifyListeners(); + } +} + /// Initializes the user's data by registering all necessary adapters and /// loading pre-existing data from local storage, if it exists. Future initUserData() async { diff --git a/app/lib/common/pages/drug/cubit.dart b/app/lib/common/pages/drug/cubit.dart index 64d5435ef..c223fe29a 100644 --- a/app/lib/common/pages/drug/cubit.dart +++ b/app/lib/common/pages/drug/cubit.dart @@ -9,13 +9,19 @@ import '../../utilities/pdf_utils.dart'; part 'cubit.freezed.dart'; class DrugCubit extends Cubit { - DrugCubit() : super(DrugState.loaded()); + DrugCubit(this.activeDrugs) : super(DrugState.loaded()); + + final ActiveDrugs activeDrugs; // ignore: avoid_positional_boolean_parameters Future setActivity(Drug drug, bool? value) async { if (value == null) return; emit(DrugState.loading()); - await setDrugActivity(drug, value); + if (value) { + await activeDrugs.add(drug.name); + } else { + await activeDrugs.remove(drug.name); + } emit(DrugState.loaded()); } @@ -32,18 +38,6 @@ class DrugCubit extends Cubit { } } -// ignore: avoid_positional_boolean_parameters -Future setDrugActivity(Drug drug, bool value) async { - final active = (UserData.instance.activeDrugNames ?? []) - .filter((name) => name != drug.name) - .toList(); - if (value) { - active.add(drug.name); - } - UserData.instance.activeDrugNames = active; - await UserData.save(); -} - @freezed class DrugState with _$DrugState { const factory DrugState.loading() = _LoadingState; diff --git a/app/lib/common/pages/drug/drug.dart b/app/lib/common/pages/drug/drug.dart index b4b46d17d..ec2ea2184 100644 --- a/app/lib/common/pages/drug/drug.dart +++ b/app/lib/common/pages/drug/drug.dart @@ -1,3 +1,5 @@ +import 'package:provider/provider.dart'; + import '../../module.dart'; import 'cubit.dart'; import 'widgets/module.dart'; @@ -13,16 +15,18 @@ class DrugPage extends StatelessWidget { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => cubit ?? DrugCubit(), - child: BlocBuilder( - builder: (context, state) { - return state.when( - loaded: () => _buildDrugsPage(context, loading: false), - loading: () => _buildDrugsPage(context, loading: true), - ); - }, - ), + return Consumer( + builder: (context, activeDrugs, child) => BlocProvider( + create: (context) => cubit ?? DrugCubit(activeDrugs), + child: BlocBuilder( + builder: (context, state) { + return state.when( + loaded: () => _buildDrugsPage(context, loading: false), + loading: () => _buildDrugsPage(context, loading: true), + ); + }, + ), + ) ); } diff --git a/app/lib/drug_selection/pages/cubit.dart b/app/lib/drug_selection/pages/cubit.dart index ae481640f..11ebc5ccb 100644 --- a/app/lib/drug_selection/pages/cubit.dart +++ b/app/lib/drug_selection/pages/cubit.dart @@ -1,18 +1,24 @@ import 'package:freezed_annotation/freezed_annotation.dart'; import '../../../common/module.dart'; -import '../../common/pages/drug/cubit.dart'; part 'cubit.freezed.dart'; class DrugSelectionPageCubit extends Cubit { - DrugSelectionPageCubit() : super(DrugSelectionPageState.stable()); + DrugSelectionPageCubit(this.activeDrugs) : + super(DrugSelectionPageState.stable()); + + final ActiveDrugs activeDrugs; // ignore: avoid_positional_boolean_parameters Future updateDrugActivity(Drug drug, bool? value) async { if (value == null) return; emit(DrugSelectionPageState.updating()); - await setDrugActivity(drug, value); + if (value) { + await activeDrugs.add(drug.name); + } else { + await activeDrugs.remove(drug.name); + } emit(DrugSelectionPageState.stable()); } } diff --git a/app/lib/drug_selection/pages/drug_selection.dart b/app/lib/drug_selection/pages/drug_selection.dart index 2f3bd91e2..a9f07182a 100644 --- a/app/lib/drug_selection/pages/drug_selection.dart +++ b/app/lib/drug_selection/pages/drug_selection.dart @@ -1,3 +1,5 @@ +import 'package:provider/provider.dart'; + import '../../common/models/drug/cached_drugs.dart'; import '../../common/module.dart' hide MetaData; import '../../common/widgets/drug_list/drug_items/drug_checkbox_list.dart'; @@ -15,25 +17,27 @@ class DrugSelectionPage extends HookWidget { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => cubit ?? DrugSelectionPageCubit(), - child: BlocBuilder( - builder: (context, state) { - return unscrollablePageScaffold( - title: context.l10n.drug_selection_header, - padding: PharMeTheme.largeSpace, - body: Column( - children: [ - _buildDescription(context), - SizedBox(height: PharMeTheme.mediumSpace), - Expanded(child:_buildDrugList(context, state)), - SizedBox(height: PharMeTheme.mediumSpace), - _buildButton(context, state), - ], - ), - ); - } - ), + return Consumer( + builder: (context, activeDrugs, child) => BlocProvider( + create: (context) => cubit ?? DrugSelectionPageCubit(activeDrugs), + child: BlocBuilder( + builder: (context, state) { + return unscrollablePageScaffold( + title: context.l10n.drug_selection_header, + padding: PharMeTheme.largeSpace, + body: Column( + children: [ + _buildDescription(context), + SizedBox(height: PharMeTheme.mediumSpace), + Expanded(child:_buildDrugList(context, state)), + SizedBox(height: PharMeTheme.mediumSpace), + _buildButton(context, state), + ], + ), + ); + } + ), + ) ); } diff --git a/app/lib/main.dart b/app/lib/main.dart index 15e286506..9a4ad8695 100644 --- a/app/lib/main.dart +++ b/app/lib/main.dart @@ -1,8 +1,15 @@ +import 'package:provider/provider.dart'; + import 'common/module.dart'; Future main() async { await initServices(); await fetchAndSaveLookups(); - runApp(PharMeApp()); + runApp( + ChangeNotifierProvider( + create: (context) => ActiveDrugs(), + child: PharMeApp(), + ), + ); await cleanupServices(); } diff --git a/app/pubspec.lock b/app/pubspec.lock index 06feadabd..dc2e1e73f 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -981,13 +981,13 @@ packages: source: hosted version: "4.2.4" provider: - dependency: transitive + dependency: "direct main" description: name: provider - sha256: e1e7413d70444ea3096815a60fe5da1b11bda8a9dc4769252cc82c53536f8bcc + sha256: "9a96a0a19b594dbc5bf0f1f27d2bc67d5f95957359b461cd9feb44ed6ae75096" url: "https://pub.dev" source: hosted - version: "6.0.4" + version: "6.1.1" pub_semver: dependency: transitive description: diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 8398d763b..5a988ce5a 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -40,6 +40,7 @@ dependencies: pdf: ^3.8.1 popover: ^0.2.8+1 printing: ^5.9.3 + provider: ^6.1.1 shared_preferences: ^2.0.15 url_launcher: ^6.1.4