|
| 1 | +/* |
| 2 | + * This file is part of wger Workout Manager <https://github.com/wger-project>. |
| 3 | + * Copyright (C) 2020, 2021 wger Team |
| 4 | + * |
| 5 | + * wger Workout Manager is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +import 'package:wger/helpers/misc.dart'; |
| 20 | +import 'package:wger/widgets/measurements/charts.dart'; |
| 21 | + |
| 22 | +extension MeasurementChartEntryListExtensions on List<MeasurementChartEntry> { |
| 23 | + List<MeasurementChartEntry> whereDate(DateTime start, DateTime? end) { |
| 24 | + return where((e) => e.date.isAfter(start) && (end == null || e.date.isBefore(end))).toList(); |
| 25 | + } |
| 26 | + |
| 27 | + List<MeasurementChartEntry> whereDateWithInterpolation(DateTime start, DateTime? end) { |
| 28 | + // Make sure our list is sorted by date |
| 29 | + sort((a, b) => a.date.compareTo(b.date)); |
| 30 | + |
| 31 | + // Initialize result list |
| 32 | + final List<MeasurementChartEntry> result = []; |
| 33 | + |
| 34 | + // Check if we have any entries on the same day as start/end |
| 35 | + bool hasEntryOnStartDay = false; |
| 36 | + bool hasEntryOnEndDay = false; |
| 37 | + |
| 38 | + // Track entries for potential interpolation |
| 39 | + MeasurementChartEntry? lastBeforeStart; |
| 40 | + MeasurementChartEntry? lastBeforeEnd; |
| 41 | + |
| 42 | + // Single pass through the data |
| 43 | + for (final entry in this) { |
| 44 | + if (entry.date.isSameDayAs(start)) { |
| 45 | + hasEntryOnStartDay = true; |
| 46 | + } |
| 47 | + if (end != null && entry.date.isSameDayAs(end)) { |
| 48 | + hasEntryOnEndDay = true; |
| 49 | + } |
| 50 | + |
| 51 | + if (end != null && entry.date.isBefore(end)) { |
| 52 | + lastBeforeEnd = entry; |
| 53 | + } |
| 54 | + |
| 55 | + if (entry.date.isBefore(start)) { |
| 56 | + lastBeforeStart = entry; |
| 57 | + } else { |
| 58 | + // insert interpolated start value if needed |
| 59 | + if (!hasEntryOnStartDay && lastBeforeStart != null) { |
| 60 | + result.insert(0, interpolateBetween(lastBeforeStart, entry, start)); |
| 61 | + hasEntryOnStartDay = true; |
| 62 | + } |
| 63 | + |
| 64 | + if (end == null || entry.date.isBefore(end)) { |
| 65 | + result.add(entry); |
| 66 | + } |
| 67 | + if (end != null && entry.date.isAfter(end)) { |
| 68 | + // insert interpolated end value if needed |
| 69 | + // note: we only interpolate end if we have data going beyond end |
| 70 | + // if let's say your plan ends in a week from now, we wouldn't want to fake data until next week. |
| 71 | + if (!hasEntryOnEndDay && lastBeforeEnd != null) { |
| 72 | + result.add(interpolateBetween(lastBeforeEnd, entry, end)); |
| 73 | + hasEntryOnEndDay = true; |
| 74 | + } |
| 75 | + // we added all our values and did all interpolations |
| 76 | + // surely all input values from here on are irrelevant. |
| 77 | + return result; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return result; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// caller needs to make sure that before.date < date < after.date |
| 86 | +MeasurementChartEntry interpolateBetween( |
| 87 | + MeasurementChartEntry before, MeasurementChartEntry after, DateTime date) { |
| 88 | + final totalDuration = after.date.difference(before.date).inMilliseconds; |
| 89 | + final startDuration = date.difference(before.date).inMilliseconds; |
| 90 | + |
| 91 | + // Create a special DateTime with milliseconds ending in 123 to mark it as interpolated |
| 92 | + // which we leverage in the UI |
| 93 | + final markedDate = |
| 94 | + DateTime(date.year, date.month, date.day, date.hour, date.minute, date.second, 123); |
| 95 | + |
| 96 | + return MeasurementChartEntry( |
| 97 | + before.value + (after.value - before.value) * (startDuration / totalDuration), |
| 98 | + markedDate, |
| 99 | + ); |
| 100 | +} |
0 commit comments