Skip to content

Commit

Permalink
feat(app): use drug sub-lists everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Sep 7, 2024
1 parent d0e1848 commit 94c7056
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 51 deletions.
50 changes: 44 additions & 6 deletions app/lib/common/widgets/drug_list/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import '../../module.dart';
typedef DrugItemBuilder = List<Widget> Function(
BuildContext context,
List<Drug> drugs,
{ required bool showDrugInteractionIndicator }
{
required bool showDrugInteractionIndicator,
required String keyPrefix,
}
);

class DrugList extends StatelessWidget {
Expand All @@ -15,6 +18,7 @@ class DrugList extends StatelessWidget {
this.buildDrugItems = buildDrugCards,
this.showDrugInteractionIndicator = false,
this.searchForDrugClass = true,
this.repeatMedications = false,
this.buildContainer,
});

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

Widget _buildDrugList(
Expand All @@ -35,18 +40,51 @@ class DrugList extends StatelessWidget {
drugs,
activeDrugs,
searchForDrugClass: searchForDrugClass,
);
).sortedBy((drug) => drug.name);
if (filteredDrugs.isEmpty && noDrugsMessage != null) {
return errorIndicator(noDrugsMessage!);
}
final drugItems = buildDrugItems(
final activeFilteredDrugs =
filteredDrugs.filter((drug) => drug.isActive).toList();
final activeDrugsList = activeFilteredDrugs.isNotEmpty
? buildDrugItems(
context,
activeFilteredDrugs,
showDrugInteractionIndicator: showDrugInteractionIndicator,
keyPrefix: 'active-',
)
: null;
final otherDrugs = repeatMedications
? filteredDrugs
: filteredDrugs.filter((drug) => !drug.isActive).toList();
final otherDrugsHeader = repeatMedications
? context.l10n.drug_list_subheader_all_drugs
: context.l10n.drug_list_subheader_other_drugs;
final allDrugsList = buildDrugItems(
context,
filteredDrugs.sortedBy((drug) => drug.name),
otherDrugs,
showDrugInteractionIndicator: showDrugInteractionIndicator,
keyPrefix: 'other-',
);
final drugLists = [
if (activeDrugsList != null) ...[
SubheaderDivider(
text: context.l10n.drug_list_subheader_active_drugs,
key: Key('header-active'),
useLine: false,
),
...activeDrugsList,
],
if (activeDrugsList != null) SubheaderDivider(
text: otherDrugsHeader,
key: Key('header-other'),
useLine: false,
),
...allDrugsList,
];
return (buildContainer != null)
? buildContainer!(drugItems)
: Column(children: drugItems);
? buildContainer!(drugLists)
: Column(crossAxisAlignment: CrossAxisAlignment.start, children: drugLists);
}

@override
Expand Down
7 changes: 5 additions & 2 deletions app/lib/common/widgets/drug_list/drug_items/drug_cards.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import 'utils.dart';
List<Widget> buildDrugCards(
BuildContext context,
List<Drug> drugs,
{ required bool showDrugInteractionIndicator }
{
required bool showDrugInteractionIndicator,
required String keyPrefix,
}
) {
drugs.sort((drugA, drugB) {
final warningLevelComparison = -drugA.warningLevel.severity
Expand All @@ -15,7 +18,7 @@ List<Widget> buildDrugCards(
return warningLevelComparison;
});
return drugs.map((drug) => DrugCard(
key: Key('drug-card-${drug.name}'),
key: Key('${keyPrefix}drug-card-${drug.name}'),
onTap: () => context.router
.push(DrugRoute(drug: drug))
.then((_) => context.read<DrugListCubit>().search()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,17 @@ List<Widget> buildDrugSelectionList(
{
DrugItemsBuildParams? buildParams,
required bool showDrugInteractionIndicator,
required String keyPrefix,
}
) {
if (buildParams == null) throw Exception();
final activeDrugs = drugs.filter((drug) => drug.isActive).toList();
final activeDrugsList = activeDrugs.isEmpty
? [Padding(
padding: EdgeInsets.all(PharMeTheme.mediumSpace),
child: Text(
context.l10n.drug_selection_no_active_drugs,
style: TextStyle(fontStyle: FontStyle.italic),
),
)]
: _buildSelectionList(
context,
activeDrugs,
buildParams,
showDrugInteractionIndicator,
keyPrefix: 'active',
);
final allDrugsList = _buildSelectionList(
return _buildSelectionList(
context,
drugs,
buildParams,
showDrugInteractionIndicator,
keyPrefix: 'all',
keyPrefix: keyPrefix,
);
return [
SubheaderDivider(
text: context.l10n.drug_selection_subheader_active_drugs,
key: Key('header-active'),
useLine: false,
),
...activeDrugsList,
SubheaderDivider(
text: context.l10n.drug_selection_subheader_all_drugs,
key: Key('header-all'),
useLine: false,
),
...allDrugsList,
];
}

List<SwitchListTile> _buildSelectionList(
Expand Down
17 changes: 10 additions & 7 deletions app/lib/common/widgets/drug_search/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ class DrugSearch extends HookWidget {
required this.state,
required this.activeDrugs,
this.keepPosition = false,
this.repeatMedications = false,
});

final bool showFilter;
final bool searchForDrugClass;
final bool keepPosition;
final bool repeatMedications;
final DrugItemBuilder buildDrugItems;
final bool showDrugInteractionIndicator;
final DrugListCubit cubit;
Expand Down Expand Up @@ -55,13 +57,14 @@ class DrugSearch extends HookWidget {
buildDrugItems: buildDrugItems,
showDrugInteractionIndicator: showDrugInteractionIndicator,
noDrugsMessage: context.l10n.search_no_drugs(
showFilter
? context.l10n.search_no_drugs_with_filter_amendment
: ''
),
searchForDrugClass: searchForDrugClass,
buildContainer:
(children) => scrollList(keepPosition: keepPosition, children),
showFilter
? context.l10n.search_no_drugs_with_filter_amendment
: ''
),
searchForDrugClass: searchForDrugClass,
buildContainer:
(children) => scrollList(keepPosition: keepPosition, children),
repeatMedications: repeatMedications,
),
_maybeBuildInteractionIndicator(context, state, activeDrugs)
?? SizedBox.shrink(),
Expand Down
5 changes: 4 additions & 1 deletion app/lib/drug_selection/pages/drug_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,20 @@ class DrugSelectionPage extends HookWidget {
buildDrugItems: (
context,
drugs,
{ required showDrugInteractionIndicator }
{ required showDrugInteractionIndicator, required keyPrefix }
) =>
buildDrugSelectionList(
context,
drugs,
showDrugInteractionIndicator: showDrugInteractionIndicator,
keyPrefix: keyPrefix,
buildParams: DrugItemsBuildParams(
isEditable: _isEditable(state),
setActivity: context.read<DrugSelectionCubit>().updateDrugActivity,
)),
showDrugInteractionIndicator: !concludesOnboarding,
// to not confuse users when selecting a medication that then is removed from the list
repeatMedications: true,
),
);
}
Expand Down
7 changes: 4 additions & 3 deletions app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
"drug_selection_onboarding_description": "Please review the medications you are currently taking below and update them if needed. You can always change the status for a medication later on a medication page or in the settings.",
"drug_selection_settings_description": "Review the medications you are currently taking below.",
"drug_selection_no_drugs_loaded": "No medications loaded",
"drug_selection_subheader_active_drugs": "Current medications",
"drug_selection_subheader_all_drugs": "All medications",
"drug_selection_no_active_drugs": "No current medications selected",

"drug_list_subheader_active_drugs": "Current medications",
"drug_list_subheader_all_drugs": "All medications",
"drug_list_subheader_other_drugs": "Other medications",

"err_could_not_retrieve_access_token": "An unexpected error occurred while retrieving the access token",
"err_fetch_user_data_failed": "An error occurred while getting data, please try again later",
Expand Down

0 comments on commit 94c7056

Please sign in to comment.