Skip to content

Commit

Permalink
feat(#668): use consumer on report page
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Nov 22, 2023
1 parent 4f4ef7e commit a98e404
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
10 changes: 5 additions & 5 deletions app/lib/common/models/userdata/userdata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,26 @@ class UserData {
}

class ActiveDrugs extends ChangeNotifier {
List<String> activeDrugs = [];
List<String> names = [];

Future<void> _preserveAndNotify() async {
UserData.instance.activeDrugNames = activeDrugs;
UserData.instance.activeDrugNames = names;
await UserData.save();
notifyListeners();
}

Future<void> setList(List<String> drugNames) async {
activeDrugs = drugNames;
names = drugNames;
await _preserveAndNotify();
}

Future<void> _add(String drugName) async {
activeDrugs.add(drugName);
names.add(drugName);
await _preserveAndNotify();
}

Future<void> _remove(String drugName) async {
activeDrugs = activeDrugs.filter((name) => name != drugName).toList();
names = names.filter((name) => name != drugName).toList();
await _preserveAndNotify();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import '../../../module.dart';
import 'utils.dart';

bool isDrugSelected(Drug drug) {
return UserData.instance.activeDrugNames!.contains(drug.name);
}

List<Widget> buildDrugCheckboxList(
BuildContext context,
List<Drug> drugs,
Expand All @@ -18,16 +14,16 @@ List<Widget> buildDrugCheckboxList(
final checkboxesEnabled = buildParams['checkboxesEnabled'];
final sortedDrugs = List<Drug>.from(drugs);
sortedDrugs.sort((drugA, drugB) {
final drugASelected = isDrugSelected(drugA);
final drugBSelected = isDrugSelected(drugB);
final drugASelected = drugA.isActive();
final drugBSelected = drugB.isActive();
if (drugASelected == drugBSelected) return drugA.name.compareTo(drugB.name);
return drugASelected ? -1 : 1;
});
return [
...sortedDrugs.map(
(drug) => CheckboxListTile(
enabled: checkboxesEnabled,
value: isDrugSelected(drug),
value: drug.isActive(),
onChanged: (value) => onCheckboxChange(drug, value),
title: Text(formatDrugName(drug, showDrugInteractionIndicator)),
subtitle: (drug.annotations.brandNames.isNotEmpty) ?
Expand Down
12 changes: 10 additions & 2 deletions app/lib/report/pages/report.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import 'package:provider/provider.dart';

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

class ReportPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final hasActiveInhibitors = UserData.instance.activeDrugNames != null &&
UserData.instance.activeDrugNames!.any(isInhibitor);
return Consumer<ActiveDrugs>(
builder: (context, activeDrugs, child) =>
_buildReportPage(context, activeDrugs)
);
}

Widget _buildReportPage(BuildContext context, ActiveDrugs activeDrugs) {
final hasActiveInhibitors = activeDrugs.names.any(isInhibitor);
final guidelineGenes = getGuidelineGenes();

final notTestedString = context.l10n.general_not_tested;
Expand Down

0 comments on commit a98e404

Please sign in to comment.