main
1import 'package:blood_pressure_app/data_util/repository_builder.dart';
2import 'package:blood_pressure_app/model/storage/interval_store.dart';
3import 'package:flutter/material.dart';
4import 'package:health_data_store/health_data_store.dart';
5import 'package:provider/provider.dart';
6
7/// Shorthand class for getting a [rangeType]s [FullEntry] values.
8class FullEntryBuilder extends StatelessWidget {
9 /// Create a loader for getting a [rangeType]s [FullEntry] values.
10 ///
11 /// Provide either [onEntries] or [onData].
12 const FullEntryBuilder({
13 super.key,
14 this.onEntries,
15 this.onData,
16 required this.rangeType,
17 }) : assert((onEntries == null) != (onData == null), 'Provide either of the builders.');
18
19 /// Builder using a sorted list of full entries.
20 final Widget Function(BuildContext context, List<FullEntry> entries)? onEntries;
21
22 /// Builder using data from the main categories.
23 final Widget Function(BuildContext context, List<BloodPressureRecord> records, List<MedicineIntake> intakes, List<Note> notes)? onData;
24
25 /// Range type to load entries from.
26 final IntervalStoreManagerLocation rangeType;
27
28 @override
29 Widget build(BuildContext context) => RepositoryBuilder<BloodPressureRecord, BloodPressureRepository>(
30 rangeType: rangeType,
31 onData: (context, records) => RepositoryBuilder<MedicineIntake, MedicineIntakeRepository>(
32 rangeType: rangeType,
33 onData: (BuildContext context, List<MedicineIntake> intakes) => RepositoryBuilder<Note, NoteRepository>(
34 rangeType: rangeType,
35 onData: (BuildContext context, List<Note> notes) {
36 final manager = context.watch<IntervalStoreManager>();
37 final timeLimitRange = manager.get(rangeType).timeLimitRange;
38 if (timeLimitRange != null) {
39 records = records.where((r) {
40 final time = TimeOfDay.fromDateTime(r.time);
41 return time.isAfter(timeLimitRange.start) && time.isBefore(timeLimitRange.end);
42 }).toList();
43 intakes = intakes.where((i) {
44 final time = TimeOfDay.fromDateTime(i.time);
45 return time.isAfter(timeLimitRange.start) && time.isBefore(timeLimitRange.end);
46 }).toList();
47 notes = notes.where((n) {
48 final time = TimeOfDay.fromDateTime(n.time);
49 return time.isAfter(timeLimitRange.start) && time.isBefore(timeLimitRange.end);
50 }).toList();
51 }
52
53 if (onData != null) return onData!(context, records, intakes, notes);
54
55 final entries = FullEntryList.merged(records, notes, intakes);
56 entries.sort((a, b) => b.time.compareTo(a.time)); // newest first
57 return onEntries!(context, entries);
58 },
59 ),
60 ),
61 );
62}