Skip to content

Commit

Permalink
feat(#712): sort and color gene tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Sep 3, 2024
1 parent dbd2569 commit a80f6f3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/lib/common/models/drug/warning_level.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ extension WarningLevelLegend on List<WarningLevel> {
required String? Function(WarningLevel) getText,
InlineSpan? separator,
}) {
// TODO(tamslo): isLastItem should consider skipped items and consider potential icon margin to add after text, https://github.com/hpi-dhc/PharMe/issues/712
var content = <InlineSpan>[];
for (final (index, warningLevel) in indexed) {
final text = getText(warningLevel);
Expand Down
86 changes: 71 additions & 15 deletions app/lib/report/pages/report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:provider/provider.dart';

import '../../common/module.dart';

typedef WarningLevelCounts = Map<WarningLevel, int>;

@RoutePage()
class ReportPage extends StatelessWidget {
@override
Expand All @@ -12,6 +14,12 @@ class ReportPage extends StatelessWidget {
);
}

int _getSeverityCount(WarningLevelCounts warningLevelCounts, int severity) {
return warningLevelCounts.filter(
(warningLevelCount) => warningLevelCount.key.severity == severity
).values.first;
}

Widget _buildReportPage(BuildContext context, ActiveDrugs activeDrugs) {
final presentGenes = Set.from(UserData.instance.genotypeResults!.values.map(
(genotypeResult) => genotypeResult.gene
Expand All @@ -24,7 +32,34 @@ class ReportPage extends StatelessWidget {
...missingGenes.map(
(gene) => GenotypeResult.missingResult(gene, context),
),
].sortedBy((genotypeResult) => genotypeResult.gene);
];
final warningLevelCounts = <String, WarningLevelCounts>{};
for (final genotypeResult in userGenotypes) {
warningLevelCounts[genotypeResult.gene] = {};
final affectedDrugs = CachedDrugs.instance.drugs?.filter(
(drug) => drug.guidelineGenotypes.contains(genotypeResult.key.value)
) ?? [];
for (final warningLevel in WarningLevel.values) {
warningLevelCounts[genotypeResult.gene]![warningLevel] =
affectedDrugs.filter(
(drug) => drug.warningLevel == warningLevel
).length;
}
}
var sortedGenotypes = userGenotypes.sortedBy(
(genotypeResult) => genotypeResult.gene
);
final sortedWarningLevels = WarningLevel.values.sortedBy(
(warningLevel) => warningLevel.severity
);
for (final warningLevel in sortedWarningLevels) {
sortedGenotypes = sortedGenotypes.sortedByDescending((genotypeResult) =>
_getSeverityCount(
warningLevelCounts[genotypeResult.gene]!,
warningLevel.severity,
),
);
}
final hasActiveInhibitors = activeDrugs.names.any(isInhibitor);
return PopScope(
canPop: false,
Expand All @@ -46,8 +81,9 @@ class ReportPage extends StatelessWidget {
]
),
),
...userGenotypes.map((genotypeResult) => GeneCard(
...sortedGenotypes.map((genotypeResult) => GeneCard(
genotypeResult,
warningLevelCounts[genotypeResult.gene]!,
key: Key('gene-card-${genotypeResult.key.value}')
)),
],
Expand All @@ -66,9 +102,19 @@ class ReportPage extends StatelessWidget {
}

class GeneCard extends StatelessWidget {
const GeneCard(this.genotypeResult, { super.key });
const GeneCard(this.genotypeResult, this.warningLevelCounts, { super.key });

final GenotypeResult genotypeResult;
final WarningLevelCounts warningLevelCounts;

Color? _getHighestSeverityColor(WarningLevelCounts warningLevelCounts) {
final sortedWarningLevels = WarningLevel.values.sortedByDescending(
(warningLevel) => warningLevel.severity
);
return sortedWarningLevels.filter(
(warningLevel) => warningLevelCounts[warningLevel]! > 0
).firstOrNull?.color;
}

@override
Widget build(BuildContext context) {
Expand All @@ -79,14 +125,13 @@ class GeneCard extends StatelessWidget {
final phenotypeText = phenotypeInformation.adaptionText.isNullOrBlank
? phenotypeInformation.phenotype
: '${phenotypeInformation.phenotype}$drugInteractionIndicator';
final affectedDrugs = CachedDrugs.instance.drugs?.filter(
(drug) => drug.guidelineGenotypes.contains(genotypeResult.key.value)
) ?? [];
final hasLegend = warningLevelCounts.values.any((count) => count > 0);
return RoundedCard(
onTap: () => context.router.push(
GeneRoute(genotypeResult: genotypeResult)
),
radius: 16,
color: _getHighestSeverityColor(warningLevelCounts),
child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
Expanded(
child: Column(
Expand All @@ -106,15 +151,26 @@ class GeneCard extends StatelessWidget {
overflow: TextOverflow.ellipsis,
),
),
Text.rich(WarningLevel.values.buildLegend(
getText: (warningLevel) {
final warningLevelCount = affectedDrugs.filter(
(drug) => drug.warningLevel == warningLevel
).length;
return warningLevelCount > 0
? warningLevelCount.toString()
: null;
}),
if (hasLegend) DecoratedBox(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10)),
),
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 2,
horizontal: 4,
),
child: Text.rich(WarningLevel.values.buildLegend(
getText: (warningLevel) {
final warningLevelCount =
warningLevelCounts[warningLevel]!;
return warningLevelCount > 0
? warningLevelCount.toString()
: null;
}),
),
),
),
],
),
Expand Down

0 comments on commit a80f6f3

Please sign in to comment.