Commit cd77c96

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-10-03 16:55:48
save other settings types in dao
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 893b4cc
Changed files (3)
lib/model/storage/db/config_dao.dart
@@ -1,5 +1,8 @@
 import 'package:blood_pressure_app/model/export_options.dart';
 import 'package:blood_pressure_app/model/storage/db/config_db.dart';
+import 'package:blood_pressure_app/model/storage/export_csv_settings_store.dart';
+import 'package:blood_pressure_app/model/storage/export_pdf_settings_store.dart';
+import 'package:blood_pressure_app/model/storage/export_settings_store.dart';
 import 'package:blood_pressure_app/model/storage/settings_store.dart';
 import 'package:sqflite/sqflite.dart';
 
@@ -52,6 +55,138 @@ class ConfigDao {
     );
   }
 
+  /// Loads the profiles [ExportSettings] object from the database.
+  ///
+  /// If any errors occur or the object is not present, a default one will be created. Changes in the object
+  /// will save to the database automatically (a listener gets attached).
+  Future<ExportSettings> loadExportSettings(int profileID) async {
+    final dbEntry = await _configDB.database.query(
+        ConfigDB.exportSettingsTable,
+        columns: ['json'],
+        where: 'profile_id = ?',
+        whereArgs: [profileID]
+    );
+
+    late final ExportSettings exportSettings;
+    if (dbEntry.isEmpty) {
+      exportSettings = ExportSettings();
+    } else {
+      assert(dbEntry.length == 1, 'The profile_id should be unique.');
+      final settingsJson = dbEntry.first['json'];
+      if (settingsJson == null) {
+        exportSettings = ExportSettings();
+      } else {
+        exportSettings = ExportSettings.fromJson(settingsJson.toString());
+      }
+    }
+    exportSettings.addListener(() {
+      _updateExportSettings(profileID, exportSettings);
+    });
+    return exportSettings;
+  }
+
+  /// Update [ExportSettings] for a profile in the database.
+  ///
+  /// Adds an entry if necessary.
+  Future<void> _updateExportSettings(int profileID, ExportSettings settings) async {
+    await _configDB.database.insert(
+        ConfigDB.exportSettingsTable,
+        {
+          'profile_id': profileID,
+          'json': settings.toJson()
+        },
+        conflictAlgorithm: ConflictAlgorithm.replace
+    );
+  }
+
+  /// Loads the profiles [CsvExportSettings] object from the database.
+  ///
+  /// If any errors occur or the object is not present, a default one will be created. Changes in the object
+  /// will save to the database automatically (a listener gets attached).
+  Future<CsvExportSettings> loadCsvExportSettings(int profileID) async {
+    final dbEntry = await _configDB.database.query(
+        ConfigDB.exportCsvSettingsTable,
+        columns: ['json'],
+        where: 'profile_id = ?',
+        whereArgs: [profileID]
+    );
+
+    late final CsvExportSettings exportSettings;
+    if (dbEntry.isEmpty) {
+      exportSettings = CsvExportSettings();
+    } else {
+      assert(dbEntry.length == 1, 'The profile_id should be unique.');
+      final settingsJson = dbEntry.first['json'];
+      if (settingsJson == null) {
+        exportSettings = CsvExportSettings();
+      } else {
+        exportSettings = CsvExportSettings.fromJson(settingsJson.toString());
+      }
+    }
+    exportSettings.addListener(() {
+      _updateCsvExportSettings(profileID, exportSettings);
+    });
+    return exportSettings;
+  }
+
+  /// Update [CsvExportSettings] for a profile in the database.
+  ///
+  /// Adds an entry if necessary.
+  Future<void> _updateCsvExportSettings(int profileID, CsvExportSettings settings) async {
+    await _configDB.database.insert(
+        ConfigDB.exportCsvSettingsTable,
+        {
+          'profile_id': profileID,
+          'json': settings.toJson()
+        },
+        conflictAlgorithm: ConflictAlgorithm.replace
+    );
+  }
+
+  /// Loads the profiles [PdfExportSettings] object from the database.
+  ///
+  /// If any errors occur or the object is not present, a default one will be created. Changes in the object
+  /// will save to the database automatically (a listener gets attached).
+  Future<PdfExportSettings> loadPdfExportSettings(int profileID) async {
+    final dbEntry = await _configDB.database.query(
+        ConfigDB.exportPdfSettingsTable,
+        columns: ['json'],
+        where: 'profile_id = ?',
+        whereArgs: [profileID]
+    );
+
+    late final PdfExportSettings exportSettings;
+    if (dbEntry.isEmpty) {
+      exportSettings = PdfExportSettings();
+    } else {
+      assert(dbEntry.length == 1, 'The profile_id should be unique.');
+      final settingsJson = dbEntry.first['json'];
+      if (settingsJson == null) {
+        exportSettings = PdfExportSettings();
+      } else {
+        exportSettings = PdfExportSettings.fromJson(settingsJson.toString());
+      }
+    }
+    exportSettings.addListener(() {
+      _updatePdfExportSettings(profileID, exportSettings);
+    });
+    return exportSettings;
+  }
+
+  /// Update [PdfExportSettings] for a profile in the database.
+  ///
+  /// Adds an entry if necessary.
+  Future<void> _updatePdfExportSettings(int profileID, PdfExportSettings settings) async {
+    await _configDB.database.insert(
+        ConfigDB.exportPdfSettingsTable,
+        {
+          'profile_id': profileID,
+          'json': settings.toJson()
+        },
+        conflictAlgorithm: ConflictAlgorithm.replace
+    );
+  }
+
   // TODO: test if custom export columns still work
   Future<List<ExportColumn>> loadExportColumns() async {
     final existingDbEntries = await _configDB.database.query(
lib/model/storage/db/config_db.dart
@@ -27,12 +27,44 @@ class ConfigDB {
   static const String _settingsTableCreationString =
       'CREATE TABLE settings(profile_id: INTEGER PRIMARY KEY, settings_json: STRING)';
 
+  /// Name of the export settings table.
+  ///
+  /// It is used to store json representations of [ExportSettings] objects. Data is saved the same way as with
+  /// [settingsTable]
+  ///
+  /// Format:
+  /// `CREATE TABLE exportSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)`
+  static const String exportSettingsTable = 'exportSettings';
+  static const String _exportSettingsTableCreationString =
+      'CREATE TABLE exportSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)';
+
+  /// Name of the table for csv export settings.
+  ///
+  /// It is used to store json representations of [CsvExportSettings] objects. Data is saved the same way as with
+  /// [settingsTable]
+  ///
+  /// Format:
+  /// `CREATE TABLE exportCsvSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)`
+  static const String exportCsvSettingsTable = 'exportCsvSettings';
+  static const String _exportCsvSettingsTableCreationString =
+      'CREATE TABLE exportCsvSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)';
+
+  /// Name of the table for pdf export settings.
+  ///
+  /// It is used to store json representations of [PdfExportSettings] objects. Data is saved the same way as with
+  /// [settingsTable]
+  ///
+  /// Format:
+  /// `CREATE TABLE exportPdfSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)`
+  static const String exportPdfSettingsTable = 'exportPdfSettings';
+  static const String _exportPdfSettingsTableCreationString =
+      'CREATE TABLE exportPdfSettings(profile_id: INTEGER PRIMARY KEY, json: STRING)';
+
   /// Name of the exportStrings table. It is used to store formats used in the [ExportConfigurationModel].
   ///
   /// Format:
   /// `CREATE TABLE exportStrings(internalColumnName STRING PRIMARY KEY, columnTitle STRING, formatPattern STRING)`
   static const String exportStringsTable = 'exportStrings';
-  // instead of just changing this string when changing the format, _onDBUpgrade should be used.
   static const String _exportStringsTableCreationString =
       'CREATE TABLE exportStrings(internalColumnName STRING PRIMARY KEY, columnTitle STRING, formatPattern STRING)';
 
@@ -57,6 +89,9 @@ class ConfigDB {
   FutureOr<void> _onDBCreate(Database db, int version) {
     db.execute(_exportStringsTableCreationString);
     db.execute(_settingsTableCreationString);
+    db.execute(_exportSettingsTableCreationString);
+    db.execute(_exportCsvSettingsTableCreationString);
+    db.execute(_exportPdfSettingsTableCreationString);
   }
 
   FutureOr<void> _onDBUpgrade(Database db, int oldVersion, int newVersion) async {
@@ -64,6 +99,9 @@ class ConfigDB {
     // might be useful, to avoid duplicated code. Currently this would only lead to complexity, without benefits.
     if (oldVersion == 1 && newVersion == 2) {
       db.execute(_settingsTableCreationString);
+      db.execute(_exportSettingsTableCreationString);
+      db.execute(_exportCsvSettingsTableCreationString);
+      db.execute(_exportPdfSettingsTableCreationString);
       db.database.setVersion(2);
     } else {
       assert(false, 'Unexpected version upgrade from $oldVersion to $newVersion.');
lib/model/storage/intervall_store.dart
@@ -10,7 +10,7 @@ class IntervallStorage extends ChangeNotifier {
     _stepSize = stepSize ?? TimeStep.last7Days;
     _currentRange = range ?? _getMostRecentDisplayIntervall();
   }
-  
+   //TODO: add to database
   late TimeStep _stepSize;
   late DateTimeRange _currentRange;