Commit 48c3047

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-06-14 15:57:52
add utility methods for entry lists
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 9e19b57
Changed files (3)
health_data_store
health_data_store/lib/src/types/full_entry.dart
@@ -1,4 +1,23 @@
 import 'package:health_data_store/health_data_store.dart';
 
-/// Entry that contains data of all supported types.
+/// Utility collection that containing all types that make sense at one point in
+/// time.
 typedef FullEntry = (BloodPressureRecord, Note, List<MedicineIntake>);
+
+/// Utility methods to work on full entries.
+extension FullEntryListUtils on List<FullEntry> {
+  /// Create a list that only contains the records field from the entries.
+  List<BloodPressureRecord> get records => map((e) => e.$1).toList();
+
+  /// Create a list that only contains the note field from the entries.
+  List<Note> get notes => map((e) => e.$2).toList();
+
+  /// Get all medicines that appear anywhere in the list.
+  List<Medicine> get distinctMedicines {
+    final Set<Medicine> meds = Set();
+    forEach((e) => e.$3.forEach((m) {
+      meds.add(m.medicine);
+    }));
+    return meds.toList();
+  }
+}
health_data_store/test/src/types/full_entry_test.dart
@@ -29,4 +29,54 @@ void main() {
     expect(entry.$2, note);
     expect(entry.$3, isEmpty);
   });
+  test('extracts records', () {
+    final record1 = mockRecord(time: 1);
+    final record2 = mockRecord(time: 2);
+    final record3 = mockRecord(time: 3);
+    final note = mockNote();
+
+    final List<FullEntry> list = [
+      (record1, note, []),
+      (record2, note, []),
+      (record3, note, []),
+      (record2, note, []),
+    ];
+
+    expect(
+      list.records,
+      containsAllInOrder([record1, record2, record3, record2]),
+    );
+  });
+  test('extracts notes', () {
+    final record = mockRecord();
+    final note1 = mockNote(note: 'a');
+    final note2 = mockNote(note: 'b');
+    final note3 = mockNote(note: 'c');
+
+    final List<FullEntry> list = [
+      (record, note1, []),
+      (record, note2, []),
+      (record, note3, []),
+      (record, note2, []),
+    ];
+
+    expect(list.notes, containsAllInOrder([note1, note2, note3, note2]));
+  });
+  test('detects correct distinct medicines', () {
+    final record = mockRecord();
+    final note = mockNote();
+
+    final med1 = mockMedicine();
+    final med2 = mockMedicine();
+    final med3 = mockMedicine();
+
+    final List<FullEntry> list = [
+      (record, note, [mockIntake(med1),mockIntake(med2)]),
+      (record, note, [mockIntake(med1)]),
+      (record, note, []),
+      (record, note, [mockIntake(med3), mockIntake(med1)]),
+    ];
+
+    expect(list.distinctMedicines, containsAll([med1, med2, med3]));
+  });
 }
health_data_store/test/src/types/medicine_test.dart
@@ -12,6 +12,7 @@ void main() {
   });
 }
 
+/// Creates a new medicine with a random designation if none is specified.
 Medicine mockMedicine({
   String? designation,
   double? dosis,