Skip to content

Commit

Permalink
refactor(app): add inhibitor list to PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Dec 6, 2024
1 parent 4f5a178 commit 12390bb
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 30 deletions.
21 changes: 17 additions & 4 deletions app/lib/common/models/drug/drug.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,26 @@ extension CriticalDrugs on List<Drug> {

List<String> getDrugsWithBrandNames(
List<String>? drugNames,
{ bool capitalize = false }
{
bool capitalize = false,
String? brandNamesPrefix,
}
) {
return drugNames?.map(
(drugName) => _getDrugWithBrandNames(drugName, capitalize: capitalize)
(drugName) => _getDrugWithBrandNames(
drugName,
capitalize: capitalize,
brandNamesPrefix: brandNamesPrefix,
)
).toList() ?? [];
}

String _getDrugWithBrandNames(
String drugName,
{ required bool capitalize }
{
bool capitalize = false,
String? brandNamesPrefix,
}
) {
final drug = DrugsWithGuidelines.instance.drugs?.firstOrNullWhere(
(drug) => drug.name == drugName
Expand All @@ -213,5 +223,8 @@ String _getDrugWithBrandNames(
if (drug == null || drug.annotations.brandNames.isEmpty) {
return displayedDrugName;
}
return '$displayedDrugName (${drug.annotations.brandNames.join(', ')})';
final branNamesString = drug.annotations.brandNames.join(', ');
return brandNamesPrefix != null
? '$displayedDrugName ($brandNamesPrefix: $branNamesString)'
: '$displayedDrugName ($branNamesString)';
}
79 changes: 57 additions & 22 deletions app/lib/common/widgets/gene_modulator_list.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../module.dart';

class GeneModulatorList extends StatelessWidget {
// TODO(tamslo): if only one item, just print label and text afterwards
class GeneModulatorList {
const GeneModulatorList({
super.key,
required this.geneName,
this.onlyActiveDrugs = false,
this.displayedDrug,
Expand All @@ -12,49 +12,84 @@ class GeneModulatorList extends StatelessWidget {
final bool onlyActiveDrugs;
final String? displayedDrug;

List<String> _filterActiveModulatorDrugNames(List<String> allModulatorDrugs) {
final activeModulators = activeInhibitorsFor(
geneName,
drug: displayedDrug,
);
return allModulatorDrugs.filter(activeModulators.contains).toList();
}

List<String> _getModulatorDrugNames(
BuildContext context,
Map<String, Map<String, dynamic>>modulatorDefinition,
String geneName,
) {
final allModulatorDrugs = modulatorDefinition[geneName]!.keys.toList();
if (onlyActiveDrugs) {
final activeModulators = activeInhibitorsFor(
geneName,
drug: displayedDrug,
);
return allModulatorDrugs.filter(activeModulators.contains).toList();
final modulatorDrugNames = onlyActiveDrugs
? _filterActiveModulatorDrugNames(allModulatorDrugs)
: allModulatorDrugs;
return getDrugsWithBrandNames(
modulatorDrugNames,
capitalize: true,
brandNamesPrefix: context.l10n.drug_item_brand_names.toLowerCase(),
);
}

Map<String, List<String>> getContent(BuildContext context) {
final contentDefinition = {
context.l10n.strong_inhibitors_description: strongDrugInhibitors,
context.l10n.moderate_inhibitors_description: moderateDrugInhibitors,
};
final content = <String, List<String>>{};
for (final subdefinition in contentDefinition.entries) {
final modulatorDefinition = subdefinition.value;
final drugNames =
_getModulatorDrugNames(context, modulatorDefinition, geneName);
if (drugNames.isEmpty) continue;
final getDescription = subdefinition.key;
content[getDescription(geneName)] = drugNames;
}
return allModulatorDrugs;
return content;
}

String asString(BuildContext context, {String linePrefix = ''}) {
final listString = StringBuffer();
for (final modulatorContentEntry in getContent(context).entries) {
final entryString = StringBuffer(modulatorContentEntry.key);
for (final drugName in modulatorContentEntry.value) {
entryString.write('\n$linePrefix- $drugName');
}
listString.write('\n$linePrefix$entryString');
}
return listString.toString();
}

GeneModulatorListWidget get widget => GeneModulatorListWidget(this);
}

class GeneModulatorListWidget extends StatelessWidget {
const GeneModulatorListWidget(this.listDefinition);

final GeneModulatorList listDefinition;

List<Widget> _buildModulatorSublist(modulatorContentEntry) {
final descriptionText = modulatorContentEntry.key;
final modulatorDrugNames = modulatorContentEntry.value;
if (modulatorDrugNames.isEmpty) return [SizedBox.shrink()];
return [
SizedBox(height: PharMeTheme.smallSpace),
Text(
descriptionText,
style: TextStyle(fontStyle: FontStyle.italic),
),
SizedBox(height: PharMeTheme.smallSpace),
UnorderedList(
getDrugsWithBrandNames(
modulatorDrugNames,
capitalize: true,
),
),
UnorderedList(modulatorDrugNames),
];
}

@override
Widget build(BuildContext context) {
final modulatorContent = {
context.l10n.strong_inhibitors_description(geneName):
_getModulatorDrugNames(strongDrugInhibitors, geneName),
context.l10n.moderate_inhibitors_description(geneName):
_getModulatorDrugNames(moderateDrugInhibitors, geneName),
};
final modulatorContent = listDefinition.getContent(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children:
Expand Down
14 changes: 11 additions & 3 deletions app/lib/common/widgets/phenoconversion_explanation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ String? getPhenoconversionExplanationString({
}) {
final displayConfig = displayType.getConfig(context);
return inhibitedGenotypes.flatMap((genotypeResult) => [
_getPhenoconversionDetailText(context, genotypeResult, drug: drugName, displayConfig: displayConfig),
// TODO: get list
_getPhenoconversionDetailText(
context,
genotypeResult, drug: drugName,
displayConfig: displayConfig,
),
GeneModulatorList(
geneName: genotypeResult.gene,
onlyActiveDrugs: true,
displayedDrug: drugName,
).asString(context, linePrefix: ' '),
]).join(displayConfig.partSeparator);
}

Expand Down Expand Up @@ -135,7 +143,7 @@ class PhenoconversionExplanation extends StatelessWidget {
geneName: genotypeResult.gene,
onlyActiveDrugs: true,
displayedDrug: drugName,
),
).widget,
]
).toList(),
);
Expand Down
2 changes: 1 addition & 1 deletion app/lib/faq/pages/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final faqContent = <FaqSection>[
children: [
Text(context.l10n.faq_answer_phenoconversion),
...inhibitableGenes.map(
(geneName) => GeneModulatorList(geneName: geneName),
(geneName) => GeneModulatorList(geneName: geneName).widget,
),
],
),
Expand Down

0 comments on commit 12390bb

Please sign in to comment.