From 63bc4dbd3c5a84960abddd02d23102e8c16565ad Mon Sep 17 00:00:00 2001 From: Tamara Slosarek Date: Fri, 1 Dec 2023 14:23:20 +0100 Subject: [PATCH] feat(#670): make drugs in selection only searchable by name --- app/lib/common/models/drug/drug.dart | 10 ++++---- app/lib/common/widgets/drug_list/builder.dart | 7 +++++- app/lib/common/widgets/drug_list/cubit.dart | 24 +++++++++++++------ app/lib/common/widgets/drug_search.dart | 8 ++++++- .../drug_selection/pages/drug_selection.dart | 1 + app/lib/l10n/app_en.arb | 1 + 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/lib/common/models/drug/drug.dart b/app/lib/common/models/drug/drug.dart index b9b3e15b..e962bf98 100644 --- a/app/lib/common/models/drug/drug.dart +++ b/app/lib/common/models/drug/drug.dart @@ -74,10 +74,12 @@ extension DrugIsActive on Drug { } extension DrugMatchesQuery on Drug { - bool matches({required String query}) { - return name.ilike(query) || - (annotations.drugclass.ilike(query)) || - (annotations.brandNames.any((brand) => brand.ilike(query))); + bool matches({required String query, required bool useClass}) { + final namesMatch = name.ilike(query) || + (annotations.brandNames.any((brand) => brand.ilike(query))); + return useClass + ? namesMatch || (annotations.drugclass.ilike(query)) + : namesMatch; } } diff --git a/app/lib/common/widgets/drug_list/builder.dart b/app/lib/common/widgets/drug_list/builder.dart index 5883d4da..081109ca 100644 --- a/app/lib/common/widgets/drug_list/builder.dart +++ b/app/lib/common/widgets/drug_list/builder.dart @@ -16,6 +16,7 @@ List buildDrugList( } ) buildDrugItems = buildDrugCards, bool showDrugInteractionIndicator = false, + bool useDrugClass = true, Map? drugItemsBuildParams, } ) { @@ -23,7 +24,11 @@ List buildDrugList( List drugs, FilterState filter, ) { - final filteredDrugs = filter.filter(drugs, activeDrugs); + final filteredDrugs = filter.filter( + drugs, + activeDrugs, + useDrugClass: useDrugClass, + ); 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 272a3ccd..d5aeae22 100644 --- a/app/lib/common/widgets/drug_list/cubit.dart +++ b/app/lib/common/widgets/drug_list/cubit.dart @@ -122,7 +122,9 @@ class FilterState { final Map showWarningLevel; final String gene; - bool isAccepted(Drug drug, ActiveDrugs activeDrugs) { + bool isAccepted(Drug drug, ActiveDrugs activeDrugs, { + required bool useDrugClass, + }) { final userGuideline = drug.userGuideline(); final guidelineGenes = drug.guidelines.isNotEmpty ? drug.guidelines.first.lookupkey.keys.toList() : @@ -136,15 +138,23 @@ class FilterState { warningLevelMatches = warningLevelMatches && showWarningLevel[WarningLevel.green]!; } - final isDrugAccepted = drug.matches(query: query) && - (activeDrugs.contains(drug.name) || showInactive) && - warningLevelMatches && - (gene.isBlank || (guidelineGenes.contains(gene))); + final isDrugAccepted = + drug.matches(query: query, useClass: useDrugClass) && + (activeDrugs.contains(drug.name) || showInactive) && + warningLevelMatches && + (gene.isBlank || (guidelineGenes.contains(gene))); return isDrugAccepted; } - List filter(List drugs, ActiveDrugs activeDrugs) => - drugs.filter((drug) => isAccepted(drug, activeDrugs)).toList(); + List filter(List drugs, ActiveDrugs activeDrugs, { + required bool useDrugClass, + }) => + drugs.filter((drug) => isAccepted( + drug, + activeDrugs, + useDrugClass: useDrugClass, + ) + ).toList(); } @freezed diff --git a/app/lib/common/widgets/drug_search.dart b/app/lib/common/widgets/drug_search.dart index a1fcdd31..fdff3a40 100644 --- a/app/lib/common/widgets/drug_search.dart +++ b/app/lib/common/widgets/drug_search.dart @@ -11,6 +11,7 @@ class DrugSearch extends HookWidget { required this.showFilter, required this.buildDrugItems, required this.showDrugInteractionIndicator, + this.useDrugClass = true, this.keepPosition = false, this.drugItemsBuildParams, DrugListCubit? cubit, @@ -18,6 +19,7 @@ class DrugSearch extends HookWidget { super(key: key); final bool showFilter; + final bool useDrugClass; final bool keepPosition; final List Function( BuildContext context, @@ -56,7 +58,10 @@ class DrugSearch extends HookWidget { ), ), SizedBox(width: PharMeTheme.smallToMediumSpace), - TooltipIcon(context.l10n.search_page_tooltip_search), + TooltipIcon(useDrugClass + ? context.l10n.search_page_tooltip_search + : context.l10n.search_page_tooltip_search_no_class + ), if (showFilter) buildFilter(context), ] ), @@ -72,6 +77,7 @@ class DrugSearch extends HookWidget { drugItemsBuildParams: drugItemsBuildParams, showDrugInteractionIndicator: showDrugInteractionIndicator, + useDrugClass: useDrugClass, ) ), ..._maybeShowDrugInteractionExplanation(context), diff --git a/app/lib/drug_selection/pages/drug_selection.dart b/app/lib/drug_selection/pages/drug_selection.dart index 379b79f5..8fe6c7ba 100644 --- a/app/lib/drug_selection/pages/drug_selection.dart +++ b/app/lib/drug_selection/pages/drug_selection.dart @@ -79,6 +79,7 @@ class DrugSelectionPage extends HookWidget { return DrugSearch( showFilter: false, keepPosition: true, + useDrugClass: false, buildDrugItems: buildDrugCheckboxList, drugItemsBuildParams: { 'checkboxesEnabled': _isEditable(state), diff --git a/app/lib/l10n/app_en.arb b/app/lib/l10n/app_en.arb index 7702e6b7..4abe6568 100644 --- a/app/lib/l10n/app_en.arb +++ b/app/lib/l10n/app_en.arb @@ -32,6 +32,7 @@ "general_not_tested": "Not tested", "search_page_tooltip_search": "Search for drugs by their name, brand name or class.", + "search_page_tooltip_search_no_class": "Search for drugs by their name or brand name.", "search_page_filter_only_active": "Only active drugs", "search_page_filter_green": "Green warning level", "search_page_filter_yellow": "Yellow warning level",