-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(app): use Widget for DrugList
- Loading branch information
Showing
10 changed files
with
98 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,61 @@ | ||
import '../../module.dart'; | ||
|
||
class DrugItemsBuildParams { | ||
DrugItemsBuildParams({required this.isEditable, required this.setActivity}); | ||
typedef DrugItemBuilder = List<Widget> Function( | ||
BuildContext context, | ||
List<Drug> drugs, | ||
{ required bool showDrugInteractionIndicator } | ||
); | ||
|
||
final bool isEditable; | ||
final SetDrugActivityFunction setActivity; | ||
} | ||
class DrugList extends StatelessWidget { | ||
const DrugList({ | ||
super.key, | ||
required this.state, | ||
required this.activeDrugs, | ||
this.noDrugsMessage, | ||
this.buildDrugItems = buildDrugCards, | ||
this.showDrugInteractionIndicator = false, | ||
this.searchForDrugClass = true, | ||
this.buildContainer, | ||
}); | ||
|
||
List<Widget> buildDrugList( | ||
BuildContext context, | ||
DrugListState state, | ||
ActiveDrugs activeDrugs, | ||
{ | ||
String? noDrugsMessage, | ||
List<Widget> Function( | ||
BuildContext context, | ||
List<Drug> drugs, | ||
{ | ||
DrugItemsBuildParams? buildParams, | ||
bool showDrugInteractionIndicator, | ||
} | ||
) buildDrugItems = buildDrugCards, | ||
bool showDrugInteractionIndicator = false, | ||
bool useDrugClass = true, | ||
DrugItemsBuildParams? drugItemsBuildParams, | ||
} | ||
) { | ||
List<Widget> buildDrugList( | ||
final DrugListState state; | ||
final ActiveDrugs activeDrugs; | ||
final String? noDrugsMessage; | ||
final DrugItemBuilder buildDrugItems; | ||
final bool showDrugInteractionIndicator; | ||
final bool searchForDrugClass; | ||
final Widget Function(List<Widget> children)? buildContainer; | ||
|
||
Widget _buildDrugList( | ||
BuildContext context, | ||
List<Drug> drugs, | ||
FilterState filter, | ||
) { | ||
final filteredDrugs = filter.filter( | ||
drugs, | ||
activeDrugs, | ||
useDrugClass: useDrugClass, | ||
searchForDrugClass: searchForDrugClass, | ||
); | ||
if (filteredDrugs.isEmpty && noDrugsMessage != null) { | ||
return [errorIndicator(noDrugsMessage)]; | ||
return errorIndicator(noDrugsMessage!); | ||
} | ||
return buildDrugItems( | ||
final drugItems = buildDrugItems( | ||
context, | ||
filteredDrugs.sortedBy((drug) => drug.name), | ||
buildParams: drugItemsBuildParams, | ||
showDrugInteractionIndicator: showDrugInteractionIndicator, | ||
); | ||
return (buildContainer != null) | ||
? buildContainer!(drugItems) | ||
: Column(children: drugItems); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return state.when( | ||
initial: SizedBox.shrink, | ||
error: () => errorIndicator(context.l10n.err_generic), | ||
loaded: (allDrugs, filter) => _buildDrugList(context, allDrugs, filter), | ||
loading: loadingIndicator, | ||
); | ||
} | ||
return state.when( | ||
initial: () => [Container()], | ||
error: () => [errorIndicator(context.l10n.err_generic)], | ||
loaded: buildDrugList, | ||
loading: () => [loadingIndicator()], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletion
9
app/lib/common/widgets/drug_list/drug_items/drug_selection_list.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters