Skip to content

Commit

Permalink
feat(app): do not repeat medications in list when filtered
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Sep 18, 2024
1 parent 86ac65c commit d259a7c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
12 changes: 9 additions & 3 deletions app/lib/common/widgets/drug_list/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DrugList extends StatelessWidget {
this.buildDrugItems = buildDrugCards,
this.showDrugInteractionIndicator = false,
this.searchForDrugClass = true,
this.repeatMedications = false,
this.repeatMedicationsWhenNotFiltered = false,
this.buildContainer,
});

Expand All @@ -28,7 +28,7 @@ class DrugList extends StatelessWidget {
final DrugItemBuilder buildDrugItems;
final bool showDrugInteractionIndicator;
final bool searchForDrugClass;
final bool repeatMedications;
final bool repeatMedicationsWhenNotFiltered;
final Widget Function(List<Widget> children)? buildContainer;

Widget _buildDrugList(
Expand All @@ -54,6 +54,12 @@ class DrugList extends StatelessWidget {
keyPrefix: 'active-',
)
: null;
final repeatMedications = repeatMedicationsWhenNotFiltered &&
!areDrugsFiltered(
state: state,
activeDrugs: activeDrugs,
searchForDrugClass: searchForDrugClass,
);
final otherDrugs = repeatMedications
? filteredDrugs
: filteredDrugs.filter((drug) => !drug.isActive).toList();
Expand All @@ -75,7 +81,7 @@ class DrugList extends StatelessWidget {
),
...activeDrugsList,
],
if (activeDrugsList != null) SubheaderDivider(
if (activeDrugsList != null && allDrugsList.isNotEmpty) SubheaderDivider(
text: otherDrugsHeader,
key: Key('header-other'),
useLine: false,
Expand Down
16 changes: 16 additions & 0 deletions app/lib/common/widgets/drug_list/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,19 @@ class DrugListState with _$DrugListState {
) = _LoadedState;
const factory DrugListState.error() = _ErrorState;
}

bool areDrugsFiltered({
required DrugListState state,
required ActiveDrugs activeDrugs,
required bool searchForDrugClass,
}) => state.whenOrNull(
loaded: (allDrugs, filter) {
final totalNumberOfDrugs = allDrugs.length;
final currentNumberOfDrugs = filter.filter(
allDrugs,
activeDrugs,
searchForDrugClass: searchForDrugClass,
).length;
return totalNumberOfDrugs != currentNumberOfDrugs;
},
) ?? false;
2 changes: 1 addition & 1 deletion app/lib/common/widgets/drug_search/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class DrugSearch extends HookWidget {
searchForDrugClass: searchForDrugClass,
buildContainer:
(children) => scrollList(keepPosition: keepPosition, children),
repeatMedications: repeatMedications,
repeatMedicationsWhenNotFiltered: repeatMedications,
),
_maybeBuildInteractionIndicator(context, state, activeDrugs)
?? SizedBox.shrink(),
Expand Down
18 changes: 6 additions & 12 deletions app/lib/common/widgets/drug_search/drug_filters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,11 @@ class DrugFilters extends StatelessWidget {
final bool searchForDrugClass;

bool _showActiveIndicator() {
final itemsAreFiltered = state.whenOrNull(
loaded: (allDrugs, filter) {
final totalNumberOfDrugs = allDrugs.length;
final currentNumberOfDrugs = filter.filter(
allDrugs,
activeDrugs,
searchForDrugClass: searchForDrugClass,
).length;
return totalNumberOfDrugs != currentNumberOfDrugs;
},
return areDrugsFiltered(
state: state,
activeDrugs: activeDrugs,
searchForDrugClass: searchForDrugClass,
);
return itemsAreFiltered ?? false;
}

Widget _buildActiveIndicator() {
Expand Down Expand Up @@ -212,7 +205,8 @@ class DrugFilters extends StatelessWidget {
icon: Stack(
children: [
Icon(Icons.filter_list),
if (enableIndicator && _showActiveIndicator()) _buildActiveIndicator(),
if (enableIndicator && _showActiveIndicator())
_buildActiveIndicator(),
],
),
color: PharMeTheme.iconColor,
Expand Down
2 changes: 1 addition & 1 deletion app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
}
},
"search_no_drugs_with_filter_amendment": " or filters",
"search_no_drugs": "No medications found. Try adjusting the search term{amendment}.\n\nIf the medication you are looking for is not included in PharMe, it might not have relevant DNA-based guidelines – then clinical dosing applies. Consult your pharmacist or doctor for more information.",
"search_no_drugs": "No medications found. Try adjusting the search term{amendment}.\n\nIf the medication you are looking for is not included in PharMe, it might not have relevant DNA-based guidelines. Clinical dosing may apply, consult your pharmacist or doctor for more information.",
"@search_no_drugs": {
"placeholders": {
"amendment": {
Expand Down

0 comments on commit d259a7c

Please sign in to comment.