Commit d3f9dc3

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-03-25 21:45:28
implement prefered pressure unit setting
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent ed32eb2
Changed files (2)
app
lib
model
app/lib/model/blood_pressure/pressure_unit.dart
@@ -6,5 +6,21 @@ enum PressureUnit {
   /// Millimeters of mercury.
   mmHg,
   /// Kilo Pascal
-  kPa,
+  kPa;
+
+  /// Encodes the pressure unit to [decode]able value.
+  int encode() => switch(this) {
+    PressureUnit.mmHg => 0,
+    PressureUnit.kPa => 1,
+  };
+  /// Decodes a pressure unit from an [encode]d value.
+  static PressureUnit? decode(int? encoded) => switch(encoded) {
+    0 => PressureUnit.mmHg,
+    1 => PressureUnit.kPa,
+    null => null,
+    _ => (){
+      assert(false);
+      return null;
+    }(),
+  };
 }
app/lib/model/storage/settings_store.dart
@@ -2,6 +2,7 @@ import 'dart:collection';
 import 'dart:convert';
 
 import 'package:blood_pressure_app/model/blood_pressure/medicine/medicine.dart';
+import 'package:blood_pressure_app/model/blood_pressure/pressure_unit.dart';
 import 'package:blood_pressure_app/model/horizontal_graph_line.dart';
 import 'package:blood_pressure_app/model/storage/convert_util.dart';
 import 'package:flutter/material.dart';
@@ -43,6 +44,7 @@ class Settings extends ChangeNotifier {
     bool? bottomAppBars,
     List<Medicine>? medications,
     int? highestMedIndex,
+    PressureUnit? preferredPressureUnit,
   }) {
     if (accentColor != null) _accentColor = accentColor;
     if (sysColor != null) _sysColor = sysColor;
@@ -67,6 +69,7 @@ class Settings extends ChangeNotifier {
     if (bottomAppBars != null) _bottomAppBars = bottomAppBars;
     if (medications != null) _medications.addAll(medications);
     if (highestMedIndex != null) _highestMedIndex = highestMedIndex;
+    if (preferredPressureUnit != null) _preferredPressureUnit = preferredPressureUnit;
     _language = language; // No check here, as null is the default as well.
   }
 
@@ -99,6 +102,7 @@ class Settings extends ChangeNotifier {
       medications: ConvertUtil.parseList<String>(map['medications'])?.map((e) =>
           Medicine.fromJson(jsonDecode(e)),).toList(),
       highestMedIndex: ConvertUtil.parseInt(map['highestMedIndex']),
+      preferredPressureUnit: PressureUnit.decode(ConvertUtil.parseInt(map['preferredPressureUnit'])),
     );
 
     // update
@@ -143,6 +147,7 @@ class Settings extends ChangeNotifier {
       'bottomAppBars': bottomAppBars,
       'medications': medications.map(jsonEncode).toList(),
       'highestMedIndex': highestMedIndex,
+      'preferredPressureUnit': preferredPressureUnit,
     };
 
   /// Serialize the object to a restoreable string.
@@ -339,6 +344,14 @@ class Settings extends ChangeNotifier {
     _bottomAppBars = value;
     notifyListeners();
   }
+
+  PressureUnit _preferredPressureUnit = PressureUnit.mmHg;
+  /// Preferred unit to display and enter measurements in.
+  PressureUnit get preferredPressureUnit => _preferredPressureUnit;
+  set preferredPressureUnit(PressureUnit value) {
+    _preferredPressureUnit = value;
+    notifyListeners();
+  }
   
   final List<Medicine> _medications = [];
   /// All medications ever added.