Skip to content

Commit

Permalink
feat(#668): pass consumer to drug list filter
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Nov 22, 2023
1 parent c61a234 commit abcff68
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 68 deletions.
4 changes: 4 additions & 0 deletions app/lib/common/models/userdata/userdata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions app/lib/common/widgets/drug_list/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'drug_items/drug_cards.dart';
List<Widget> buildDrugList(
BuildContext context,
DrugListState state,
ActiveDrugs activeDrugs,
{
String? noDrugsMessage,
List<Widget> Function(
Expand All @@ -18,8 +19,11 @@ List<Widget> buildDrugList(
Map? drugItemsBuildParams,
}
) {
List<Widget> buildDrugList(List<Drug> drugs, FilterState filter) {
final filteredDrugs = filter.filter(drugs);
List<Widget> buildDrugList(
List<Drug> drugs,
FilterState filter,
) {
final filteredDrugs = filter.filter(drugs, activeDrugs);
if (filteredDrugs.isEmpty && noDrugsMessage != null) {
return [errorIndicator(noDrugsMessage)];
}
Expand Down
7 changes: 4 additions & 3 deletions app/lib/common/widgets/drug_list/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class FilterState {
final Map<WarningLevel, bool> 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() :
Expand All @@ -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<Drug> filter(List<Drug> drugs) => drugs.filter(isAccepted).toList();
List<Drug> filter(List<Drug> drugs, ActiveDrugs activeDrugs) =>
drugs.filter((drug) => isAccepted(drug, activeDrugs)).toList();
}

@freezed
Expand Down
8 changes: 6 additions & 2 deletions app/lib/common/widgets/drug_search.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<ActiveDrugs>(
builder: (context, activeDrugs, child) => BlocProvider(
create: (context) => cubit,
child: BlocBuilder<DrugListCubit, DrugListState>(
builder: (context, state) {
Expand All @@ -61,6 +63,7 @@ class DrugSearch extends HookWidget {
buildDrugList(
context,
state,
activeDrugs,
buildDrugItems: buildDrugItems,
noDrugsMessage: noDrugsMessage,
drugItemsBuildParams: drugItemsBuildParams,
Expand All @@ -71,7 +74,8 @@ class DrugSearch extends HookWidget {
..._maybeShowDrugInteractionExplanation(context),
],
);
}
}
)
)
);
}
Expand Down
126 changes: 65 additions & 61 deletions app/lib/report/pages/gene.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,70 +15,72 @@ class GenePage extends HookWidget {

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => cubit,
child: BlocBuilder<DrugListCubit, DrugListState>(
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<ActiveDrugs>(
builder: (context, activeDrugs, child) => BlocProvider(
create: (context) => cubit,
child: BlocBuilder<DrugListCubit, DrugListState>(
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)
],
),
),
),
],
],
),
),
),
)
);
}

Expand Down

0 comments on commit abcff68

Please sign in to comment.