Commit 41a0668

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-03-16 14:05:24
implement DataRange type
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 1af3099
Changed files (3)
health_data_store
health_data_store/lib/src/types/date_range.dart
@@ -0,0 +1,27 @@
+import 'package:freezed_annotation/freezed_annotation.dart';
+
+part 'date_range.freezed.dart';
+
+/// Encapsulates a start and end [DateTime] that represent the range of dates.
+///
+/// The range includes the [start] and [end] dates. The [start] and [end] dates
+/// may be equal to indicate a date range of a single day. The [start] date must
+/// not be after the [end] date.
+@freezed
+class DateRange with _$DateRange {
+  // ignore: unused_element,
+  const DateRange._();
+
+  /// Creates a date range for the given start and end [DateTime].
+  const factory DateRange({
+    /// The start of the range of dates.
+    required DateTime start,
+    /// The end of the range of dates.
+    required DateTime end,
+  }) = _DateRange;
+
+  /// Returns a [Duration] of the time between [start] and [end].
+  ///
+  /// See [DateTime.difference] for more details.
+  Duration get duration => end.difference(start);
+}
health_data_store/lib/health_data_store.dart
@@ -2,3 +2,8 @@
 library;
 
 export 'src/health_data_store.dart';
+export 'src/types/blood_pressure_record.dart';
+export 'src/types/date_range.dart';
+export 'src/types/medicine.dart';
+export 'src/types/medicine_intake.dart';
+export 'src/types/note.dart';
health_data_store/test/src/types/date_range_test.dart
@@ -0,0 +1,18 @@
+import 'package:health_data_store/health_data_store.dart';
+import 'package:test/test.dart';
+
+void main() {
+  test('should initialize', () {
+    final timeA = DateTime.now();
+    final timeB = timeA.subtract(Duration(hours: 20));
+    final range = DateRange(start: timeA, end: timeB);
+    expect(range.start, equals(timeA));
+    expect(range.end, equals(timeB));
+  });
+  test('should calculate difference', () {
+    final timeA = DateTime.now();
+    final timeB = timeA.subtract(Duration(hours: 21, seconds: 42));
+    final range = DateRange(start: timeA, end: timeB);
+    expect(range.duration, equals(Duration(hours: -21, seconds: -42)));
+  });
+}