Commit 263db2f

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-01-17 18:15:11
only show relevant medications
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 6d3f5d7
Changed files (5)
lib
components
model
blood_pressure
medicine
storage
screens
lib/components/dialoges/add_measurement_dialoge.dart
@@ -66,7 +66,9 @@ class _AddEntryDialogeState extends State<AddEntryDialoge> {
   /// Last [FormState.save]d note.
   String? notes;
   
-  /// Index of the selected medicine in `widget.settings.medications`.
+  /// Index of the selected medicine in non hidden medications.
+  ///
+  /// Non hidden medications are obtained at the start of the build method.
   int? medicineId;
 
   /// Whether to show the medication dosis input
@@ -210,6 +212,7 @@ class _AddEntryDialogeState extends State<AddEntryDialoge> {
 
   @override
   Widget build(BuildContext context) {
+    final medications = widget.settings.medications.where((e) => !e.hidden);
     final localizations = AppLocalizations.of(context)!;
     return FullscreenDialoge(
       onActionButtonPressed: () {
@@ -221,7 +224,7 @@ class _AddEntryDialogeState extends State<AddEntryDialoge> {
               && medicineId != null) {
             intake = MedicineIntake(
               timestamp: time,
-              medicine: widget.settings.medications
+              medicine: medications
                   .where((e) => e.id == medicineId).first,
               dosis: medicineDosis!,
             );
@@ -309,7 +312,7 @@ class _AddEntryDialogeState extends State<AddEntryDialoge> {
                 initialColor: needlePin?.color ?? Colors.transparent,
                 shape: buildShapeBorder(needlePin?.color),
               ),
-              if (widget.settings.medications.isNotEmpty && widget.initialRecord == null)
+              if (medications.isNotEmpty && widget.initialRecord == null)
                 Padding(
                   padding: const EdgeInsets.symmetric(vertical: 16),
                   child: Row(
@@ -317,10 +320,10 @@ class _AddEntryDialogeState extends State<AddEntryDialoge> {
                       Expanded(
                         child: DropdownButtonFormField<Medicine?>(
                           isExpanded: true,
-                          value: widget.settings.medications
+                          value: medications
                               .where((e) => e.id == medicineId).firstOrNull,
                           items: [
-                            for (final e in widget.settings.medications)
+                            for (final e in medications)
                               DropdownMenuItem(
                                 value: e,
                                 child: Text(e.designation),
lib/model/blood_pressure/medicine/medicine.dart
@@ -23,7 +23,7 @@ class Medicine {
     required this.designation,
     required this.color,
     required this.defaultDosis,
-    required this.hidden,
+    this.hidden = false,
   });
 
   /// Serialize the object to a restoreable map.
lib/model/storage/settings_store.dart
@@ -307,7 +307,8 @@ class Settings extends ChangeNotifier {
   final List<Medicine> _medications = [];
   /// All medications ever added.
   ///
-  /// This includes medications that got hidden.
+  /// This includes medications that got hidden. To obtain medications for a
+  /// selection, do `settings.medications.where((e) => !e.hidden)`.
   UnmodifiableListView<Medicine> get medications => 
       UnmodifiableListView(_medications);
 
@@ -336,7 +337,7 @@ class Settings extends ChangeNotifier {
 // When adding fields notice the checklist at the top.
 }
 
-extension _Serialization on ThemeMode {
+extension Serialization on ThemeMode {
   /// Turns enum into a restoreable integer.
   int serialize() {
     switch(this) {
lib/screens/subsettings/medicine_manager_screen.dart
@@ -5,7 +5,11 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
 import 'package:provider/provider.dart';
 
 /// Screen to view and edit medications saved in [Settings].
+///
+/// This screen allows adding and removing medication but not modifying them in
+/// order to keep the code simple and maintainable.
 class MedicineManagerScreen extends StatelessWidget {
+  /// Create a screen to manage medications in settings.
   const MedicineManagerScreen({super.key});
 
   @override
@@ -17,7 +21,8 @@ class MedicineManagerScreen extends StatelessWidget {
       ),
       body: Center(child: Consumer<Settings>(
           builder: (context, settings, child) {
-            final medications = settings.medications;
+            final medications = settings.medications.where((e) => !e.hidden)
+                .toList();
             return ListView.builder(
                 itemCount: medications.length + 1,
                 itemBuilder: (context, i) {
@@ -26,20 +31,22 @@ class MedicineManagerScreen extends StatelessWidget {
                       leading: const Icon(Icons.add),
                       title: Text(localizations.addMedication),
                       onTap: () async {
-                        final medicine = await showAddMedicineDialoge(context, settings);
+                        final medicine = await showAddMedicineDialoge(context,
+                            settings,);
                         if (medicine != null) settings.addMedication(medicine);
                       },
                     );
                   }
                   return ListTile(
-                    leading: medications[i].color == Colors.transparent ? null : Container(
-                      width: 40.0,
-                      height: 40.0,
-                      decoration: BoxDecoration(
-                        color: medications[i].color,
-                        shape: BoxShape.circle,
+                    leading: medications[i].color == Colors.transparent ? null
+                      : Container(
+                        width: 40.0,
+                        height: 40.0,
+                        decoration: BoxDecoration(
+                          color: medications[i].color,
+                          shape: BoxShape.circle,
+                        ),
                       ),
-                    ),
                     title: Text(medications[i].designation),
                     subtitle: Text('${localizations.defaultDosis}: '
                         '${medications[i].defaultDosis}'),
lib/screens/settings_screen.dart
@@ -24,7 +24,9 @@ import 'package:restart_app/restart_app.dart';
 import 'package:sqflite/sqflite.dart';
 import 'package:url_launcher/url_launcher.dart';
 
+/// Primary settings page to manage basic settings and link to subsettings.
 class SettingsPage extends StatelessWidget {
+  /// Create a primary settings screen.
   const SettingsPage({super.key});
 
   @override
@@ -80,7 +82,7 @@ class SettingsPage extends StatelessWidget {
                 items: [
                   DropdownMenuItem(child: Text(localizations.system)),
                   for (final l in AppLocalizations.supportedLocales)
-                    DropdownMenuItem(value: l, child: Text(getDisplayLanguage(l) ?? l.languageCode)),
+                    DropdownMenuItem(value: l, child: Text(getDisplayLanguage(l))),
                 ],
                 onChanged: (Locale? value) {
                   settings.language = value;