Skip to content

Commit

Permalink
feat(#670): make drugs in selection only searchable by name
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Dec 1, 2023
1 parent 43e670a commit 63bc4db
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
10 changes: 6 additions & 4 deletions app/lib/common/models/drug/drug.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
7 changes: 6 additions & 1 deletion app/lib/common/widgets/drug_list/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ List<Widget> buildDrugList(
}
) buildDrugItems = buildDrugCards,
bool showDrugInteractionIndicator = false,
bool useDrugClass = true,
Map? drugItemsBuildParams,
}
) {
List<Widget> buildDrugList(
List<Drug> 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)];
}
Expand Down
24 changes: 17 additions & 7 deletions app/lib/common/widgets/drug_list/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ class FilterState {
final Map<WarningLevel, bool> 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() :
Expand All @@ -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<Drug> filter(List<Drug> drugs, ActiveDrugs activeDrugs) =>
drugs.filter((drug) => isAccepted(drug, activeDrugs)).toList();
List<Drug> filter(List<Drug> drugs, ActiveDrugs activeDrugs, {
required bool useDrugClass,
}) =>
drugs.filter((drug) => isAccepted(
drug,
activeDrugs,
useDrugClass: useDrugClass,
)
).toList();
}

@freezed
Expand Down
8 changes: 7 additions & 1 deletion app/lib/common/widgets/drug_search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ class DrugSearch extends HookWidget {
required this.showFilter,
required this.buildDrugItems,
required this.showDrugInteractionIndicator,
this.useDrugClass = true,
this.keepPosition = false,
this.drugItemsBuildParams,
DrugListCubit? cubit,
}) : cubit = cubit ?? DrugListCubit(),
super(key: key);

final bool showFilter;
final bool useDrugClass;
final bool keepPosition;
final List<Widget> Function(
BuildContext context,
Expand Down Expand Up @@ -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),
]
),
Expand All @@ -72,6 +77,7 @@ class DrugSearch extends HookWidget {
drugItemsBuildParams: drugItemsBuildParams,
showDrugInteractionIndicator:
showDrugInteractionIndicator,
useDrugClass: useDrugClass,
)
),
..._maybeShowDrugInteractionExplanation(context),
Expand Down
1 change: 1 addition & 0 deletions app/lib/drug_selection/pages/drug_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class DrugSelectionPage extends HookWidget {
return DrugSearch(
showFilter: false,
keepPosition: true,
useDrugClass: false,
buildDrugItems: buildDrugCheckboxList,
drugItemsBuildParams: {
'checkboxesEnabled': _isEditable(state),
Expand Down
1 change: 1 addition & 0 deletions app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 63bc4db

Please sign in to comment.