Skip to content

Commit

Permalink
Implemented Top3Avg aggregation in visualization tab and use it as de…
Browse files Browse the repository at this point in the history
…fault (#289)
  • Loading branch information
andreped authored Sep 22, 2024
1 parent 34f90ae commit a2f035a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
43 changes: 41 additions & 2 deletions lib/tabs/visualization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,45 @@ class _VisualizationTabState extends State<VisualizationTab> {
value = totalRepsSets > 0 ? totalWeight / totalRepsSets : 0.0;
break;

case 'Top3Avg':
// Sort records by the value of interest in descending order
final sortedRecords = recordsForDay
.map((record) => {
'weight':
double.tryParse(record['weight'].toString()) ?? 0.0,
'reps': double.tryParse(record['reps'].toString()) ?? 1.0,
'sets': double.tryParse(record['sets'].toString()) ?? 1.0,
})
.toList()
..sort((a, b) => ((b['weight'] ?? 0.0) *
(b['reps'] ?? 1.0) *
(b['sets'] ?? 1.0))
.compareTo((a['weight'] ?? 0.0) *
(a['reps'] ?? 1.0) *
(a['sets'] ?? 1.0)));

// Take the top 3 records
final top3Records = sortedRecords.take(3).toList();

// Calculate the weighted average for the top 3 records
double top3TotalWeight = 0.0;
double top3TotalRepsSets = 0.0;

for (var record in top3Records) {
final weight = record['weight'];
final reps = record['reps'];
final sets = record['sets'];

top3TotalWeight +=
(sets ?? 1.0) * (reps ?? 1.0) * (weight ?? 0.0);
top3TotalRepsSets += (sets ?? 1.0) * (reps ?? 1.0);
}

value = top3TotalRepsSets > 0
? top3TotalWeight / top3TotalRepsSets
: 0.0;
break;

case 'Total':
value = recordsForDay.fold(0.0, (sum, record) {
final sets = double.tryParse(record['sets'].toString()) ?? 1.0;
Expand Down Expand Up @@ -317,7 +356,7 @@ class _VisualizationTabState extends State<VisualizationTab> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
width: 90,
width: 100,
child: _buildAggregationDropdown(theme),
),
const SizedBox(height: 16.0),
Expand Down Expand Up @@ -435,7 +474,7 @@ class _VisualizationTabState extends State<VisualizationTab> {
});
}
},
items: ['Max', 'Average', 'Total'].map((method) {
items: ['Max', 'Average', 'Top3Avg', 'Total'].map((method) {
return DropdownMenuItem<String>(
value: method,
child: Text(method,
Expand Down
6 changes: 5 additions & 1 deletion lib/widgets/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class _SettingsModalState extends State<SettingsModal> {
super.initState();
_appTheme = widget.appTheme;
_isKg = widget.isKg;
_aggregationMethod = 'Max'; // Default value
_aggregationMethod = 'Top3Avg'; // Default value
_plotType = 'Line'; // Default value
_appVersion = _getAppVersion();
_loadSettings();
Expand Down Expand Up @@ -299,6 +299,10 @@ class _SettingsModalState extends State<SettingsModal> {
value: 'Average',
child: Text('Average'),
),
DropdownMenuItem(
value: 'Top3Avg',
child: Text('Top3Avg'),
),
],
onChanged: _handleAggregationMethodChange,
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 0.7.6+35
version: 0.8.0+36

environment:
sdk: '>=3.4.3 <4.0.0'
Expand Down

0 comments on commit a2f035a

Please sign in to comment.