main
1import 'package:blood_pressure_app/model/storage/db/config_db.dart';
2import 'package:blood_pressure_app/model/storage/db/settings_loader.dart';
3import 'package:blood_pressure_app/model/storage/export_columns_store.dart';
4import 'package:blood_pressure_app/model/storage/export_csv_settings_store.dart';
5import 'package:blood_pressure_app/model/storage/export_pdf_settings_store.dart';
6import 'package:blood_pressure_app/model/storage/export_settings_store.dart';
7import 'package:blood_pressure_app/model/storage/export_xsl_settings_store.dart';
8import 'package:blood_pressure_app/model/storage/interval_store.dart';
9import 'package:blood_pressure_app/model/storage/settings_store.dart';
10
11/// Class for loading data from the database.
12///
13/// The user of this class needs to pay attention to dispose all old instance
14/// of objects created by the instance methods in order to ensure there are no
15/// concurrent writes to the database. Having multiple instance will cause data
16/// loss because states are not synced again after one changes.
17///
18/// The load... methods have to schedule a initial save to db in case an
19/// migration / update of fields occurred.
20@deprecated
21class ConfigDao implements SettingsLoader {
22 /// Create a serializer to initialize data from a database.
23 ConfigDao(this._configDB);
24
25 final ConfigDB _configDB;
26
27 Settings? _settingsInstance = null;
28 @override
29 Future<Settings> loadSettings() async {
30 if (_settingsInstance != null) return _settingsInstance!;
31 final dbEntry = await _configDB.database.query(
32 ConfigDB.settingsTable,
33 columns: ['settings_json'],
34 where: 'profile_id = ?',
35 whereArgs: [0],
36 );
37
38 late final Settings settings;
39 if (dbEntry.isEmpty) {
40 settings = Settings();
41 } else {
42 assert(dbEntry.length == 1, 'The profile_id should be unique.');
43 final settingsJson = dbEntry.first['settings_json'];
44 if (settingsJson == null) {
45 settings = Settings();
46 } else {
47 settings = Settings.fromJson(settingsJson.toString());
48 }
49 }
50 _settingsInstance = settings;
51 return settings;
52 }
53
54 ExportSettings? _exportSettingsInstance = null;
55 @override
56 Future<ExportSettings> loadExportSettings() async {
57 if (_exportSettingsInstance != null) return _exportSettingsInstance!;
58 final dbEntry = await _configDB.database.query(
59 ConfigDB.exportSettingsTable,
60 columns: ['json'],
61 where: 'profile_id = ?',
62 whereArgs: [0],
63 );
64
65 late final ExportSettings exportSettings;
66 if (dbEntry.isEmpty) {
67 exportSettings = ExportSettings();
68 } else {
69 assert(dbEntry.length == 1, 'The profile_id should be unique.');
70 final settingsJson = dbEntry.first['json'];
71 if (settingsJson == null) {
72 exportSettings = ExportSettings();
73 } else {
74 exportSettings = ExportSettings.fromJson(settingsJson.toString());
75 }
76 }
77 _exportSettingsInstance = exportSettings;
78 return exportSettings;
79 }
80
81 CsvExportSettings? _csvExportSettingsInstance = null;
82 @override
83 Future<CsvExportSettings> loadCsvExportSettings() async {
84 if (_csvExportSettingsInstance != null) return _csvExportSettingsInstance!;
85 final dbEntry = await _configDB.database.query(
86 ConfigDB.exportCsvSettingsTable,
87 columns: ['json'],
88 where: 'profile_id = ?',
89 whereArgs: [0],
90 );
91
92 late final CsvExportSettings exportSettings;
93 if (dbEntry.isEmpty) {
94 exportSettings = CsvExportSettings();
95 } else {
96 assert(dbEntry.length == 1, 'The profile_id should be unique.');
97 final settingsJson = dbEntry.first['json'];
98 if (settingsJson == null) {
99 exportSettings = CsvExportSettings();
100 } else {
101 exportSettings = CsvExportSettings.fromJson(settingsJson.toString());
102 }
103 }
104 _csvExportSettingsInstance = exportSettings;
105 return exportSettings;
106 }
107
108 PdfExportSettings? _pdfExportSettingsInstance = null;
109 @override
110 Future<PdfExportSettings> loadPdfExportSettings() async {
111 if (_pdfExportSettingsInstance != null) return _pdfExportSettingsInstance!;
112 final dbEntry = await _configDB.database.query(
113 ConfigDB.exportPdfSettingsTable,
114 columns: ['json'],
115 where: 'profile_id = ?',
116 whereArgs: [0],
117 );
118
119 late final PdfExportSettings exportSettings;
120 if (dbEntry.isEmpty) {
121 exportSettings = PdfExportSettings();
122 } else {
123 assert(dbEntry.length == 1, 'The profile_id should be unique.');
124 final settingsJson = dbEntry.first['json'];
125 if (settingsJson == null) {
126 exportSettings = PdfExportSettings();
127 } else {
128 exportSettings = PdfExportSettings.fromJson(settingsJson.toString());
129 }
130 }
131 _pdfExportSettingsInstance = exportSettings;
132 return exportSettings;
133 }
134
135 IntervalStoreManager? _intervallStorageInstance;
136 @override
137 Future<IntervalStoreManager> loadIntervalStorageManager() async {
138 _intervallStorageInstance ??= IntervalStoreManager(
139 await _loadStore(0),
140 await _loadStore(1),
141 await _loadStore(2),
142 );
143 return _intervallStorageInstance!;
144 }
145
146 Future<IntervalStorage> _loadStore(int storageID) async {
147 final dbEntry = await _configDB.database.query(
148 ConfigDB.selectedIntervalStorageTable,
149 columns: ['stepSize', 'start', 'end'],
150 where: 'profile_id = ? AND storage_id = ?',
151 whereArgs: [0, storageID],
152 );
153 late final IntervalStorage intervallStorage;
154 if (dbEntry.isEmpty) {
155 intervallStorage = IntervalStorage();
156 } else {
157 assert(dbEntry.length == 1, 'Keys should ensure only one entry is possible.');
158 intervallStorage = IntervalStorage.fromMap(dbEntry.first);
159 }
160 return intervallStorage;
161 }
162
163 ExportColumnsManager? _exportColumnsManagerInstance = null;
164 @override
165 Future<ExportColumnsManager> loadExportColumnsManager() async {
166 if (_exportColumnsManagerInstance != null) return _exportColumnsManagerInstance!;
167 final dbEntry = await _configDB.database.query(
168 ConfigDB.exportColumnsTable,
169 columns: ['json'],
170 where: 'profile_id = ?',
171 whereArgs: [0],
172 );
173
174 late final ExportColumnsManager columnsManager;
175 if (dbEntry.isEmpty) {
176 columnsManager = ExportColumnsManager();
177 } else {
178 assert(dbEntry.length == 1, 'The profile_id should be unique.');
179 final json = dbEntry.first['json'];
180 if (json == null) {
181 columnsManager = ExportColumnsManager();
182 } else {
183 columnsManager = ExportColumnsManager.fromJson(json.toString());
184 }
185 }
186 _exportColumnsManagerInstance = columnsManager;
187 return columnsManager;
188 }
189
190 @override
191 Future<ExcelExportSettings> loadXslExportSettings() async =>
192 ExcelExportSettings(); // This was added after file settings
193}