From abcff680b770281a9c2c2390275e0284c2706014 Mon Sep 17 00:00:00 2001 From: Tamara Slosarek Date: Wed, 22 Nov 2023 18:34:09 +0100 Subject: [PATCH] feat(#668): pass consumer to drug list filter --- app/lib/common/models/userdata/userdata.dart | 4 + app/lib/common/widgets/drug_list/builder.dart | 8 +- app/lib/common/widgets/drug_list/cubit.dart | 7 +- app/lib/common/widgets/drug_search.dart | 8 +- app/lib/report/pages/gene.dart | 126 +++++++++--------- 5 files changed, 85 insertions(+), 68 deletions(-) diff --git a/app/lib/common/models/userdata/userdata.dart b/app/lib/common/models/userdata/userdata.dart index e2a1f4a0..2f94ec68 100644 --- a/app/lib/common/models/userdata/userdata.dart +++ b/app/lib/common/models/userdata/userdata.dart @@ -203,6 +203,10 @@ class ActiveDrugs extends ChangeNotifier { await _remove(drugName); } } + + bool contains(String drugName) { + return names.contains(drugName); + } } /// Initializes the user's data by registering all necessary adapters and diff --git a/app/lib/common/widgets/drug_list/builder.dart b/app/lib/common/widgets/drug_list/builder.dart index 40d05f7f..5883d4da 100644 --- a/app/lib/common/widgets/drug_list/builder.dart +++ b/app/lib/common/widgets/drug_list/builder.dart @@ -4,6 +4,7 @@ import 'drug_items/drug_cards.dart'; List buildDrugList( BuildContext context, DrugListState state, + ActiveDrugs activeDrugs, { String? noDrugsMessage, List Function( @@ -18,8 +19,11 @@ List buildDrugList( Map? drugItemsBuildParams, } ) { - List buildDrugList(List drugs, FilterState filter) { - final filteredDrugs = filter.filter(drugs); + List buildDrugList( + List drugs, + FilterState filter, + ) { + final filteredDrugs = filter.filter(drugs, activeDrugs); if (filteredDrugs.isEmpty && noDrugsMessage != null) { return [errorIndicator(noDrugsMessage)]; } diff --git a/app/lib/common/widgets/drug_list/cubit.dart b/app/lib/common/widgets/drug_list/cubit.dart index 90e278b7..272a3ccd 100644 --- a/app/lib/common/widgets/drug_list/cubit.dart +++ b/app/lib/common/widgets/drug_list/cubit.dart @@ -122,7 +122,7 @@ class FilterState { final Map showWarningLevel; final String gene; - bool isAccepted(Drug drug) { + bool isAccepted(Drug drug, ActiveDrugs activeDrugs) { final userGuideline = drug.userGuideline(); final guidelineGenes = drug.guidelines.isNotEmpty ? drug.guidelines.first.lookupkey.keys.toList() : @@ -137,13 +137,14 @@ class FilterState { showWarningLevel[WarningLevel.green]!; } final isDrugAccepted = drug.matches(query: query) && - (drug.isActive() || showInactive) && + (activeDrugs.contains(drug.name) || showInactive) && warningLevelMatches && (gene.isBlank || (guidelineGenes.contains(gene))); return isDrugAccepted; } - List filter(List drugs) => drugs.filter(isAccepted).toList(); + List filter(List drugs, ActiveDrugs activeDrugs) => + drugs.filter((drug) => isAccepted(drug, activeDrugs)).toList(); } @freezed diff --git a/app/lib/common/widgets/drug_search.dart b/app/lib/common/widgets/drug_search.dart index 4a08fa73..77df82e4 100644 --- a/app/lib/common/widgets/drug_search.dart +++ b/app/lib/common/widgets/drug_search.dart @@ -1,5 +1,6 @@ import 'package:flutter/cupertino.dart'; +import 'package:provider/provider.dart'; import '../../../common/module.dart'; import '../../common/pages/drug/widgets/tooltip_icon.dart'; @@ -35,7 +36,8 @@ class DrugSearch extends HookWidget { ? context.l10n.search_no_drugs_with_filter_amendment : ''; final noDrugsMessage = context.l10n.search_no_drugs(amendment); - return BlocProvider( + return Consumer( + builder: (context, activeDrugs, child) => BlocProvider( create: (context) => cubit, child: BlocBuilder( builder: (context, state) { @@ -61,6 +63,7 @@ class DrugSearch extends HookWidget { buildDrugList( context, state, + activeDrugs, buildDrugItems: buildDrugItems, noDrugsMessage: noDrugsMessage, drugItemsBuildParams: drugItemsBuildParams, @@ -71,7 +74,8 @@ class DrugSearch extends HookWidget { ..._maybeShowDrugInteractionExplanation(context), ], ); - } + } + ) ) ); } diff --git a/app/lib/report/pages/gene.dart b/app/lib/report/pages/gene.dart index 562a81e1..fae05a03 100644 --- a/app/lib/report/pages/gene.dart +++ b/app/lib/report/pages/gene.dart @@ -1,3 +1,5 @@ +import 'package:provider/provider.dart'; + import '../../common/module.dart'; import '../../common/pages/drug/widgets/sub_header.dart'; import '../../common/pages/drug/widgets/tooltip_icon.dart'; @@ -13,70 +15,72 @@ class GenePage extends HookWidget { @override Widget build(BuildContext context) { - return BlocProvider( - create: (context) => cubit, - child: BlocBuilder( - builder: (context, state) => pageScaffold( - title: context.l10n.gene_page_headline(phenotype.geneSymbol), - body: [ - Padding( - padding: EdgeInsets.symmetric( - horizontal: PharMeTheme.smallToMediumSpace, - vertical: PharMeTheme.mediumSpace - ), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - SubHeader( - context.l10n.gene_page_your_variant(phenotype.geneSymbol), - tooltip: context.l10n - .gene_page_name_tooltip(phenotype.geneSymbol), - ), - SizedBox(height: PharMeTheme.smallToMediumSpace), - RoundedCard( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Table( - columnWidths: Map.from({ - 0: IntrinsicColumnWidth(), - 1: IntrinsicColumnWidth(flex: 1), - }), - children: [ - _buildRow( - context.l10n.gene_page_genotype, - phenotype.genotype, - tooltip: context.l10n.gene_page_genotype_tooltip - ), - _buildRow(context.l10n.gene_page_phenotype, - UserData.phenotypeFor( - phenotype.geneSymbol, - context, - ).phenotype, - tooltip: - context.l10n.gene_page_phenotype_tooltip - ), - ], - ), - if (inhibitableGenes.contains(phenotype.geneSymbol)) - ...buildDrugInteractionInfo( - context, - phenotype.geneSymbol, + return Consumer( + builder: (context, activeDrugs, child) => BlocProvider( + create: (context) => cubit, + child: BlocBuilder( + builder: (context, state) => pageScaffold( + title: context.l10n.gene_page_headline(phenotype.geneSymbol), + body: [ + Padding( + padding: EdgeInsets.symmetric( + horizontal: PharMeTheme.smallToMediumSpace, + vertical: PharMeTheme.mediumSpace + ), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + SubHeader( + context.l10n.gene_page_your_variant(phenotype.geneSymbol), + tooltip: context.l10n + .gene_page_name_tooltip(phenotype.geneSymbol), + ), + SizedBox(height: PharMeTheme.smallToMediumSpace), + RoundedCard( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Table( + columnWidths: Map.from({ + 0: IntrinsicColumnWidth(), + 1: IntrinsicColumnWidth(flex: 1), + }), + children: [ + _buildRow( + context.l10n.gene_page_genotype, + phenotype.genotype, + tooltip: context.l10n.gene_page_genotype_tooltip + ), + _buildRow(context.l10n.gene_page_phenotype, + UserData.phenotypeFor( + phenotype.geneSymbol, + context, + ).phenotype, + tooltip: + context.l10n.gene_page_phenotype_tooltip + ), + ], ), - ], - )), - SizedBox(height: PharMeTheme.smallToMediumSpace), - SubHeader(context.l10n.gene_page_affected_drugs, - tooltip: context.l10n.gene_page_affected_drugs_tooltip), - SizedBox(height: PharMeTheme.smallSpace), - ...buildDrugList(context, state, - noDrugsMessage: context.l10n.gene_page_no_affected_drugs) - ], + if (inhibitableGenes.contains(phenotype.geneSymbol)) + ...buildDrugInteractionInfo( + context, + phenotype.geneSymbol, + ), + ], + )), + SizedBox(height: PharMeTheme.smallToMediumSpace), + SubHeader(context.l10n.gene_page_affected_drugs, + tooltip: context.l10n.gene_page_affected_drugs_tooltip), + SizedBox(height: PharMeTheme.smallSpace), + ...buildDrugList(context, state, activeDrugs, + noDrugsMessage: context.l10n.gene_page_no_affected_drugs) + ], + ), ), - ), - ], + ], + ), ), - ), + ) ); }