Commit a40f1a3

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-12-22 17:33:59
update color and needle pin scripted parsing
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 0961a74
Changed files (4)
lib/model/export_import/csv_converter.dart
@@ -7,7 +7,6 @@ import 'package:blood_pressure_app/model/storage/export_columns_store.dart';
 import 'package:blood_pressure_app/model/storage/export_csv_settings_store.dart';
 import 'package:collection/collection.dart';
 import 'package:csv/csv.dart';
-import 'package:flutter/material.dart';
 
 /// Utility class to convert between csv strings and [BloodPressureRecord]s.
 class CsvConverter {
@@ -111,11 +110,6 @@ class CsvConverter {
               (piece) => piece.$1 == RowDataFieldType.notes)?.$2 ?? '';
       MeasurementNeedlePin? needlePin = recordPieces.firstWhereOrNull(
               (piece) => piece.$1 == RowDataFieldType.needlePin)?.$2;
-      if (needlePin == null) {
-        final Color? color = recordPieces.firstWhereOrNull(
-                (piece) => piece.$1 == RowDataFieldType.color)?.$2;
-        if (color != null) needlePin = MeasurementNeedlePin(color);
-      }
 
       records.add(BloodPressureRecord(timestamp, sys, dia, pul, note, needlePin: needlePin));
       currentLineNumber++;
lib/model/export_import/import_field_type.dart
@@ -18,7 +18,7 @@ enum RowDataFieldType {
   /// Guarantees [String] is returned.
   notes,
   @Deprecated('use needlePin instead. Can be removed in code as all colors can be expressed as needle pins')
-  /// Guarantees [Color] is returned.
+  /// Guarantees [MeasurementNeedlePin] is returned.
   color,
   /// Guarantees that the returned type is of type [MeasurementNeedlePin].
   needlePin;
lib/model/export_import/record_formatter.dart
@@ -1,3 +1,5 @@
+import 'dart:convert';
+
 import 'package:blood_pressure_app/model/blood_pressure.dart';
 import 'package:blood_pressure_app/model/export_import/import_field_type.dart';
 import 'package:flutter/material.dart';
@@ -53,13 +55,16 @@ class ScriptedFormatter implements Formatter {
       case RowDataFieldType.notes:
         return text;
       case RowDataFieldType.color:
-        final num = int.tryParse(text);
-        if (num != null) return Color(num);
-        return null;
+        assert(false);
       case RowDataFieldType.needlePin:
         final num = int.tryParse(text);
-        if (num == null) return null;
-        return MeasurementNeedlePin(Color(num));
+        if (num != null) return MeasurementNeedlePin(Color(num));
+        try {
+          return MeasurementNeedlePin.fromJson(jsonDecode(text));
+        } catch (e) {
+          assert(e is FormatException || e is TypeError);
+          return null;
+        }
     }}();
     if (value != null) return (restoreAbleType!, value);
     return null;
@@ -75,7 +80,7 @@ class ScriptedFormatter implements Formatter {
     fieldContents = fieldContents.replaceAll(r'$DIA', record.diastolic.toString());
     fieldContents = fieldContents.replaceAll(r'$PUL', record.pulse.toString());
     fieldContents = fieldContents.replaceAll(r'$NOTE', record.notes.toString());
-    fieldContents = fieldContents.replaceAll(r'$COLOR', record.needlePin?.color.value.toString() ?? '');
+    fieldContents = fieldContents.replaceAll(r'$COLOR', jsonEncode(record.needlePin?.toJson()));
 
     // math
     fieldContents = fieldContents.replaceAllMapped(RegExp(r'\{\{([^}]*)}}'), (m) {
@@ -123,7 +128,7 @@ class ScriptedFormatter implements Formatter {
       } else if (pattern == r'$TIMESTAMP') {
         _restoreAbleType = RowDataFieldType.timestamp;
       } else if (pattern == r'$COLOR') {
-        _restoreAbleType = RowDataFieldType.color;
+        _restoreAbleType = RowDataFieldType.needlePin;
       } else if (pattern == r'$NOTE') {
         _restoreAbleType = RowDataFieldType.notes;
       } else if (replaced.contains(RegExp(r'[^{},$]*\$(PUL|DIA|SYS)[^{},$]*'))) {
test/model/export_import/record_formatter_test.dart
@@ -1,3 +1,5 @@
+import 'dart:convert';
+
 import 'package:blood_pressure_app/model/blood_pressure.dart';
 import 'package:blood_pressure_app/model/export_import/import_field_type.dart';
 import 'package:blood_pressure_app/model/export_import/record_formatter.dart';
@@ -23,7 +25,7 @@ void main() {
       expect(ScriptedFormatter(r'$SYS',).encode(testRecord), testRecord.systolic.toString());
       expect(ScriptedFormatter(r'$DIA',).encode(testRecord), testRecord.diastolic.toString());
       expect(ScriptedFormatter(r'$PUL',).encode(testRecord), testRecord.pulse.toString());
-      expect(ScriptedFormatter(r'$COLOR',).encode(testRecord), testRecord.needlePin!.color.value.toString());
+      expect(ScriptedFormatter(r'$COLOR',).encode(testRecord), jsonEncode(testRecord.needlePin!.toJson()));
       expect(ScriptedFormatter(r'$NOTE',).encode(testRecord), testRecord.notes);
       expect(ScriptedFormatter(r'$TIMESTAMP',).encode(testRecord), testRecord.creationTime.millisecondsSinceEpoch.toString());
       expect(ScriptedFormatter(r'$SYS$DIA$PUL',).encode(testRecord), (testRecord.systolic.toString()
@@ -46,7 +48,7 @@ void main() {
       expect(ScriptedFormatter(r'$PUL',).restoreAbleType, RowDataFieldType.pul);
       expect(ScriptedFormatter(r'$TIMESTAMP',).restoreAbleType, RowDataFieldType.timestamp);
       expect(ScriptedFormatter(r'$NOTE',).restoreAbleType, RowDataFieldType.notes);
-      expect(ScriptedFormatter(r'$COLOR',).restoreAbleType, RowDataFieldType.color);
+      expect(ScriptedFormatter(r'$COLOR',).restoreAbleType, RowDataFieldType.needlePin);
       expect(ScriptedFormatter(r'test$SYS123',).restoreAbleType, RowDataFieldType.sys);
       expect(ScriptedFormatter(r'test$DIA123',).restoreAbleType, RowDataFieldType.dia);
       expect(ScriptedFormatter(r'test$PUL123',).restoreAbleType, RowDataFieldType.pul);
@@ -66,9 +68,9 @@ void main() {
       final encodedPurple = ScriptedFormatter(r'$COLOR',)
           .encode(BloodPressureRecord(DateTime.now(), null, null, null, '',
           needlePin: const MeasurementNeedlePin(Colors.purple)));
-      expect(ScriptedFormatter(r'$COLOR',).decode(encodedPurple)?.$1, RowDataFieldType.color);
-      expect(ScriptedFormatter(r'$COLOR',).decode(encodedPurple)?.$2, isA<Color>()
-          .having((p0) => p0.value, 'color', Colors.purple.value));
+      expect(ScriptedFormatter(r'$COLOR',).decode(encodedPurple)?.$1, RowDataFieldType.needlePin);
+      expect(ScriptedFormatter(r'$COLOR',).decode(encodedPurple)?.$2, isA<MeasurementNeedlePin>()
+          .having((p0) => p0.color.value, 'color', Colors.purple.value));
       expect(ScriptedFormatter(r'test$SYS',).decode('test567'), (RowDataFieldType.sys, 567));
       expect(ScriptedFormatter(r'test$SYS123',).decode('test567123'), (RowDataFieldType.sys, 567));
       expect(ScriptedFormatter(r'test$DIA123',).decode('test567123'), (RowDataFieldType.dia, 567));
@@ -96,6 +98,13 @@ void main() {
       expect(ScriptedFormatter(r'($SYS',).decode('(123'), (RowDataFieldType.sys, 123));
       expect(ScriptedFormatter(r'($NOTE',).decode('(test'), null);
     });
+    test('should needlepin ignore invalid json', () {
+      expect(ScriptedFormatter(r'$COLOR',).decode(''), null);
+      expect(ScriptedFormatter(r'$COLOR',).decode('null'), null);
+      expect(ScriptedFormatter(r'$COLOR',).decode('{test'), null);
+      expect(ScriptedFormatter(r'$COLOR',).decode('{"test": 1}'), null);
+      expect(ScriptedFormatter(r'$COLOR',).decode('{}'), null);
+    });
   });
 
   group('ScriptedTimeFormatter', () {