Skip to content

Commit a2f035a

Browse files
authored
Implemented Top3Avg aggregation in visualization tab and use it as default (#289)
1 parent 34f90ae commit a2f035a

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

lib/tabs/visualization.dart

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,45 @@ class _VisualizationTabState extends State<VisualizationTab> {
197197
value = totalRepsSets > 0 ? totalWeight / totalRepsSets : 0.0;
198198
break;
199199

200+
case 'Top3Avg':
201+
// Sort records by the value of interest in descending order
202+
final sortedRecords = recordsForDay
203+
.map((record) => {
204+
'weight':
205+
double.tryParse(record['weight'].toString()) ?? 0.0,
206+
'reps': double.tryParse(record['reps'].toString()) ?? 1.0,
207+
'sets': double.tryParse(record['sets'].toString()) ?? 1.0,
208+
})
209+
.toList()
210+
..sort((a, b) => ((b['weight'] ?? 0.0) *
211+
(b['reps'] ?? 1.0) *
212+
(b['sets'] ?? 1.0))
213+
.compareTo((a['weight'] ?? 0.0) *
214+
(a['reps'] ?? 1.0) *
215+
(a['sets'] ?? 1.0)));
216+
217+
// Take the top 3 records
218+
final top3Records = sortedRecords.take(3).toList();
219+
220+
// Calculate the weighted average for the top 3 records
221+
double top3TotalWeight = 0.0;
222+
double top3TotalRepsSets = 0.0;
223+
224+
for (var record in top3Records) {
225+
final weight = record['weight'];
226+
final reps = record['reps'];
227+
final sets = record['sets'];
228+
229+
top3TotalWeight +=
230+
(sets ?? 1.0) * (reps ?? 1.0) * (weight ?? 0.0);
231+
top3TotalRepsSets += (sets ?? 1.0) * (reps ?? 1.0);
232+
}
233+
234+
value = top3TotalRepsSets > 0
235+
? top3TotalWeight / top3TotalRepsSets
236+
: 0.0;
237+
break;
238+
200239
case 'Total':
201240
value = recordsForDay.fold(0.0, (sum, record) {
202241
final sets = double.tryParse(record['sets'].toString()) ?? 1.0;
@@ -317,7 +356,7 @@ class _VisualizationTabState extends State<VisualizationTab> {
317356
mainAxisAlignment: MainAxisAlignment.spaceBetween,
318357
children: [
319358
SizedBox(
320-
width: 90,
359+
width: 100,
321360
child: _buildAggregationDropdown(theme),
322361
),
323362
const SizedBox(height: 16.0),
@@ -435,7 +474,7 @@ class _VisualizationTabState extends State<VisualizationTab> {
435474
});
436475
}
437476
},
438-
items: ['Max', 'Average', 'Total'].map((method) {
477+
items: ['Max', 'Average', 'Top3Avg', 'Total'].map((method) {
439478
return DropdownMenuItem<String>(
440479
value: method,
441480
child: Text(method,

lib/widgets/settings.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class _SettingsModalState extends State<SettingsModal> {
3535
super.initState();
3636
_appTheme = widget.appTheme;
3737
_isKg = widget.isKg;
38-
_aggregationMethod = 'Max'; // Default value
38+
_aggregationMethod = 'Top3Avg'; // Default value
3939
_plotType = 'Line'; // Default value
4040
_appVersion = _getAppVersion();
4141
_loadSettings();
@@ -299,6 +299,10 @@ class _SettingsModalState extends State<SettingsModal> {
299299
value: 'Average',
300300
child: Text('Average'),
301301
),
302+
DropdownMenuItem(
303+
value: 'Top3Avg',
304+
child: Text('Top3Avg'),
305+
),
302306
],
303307
onChanged: _handleAggregationMethodChange,
304308
),

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
1616
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
1717
# In Windows, build-name is used as the major, minor, and patch parts
1818
# of the product and file versions while build-number is used as the build suffix.
19-
version: 0.7.6+35
19+
version: 0.8.0+36
2020

2121
environment:
2222
sdk: '>=3.4.3 <4.0.0'

0 commit comments

Comments
 (0)