Commit 6b39ce3
Changed files (1)
app
lib
screens
elements
app/lib/screens/elements/measurement_graph.dart
@@ -29,79 +29,79 @@ class _LineChartState extends State<_LineChart> {
height: widget.height,
child: RepositoryBuilder<MedicineIntake, MedicineIntakeRepository>(
rangeType: IntervallStoreManagerLocation.mainPage,
- onData: (context, List<dynamic> intakesData) => Consumer<Settings>(
- builder: (context, settings, child) => BloodPressureBuilder(
+ onData: (context, List<MedicineIntake> intakes) => Consumer<Settings>(
+ builder: (context, settings, child) => RepositoryBuilder<Note, NoteRepository>(
rangeType: IntervallStoreManagerLocation.mainPage,
- onData: (BuildContext context, UnmodifiableListView<BloodPressureRecord> records) {
- // TODO: Figure out why type safety isn't possible. (see home_screen for more info)
- final List<MedicineIntake> intakes = intakesData.cast();
+ onData: (context, List<Note> notes) => BloodPressureBuilder(
+ rangeType: IntervallStoreManagerLocation.mainPage,
+ onData: (BuildContext context, UnmodifiableListView<BloodPressureRecord> records) {
+ final List<BloodPressureRecord> data = records.toList();
+ data.sort((a, b) => a.time.compareTo(b.time));
- final List<BloodPressureRecord> data = records.toList();
- data.sort((a, b) => a.time.compareTo(b.time));
-
- // calculate lines for graph
- final pulSpots = <FlSpot>[];
- final diaSpots = <FlSpot>[];
- final sysSpots = <FlSpot>[];
- int maxValue = 0;
- final int minValue = (settings.validateInputs ? 30 : 0);
- /// Horizontally first value
- double? graphBegin;
- /// Horizontally last value
- double? graphEnd;
- for (final e in data) {
- final x = e.time.millisecondsSinceEpoch.toDouble();
- if (e.dia != null) {
- diaSpots.add(FlSpot(x, e.dia!.mmHg.toDouble()));
- maxValue = max(maxValue, e.dia!.mmHg);
- }
- if (e.sys != null) {
- sysSpots.add(FlSpot(x, e.sys!.mmHg.toDouble()));
- maxValue = max(maxValue, e.sys!.mmHg);
- }
- if (e.pul != null) {
- pulSpots.add(FlSpot(x, e.pul!.toDouble()));
- maxValue = max(maxValue, e.pul!);
+ // calculate lines for graph
+ final pulSpots = <FlSpot>[];
+ final diaSpots = <FlSpot>[];
+ final sysSpots = <FlSpot>[];
+ int maxValue = 0;
+ final int minValue = (settings.validateInputs ? 30 : 0);
+ /// Horizontally first value
+ double? graphBegin;
+ /// Horizontally last value
+ double? graphEnd;
+ for (final e in data) {
+ final x = e.time.millisecondsSinceEpoch.toDouble();
+ if (e.dia != null) {
+ diaSpots.add(FlSpot(x, e.dia!.mmHg.toDouble()));
+ maxValue = max(maxValue, e.dia!.mmHg);
+ }
+ if (e.sys != null) {
+ sysSpots.add(FlSpot(x, e.sys!.mmHg.toDouble()));
+ maxValue = max(maxValue, e.sys!.mmHg);
+ }
+ if (e.pul != null) {
+ pulSpots.add(FlSpot(x, e.pul!.toDouble()));
+ maxValue = max(maxValue, e.pul!);
+ }
+ graphBegin ??= x;
+ graphEnd ??= x;
+ if (x < graphBegin) graphBegin = x;
+ if (x > graphEnd) graphEnd = x;
}
- graphBegin ??= x;
- graphEnd ??= x;
- if (x < graphBegin) graphBegin = x;
- if (x > graphEnd) graphEnd = x;
- }
- if (diaSpots.length < 2 && sysSpots.length < 2 && pulSpots.length < 2 || graphBegin == null || graphEnd == null) {
- return Text(AppLocalizations.of(context)!.errNotEnoughDataToGraph);
- }
+ if (diaSpots.length < 2 && sysSpots.length < 2 && pulSpots.length < 2 || graphBegin == null || graphEnd == null) {
+ return Text(AppLocalizations.of(context)!.errNotEnoughDataToGraph);
+ }
- // Add padding to avoid overflowing vertical lines.
- final len = graphEnd - graphBegin;
- graphBegin -= len / 40;
- graphEnd += len / 40;
+ // Add padding to avoid overflowing vertical lines.
+ final len = graphEnd - graphBegin;
+ graphBegin -= len / 40;
+ graphEnd += len / 40;
- return TweenAnimationBuilder<double>(
- duration: Duration(milliseconds: settings.animationSpeed), // interacts with LineChart duration property
- tween: Tween<double>(begin: 0, end: settings.graphLineThickness),
- builder: (context, animatedThickness, child) => LineChart(
- duration: const Duration(milliseconds: 200),
- LineChartData(
- minY: minValue.toDouble(),
- maxY: maxValue + 5,
- minX: graphBegin,
- maxX: graphEnd,
- clipData: const FlClipData.all(),
- titlesData: _buildFlTitlesData(settings,
- DateTimeRange(start: data.first.time, end: data.last.time),),
- lineTouchData: const LineTouchData(
- touchTooltipData: LineTouchTooltipData(tooltipMargin: -200, tooltipRoundedRadius: 20),
- ),
- lineBarsData: buildBars(animatedThickness, settings, sysSpots, diaSpots, pulSpots,
- maxValue, minValue, graphBegin, graphEnd, records,
- intakes,
+ return TweenAnimationBuilder<double>(
+ duration: Duration(milliseconds: settings.animationSpeed), // interacts with LineChart duration property
+ tween: Tween<double>(begin: 0, end: settings.graphLineThickness),
+ builder: (context, animatedThickness, child) => LineChart(
+ duration: const Duration(milliseconds: 200),
+ LineChartData(
+ minY: minValue.toDouble(),
+ maxY: maxValue + 5,
+ minX: graphBegin,
+ maxX: graphEnd,
+ clipData: const FlClipData.all(),
+ titlesData: _buildFlTitlesData(settings,
+ DateTimeRange(start: data.first.time, end: data.last.time),),
+ lineTouchData: const LineTouchData(
+ touchTooltipData: LineTouchTooltipData(tooltipMargin: -200, tooltipRoundedRadius: 20),
+ ),
+ lineBarsData: buildBars(animatedThickness, settings, sysSpots, diaSpots, pulSpots,
+ maxValue, minValue, graphBegin, graphEnd,
+ records, intakes, notes,
+ ),
),
),
- ),
- );
- },
+ );
+ },
+ ),
),
),
),
@@ -121,6 +121,7 @@ class _LineChartState extends State<_LineChart> {
double? graphEnd,
Iterable<BloodPressureRecord> allRecords,
Iterable<MedicineIntake> allIntakes,
+ Iterable<Note> notes,
) {
final bars = [
_buildBarData(animatedThickness, sysSpots, settings.sysColor, true, settings.sysWarn.toDouble()),
@@ -137,7 +138,7 @@ class _LineChartState extends State<_LineChart> {
_buildRegressionLine(pulSpots),
]);
}
- bars.addAll(_buildNeedlePins(allRecords, minValue, maxValue, settings));
+ bars.addAll(_buildNeedlePins(notes, minValue, maxValue, settings));
for (final intake in allIntakes) {
bars.add(LineChartBarData(
spots: [
@@ -189,22 +190,18 @@ class _LineChartState extends State<_LineChart> {
);
}
- List<LineChartBarData> _buildNeedlePins(Iterable<BloodPressureRecord> allRecords, int min, int max, Settings settings,) {
- final pins = <LineChartBarData>[];
- // TODO: reimplement with health data store notes type
- /*for (final r in allRecords.where((e) => e.needlePin != null)) {
- pins.add(LineChartBarData(
+ List<LineChartBarData> _buildNeedlePins(Iterable<Note> notes, int min, int max, Settings settings,) => [
+ for (final n in notes.where((e) => e.color != null))
+ LineChartBarData(
spots: [
- FlSpot(r.time.millisecondsSinceEpoch.toDouble(), min.toDouble()),
- FlSpot(r.time.millisecondsSinceEpoch.toDouble(), max + 5),
+ FlSpot(n.time.millisecondsSinceEpoch.toDouble(), min.toDouble()),
+ FlSpot(n.time.millisecondsSinceEpoch.toDouble(), max + 5),
],
barWidth: settings.needlePinBarWidth,
dotData: const FlDotData(show: false),
- color: r.needlePin!.color.withAlpha(100),
- ),);
- }*/
- return pins;
- }
+ color: Color(n.color!).withAlpha(100),
+ ),
+ ];
LineChartBarData _buildBarData(double lineThickness, List<FlSpot> spots, Color color, bool hasAreaData, [double? areaDataCutOff]) => LineChartBarData(
spots: spots,
@@ -257,10 +254,9 @@ class _LineChartState extends State<_LineChart> {
);
}
-// TODO: rewrite without fl_chart and simpler to maintain
class MeasurementGraph extends StatelessWidget {
-
const MeasurementGraph({super.key, this.height = 290});
+
final double height;
@override