Commit 6984347
Changed files (2)
lib
model
export_import
storage
lib/model/export_import/column.dart
@@ -1,17 +1,11 @@
+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_gen/gen_l10n/app_localizations.dart';
-/// Class for storing export behavior of columns.
-class ExportColumn implements Formatter {
- /// Create a object that handles export behavior for data in a column.
- ///
- /// [formatter] will be created according to [formatString].
- ExportColumn(this.internalIdentifier, this.csvTitle, String formatString) {
- formatter = ScriptedFormatter(formatString);
- }
-
+sealed class ExportColumn implements Formatter {
/// Unique internal identifier that is used to identify a column in the app.
///
/// A identifier can be any string, but is usually structured with a prefix and
@@ -20,16 +14,49 @@ class ExportColumn implements Formatter {
/// app.
///
/// It should not be used instead of [csvTitle].
- final String internalIdentifier;
+ String get internalIdentifier;
/// Column title in a csv file.
///
/// May not contain characters intended for CSV column separation (e.g. `,`).
- final String csvTitle;
+ String get csvTitle;
/// Column title in user facing places that don't require strict rules.
///
/// 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
+class UserColumn extends ExportColumn {
+ /// Create a object that handles export behavior for data in a column.
+ ///
+ /// [formatter] will be created according to [formatString].
+ UserColumn(this.internalIdentifier, this.csvTitle, String formatString) {
+ formatter = ScriptedFormatter(formatString);
+ }
+
+ @override
+ final String internalIdentifier;
+
+ @override
+ final String csvTitle;
+
+ @override
String userTitle(AppLocalizations localizations) => csvTitle;
/// Converter associated with this column.
lib/model/storage/export_columns_store.dart
@@ -0,0 +1,40 @@
+import 'dart:collection';
+import 'dart:convert';
+
+import 'package:blood_pressure_app/model/export_import/column.dart';
+import 'package:flutter/material.dart';
+
+class ExportColumnsManager extends ChangeNotifier {
+ static const List<String> reservedNamespaces = ['buildIn', 'myHeart'];
+
+ /// Map between all [ExportColumn.internalIdentifier]s and [ExportColumn]s added by a user.
+ final Map<String, ExportColumn> _userColumns = {};
+
+ /// View of map between all [ExportColumn.internalName]s and [ExportColumn]s added by a user.
+ UnmodifiableMapView<String, ExportColumn> get userColumns => UnmodifiableMapView(_userColumns);
+
+ /// Tries to save the column to the map with the [ExportColumn.internalName] key.
+ ///
+ /// This method fails and returns false when there is a default [ExportColumn] with the same internal name is
+ /// available.
+ bool addOrUpdate(ExportColumn column) {
+ if (reservedNamespaces.any((element) => column.internalIdentifier.startsWith(element)) return false;
+ _userColumns[column.internalIdentifier] = column;
+ notifyListeners();
+ return true;
+ }
+
+ /// Deletes a [ExportColumn] the user added.
+ ///
+ /// Calling this with the [ExportColumnI.internalIdentifier] of build-in columns
+ /// or undefined columns will have no effect.
+ void deleteUserColumn(String identifier) {
+ assert(_userColumns.containsKey(identifier), 'Don\'t call for non user columns');
+ _userColumns.remove(identifier);
+ }
+
+ String toJson() {
+ // TODO
+ throw UnimplementedError();
+ }
+}
\ No newline at end of file