Commit 62f679d

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-11-10 14:28:48
implement saving
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent bf29ec0
Changed files (2)
lib
components
model
lib/components/dialoges/add_measurement.dart
@@ -34,9 +34,24 @@ class AddMeasurementDialoge extends StatefulWidget {
 class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
   final formKey = GlobalKey<FormState>();
 
+  /// Currently selected time.
   late DateTime time;
+
+  /// Current selected needlePin.
   MeasurementNeedlePin? needlePin;
 
+  /// Last [FormState.save]d systolic value.
+  int? systolic;
+
+  /// Last [FormState.save]d diastolic value.
+  int? diastolic;
+
+  /// Last [FormState.save]d pulse value.
+  int? pulse;
+
+  /// Last [FormState.save]d note.
+  String? notes;
+
   @override
   void initState() {
     super.initState();
@@ -84,6 +99,7 @@ class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
   Widget buildValueInput(AppLocalizations localizations, {
     int? initialValue,
     String? hintText,
+    void Function(String?)? onSaved,
     // FocusNode? focusNode, TODO: check if works without
   }) {
     return Expanded(
@@ -91,6 +107,7 @@ class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
         initialValue: (initialValue ?? '').toString(),
         decoration: getInputDecoration(hintText),
         keyboardType: TextInputType.number,
+        onSaved: onSaved,
         inputFormatters: <TextInputFormatter>[FilteringTextInputFormatter.digitsOnly],
         onChanged: (String? value) {
           if (value != null && value.isNotEmpty && (int.tryParse(value) ?? -1) > 40) {
@@ -146,11 +163,10 @@ class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
           TextButton(
               onPressed: () {
                 if (formKey.currentState?.validate() ?? false) {
-                  // TODO: save; get values from form
+                  formKey.currentState?.save();
+                  final record = BloodPressureRecord(time, systolic, diastolic, pulse, notes ?? '', needlePin: needlePin);
+                  Navigator.of(context).pop(record);
                 }
-                /*if(inputs valid) {
-                  Navigator.of(context).pop(timeFormatFieldController.text);
-                }*/
               },
               child: Text(localizations.btnSave)
           )
@@ -169,16 +185,19 @@ class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
                 buildValueInput(localizations,
                   hintText: localizations.sysLong,
                   initialValue: widget.initialRecord?.systolic,
+                  onSaved: (value) => setState(() => systolic = int.tryParse(value ?? '')),
                 ),
                 const SizedBox(width: 10,),
                 buildValueInput(localizations,
                   hintText: localizations.diaLong,
                   initialValue: widget.initialRecord?.diastolic,
+                  onSaved: (value) => setState(() => diastolic = int.tryParse(value ?? '')),
                 ),
                 const SizedBox(width: 10,),
                 buildValueInput(localizations,
                   hintText: localizations.pulLong,
                   initialValue: widget.initialRecord?.pulse,
+                  onSaved: (value) => setState(() => pulse = int.tryParse(value ?? '')),
                 ),
               ],
             ),
@@ -189,6 +208,7 @@ class _AddMeasurementDialogeState extends State<AddMeasurementDialoge> {
                 decoration: getInputDecoration(localizations.addNote),
                 minLines: 1,
                 maxLines: 4,
+                onSaved: (value) => setState(() => notes = value),
               ),
             ),
             ColorSelectionListTile(
lib/model/blood_pressure.dart
@@ -166,7 +166,7 @@ class BloodPressureRecord {
 
   @override
   String toString() {
-    return 'BloodPressureRecord($creationTime, $systolic, $diastolic, $pulse, $notes)';
+    return 'BloodPressureRecord($creationTime, $systolic, $diastolic, $pulse, $notes, $needlePin)';
   }
 }
 
@@ -181,6 +181,11 @@ class MeasurementNeedlePin {
   Map<String, dynamic> toJson() => {
     'color': color.value,
   };
+
+  @override
+  String toString() {
+    return 'MeasurementNeedlePin{$color}';
+  }
 }
 
 // source: https://pressbooks.library.torontomu.ca/vitalsign/chapter/blood-pressure-ranges/ (last access: 20.05.2023)