Commit 56b1e82
Changed files (5)
lib
model
lib/model/export_import/column.dart
@@ -3,8 +3,103 @@ import 'dart:convert';
import 'package:blood_pressure_app/model/blood_pressure.dart';
import 'package:blood_pressure_app/model/export_import/legacy_column.dart';
import 'package:blood_pressure_app/model/export_import/reocord_formatter.dart';
+import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
+/// Converters for [BloodPressureRecord] attributes.
+class NativeColumn extends ExportColumn {
+ NativeColumn._create(this._csvTitle, this._restoreableType, this._encode, this._decode);
+
+ final String _csvTitle;
+ final RowDataFieldType _restoreableType;
+ final String Function(BloodPressureRecord record) _encode;
+ final Object? Function(String pattern) _decode;
+
+ static final NativeColumn timestamp = NativeColumn._create(
+ 'timestampUnixMs',
+ RowDataFieldType.timestamp,
+ (record) => record.creationTime.millisecondsSinceEpoch.toString(),
+ (pattern) {
+ final value = int.tryParse(pattern);
+ return (value == null) ? null : DateTime.fromMillisecondsSinceEpoch(value);
+ }
+ );
+ static final NativeColumn systolic = NativeColumn._create(
+ 'systolic',
+ RowDataFieldType.sys,
+ (record) => record.systolic.toString(),
+ (pattern) => int.tryParse(pattern)
+ );
+ static final NativeColumn diastolic = NativeColumn._create(
+ 'diastolic',
+ RowDataFieldType.dia,
+ (record) => record.diastolic.toString(),
+ (pattern) => int.tryParse(pattern)
+ );
+ static final NativeColumn pulse = NativeColumn._create(
+ 'notes',
+ RowDataFieldType.notes,
+ (record) => record.notes,
+ (pattern) => pattern
+ );
+ static final NativeColumn color = NativeColumn._create(
+ 'color',
+ RowDataFieldType.needlePin,
+ (record) => record.needlePin?.color.value.toString() ?? '',
+ (pattern) {
+ final value = int.tryParse(pattern);
+ if (value == null) return null;
+ return MeasurementNeedlePin(Color(value));
+ }
+ );
+ static final NativeColumn needlePin = NativeColumn._create(
+ 'needlePin',
+ RowDataFieldType.needlePin,
+ (record) => jsonEncode(record.needlePin?.toJson()),
+ (pattern) {
+ final json = jsonDecode(pattern);
+ if (json is! Map<String, dynamic>) return null;
+ try {
+ return MeasurementNeedlePin.fromJson(json);
+ } on FormatException {
+ return null;
+ }
+ }
+ );
+
+ @override
+ String get csvTitle => _csvTitle;
+
+ @override
+ (RowDataFieldType, Object)? decode(String pattern) {
+ final value = _decode(pattern);
+ if (value == null) return null;
+ return (_restoreableType, value);
+ }
+
+ @override
+ String encode(BloodPressureRecord record) => _encode(record);
+
+ @override
+ String? get formatPattern => null;
+
+ @override
+ String get internalIdentifier => 'buildin.$csvTitle';
+
+ @override
+ RowDataFieldType? get restoreAbleType => _restoreableType;
+
+ @override
+ String userTitle(AppLocalizations localizations) => _restoreableType.localize(localizations);
+
+
+}
+
+// TODO: add class for formattedTimestamp
+// TODO: keep pulsePressure option
+
+
+/// Interface for converters that allow formatting and provide metadata.
sealed class ExportColumn implements Formatter {
/// Unique internal identifier that is used to identify a column in the app.
///
@@ -25,20 +120,9 @@ sealed class ExportColumn implements Formatter {
///
/// It will be displayed on the exported PDF file or in the column selection.
String userTitle(AppLocalizations localizations);
-
- static String serialize(ExportColumn column) {
- int type = switch (column) {
- UserColumn() => 1,
- };
- return jsonEncode({
- 't': type,
- 'id': column.internalIdentifier,
- 'csvTitle': column.csvTitle,
- // TODO: class specific json data?
- });
- }
}
+
/// Class for storing export behavior of columns.
///
/// In most cases using the sealed
@@ -73,4 +157,4 @@ class UserColumn extends ExportColumn {
@override
RowDataFieldType? get restoreAbleType => formatter.restoreAbleType;
-}
\ No newline at end of file
+}
lib/model/export_import/legacy_column.dart
@@ -82,7 +82,10 @@ class ExportColumn { // TODO: change this class so it implements the interface.
/// Type a [ExportColumn] can be parsed as.
enum RowDataFieldType {
- timestamp, sys, dia, pul, notes, color;
+ timestamp, sys, dia, pul, notes,
+ @Deprecated('use needlePin instead')
+ color,
+ needlePin;
String localize(AppLocalizations localizations) {
switch(this) {
@@ -98,6 +101,8 @@ enum RowDataFieldType {
return localizations.notes;
case RowDataFieldType.color:
return localizations.color;
+ case RowDataFieldType.needlePin:
+ return localizations.color;
}
}
}
\ No newline at end of file
lib/model/storage/export_columns_store.dart
@@ -4,7 +4,7 @@ import 'dart:convert';
import 'package:blood_pressure_app/model/export_import/column.dart';
import 'package:flutter/material.dart';
-class ExportColumnsManager extends ChangeNotifier {
+class ExportColumnsManager extends ChangeNotifier { // TODO: separate ExportColumnsManager for export and import ?
/// Create a new class for managing export columns.
///
/// It will be filled with the default columns but won't contain initial user columns.
@@ -52,6 +52,8 @@ class ExportColumnsManager extends ChangeNotifier {
'formatString': c.formatPattern
});
break;
+ case NativeColumn():
+ assert(false, 'User is currently not able to create native columns.');
}
}
return jsonEncode({'userColumns': columns});
lib/model/export_import.dart
@@ -131,6 +131,8 @@ class ExportFileCreator {
break;
case RowDataFieldType.color:
color ??= parsedRecordDataType.$2 as Color?;
+ case RowDataFieldType.needlePin:
+ color = (parsedRecordDataType.$2 as MeasurementNeedlePin?)?.color;
}
}
}
lib/model/export_options.dart
@@ -13,6 +13,7 @@ class ExportFields {
static const defaultPdf = ['formattedTimestamp','systolic','diastolic','pulse','notes'];
}
+@Deprecated('will get replaced with ExportColumnsManager')
class ExportConfigurationModel {
// 2 sources.
static ExportConfigurationModel? _instance;