Skip to content

Commit

Permalink
Allow for setting default aggregation method and ploy type in setting…
Browse files Browse the repository at this point in the history
…s modal (#288)
  • Loading branch information
andreped authored Sep 22, 2024
1 parent 92dca82 commit 51aa0c8
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 3 deletions.
25 changes: 25 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
AppTheme _appTheme = AppTheme.system; // Default to system theme
bool _isKg = true; // Default unit system
String _aggregationMethod = 'Max'; // Default aggregation method
String _plotType = 'Line'; // Default plot type

@override
void initState() {
Expand All @@ -29,13 +31,18 @@ class _MyAppState extends State<MyApp> {
setState(() {
_appTheme = AppTheme.values[prefs.getInt('appTheme') ?? _appTheme.index];
_isKg = prefs.getBool('isKg') ?? _isKg;
_aggregationMethod =
prefs.getString('aggregationMethod') ?? _aggregationMethod;
_plotType = prefs.getString('plotType') ?? _plotType;
});
}

Future<void> _saveSettings() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('appTheme', _appTheme.index);
await prefs.setBool('isKg', _isKg);
await prefs.setString('aggregationMethod', _aggregationMethod);
await prefs.setString('plotType', _plotType);
}

void _toggleTheme(AppTheme newTheme) {
Expand All @@ -52,6 +59,20 @@ class _MyAppState extends State<MyApp> {
_saveSettings();
}

void _setAggregationMethod(String newMethod) {
setState(() {
_aggregationMethod = newMethod;
});
_saveSettings();
}

void _setPlotType(String newType) {
setState(() {
_plotType = newType;
});
_saveSettings();
}

@override
Widget build(BuildContext context) {
// Get current brightness
Expand All @@ -70,6 +91,10 @@ class _MyAppState extends State<MyApp> {
updateTheme: _toggleTheme,
isKg: _isKg,
toggleUnit: _toggleUnit,
aggregationMethod: _aggregationMethod,
setAggregationMethod: _setAggregationMethod,
plotType: _plotType,
setPlotType: _setPlotType,
),
);
}
Expand Down
22 changes: 22 additions & 0 deletions lib/tabs/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ class ExerciseStoreApp extends StatelessWidget {
final ValueChanged<AppTheme> updateTheme;
final bool isKg;
final ValueChanged<bool> toggleUnit;
final String aggregationMethod;
final ValueChanged<String> setAggregationMethod;
final String plotType;
final ValueChanged<String> setPlotType;

const ExerciseStoreApp({
Key? key,
required this.appTheme,
required this.updateTheme,
required this.isKg,
required this.toggleUnit,
required this.aggregationMethod,
required this.setAggregationMethod,
required this.plotType,
required this.setPlotType,
}) : super(key: key);

@override
Expand All @@ -38,6 +46,10 @@ class ExerciseStoreApp extends StatelessWidget {
updateTheme: updateTheme,
isKg: isKg,
toggleUnit: toggleUnit,
aggregationMethod: aggregationMethod,
setAggregationMethod: setAggregationMethod,
plotType: plotType,
setPlotType: setPlotType,
),
);
}
Expand All @@ -48,13 +60,21 @@ class ExerciseStoreHomePage extends StatefulWidget {
final ValueChanged<AppTheme> updateTheme;
final bool isKg;
final ValueChanged<bool> toggleUnit;
final String aggregationMethod;
final ValueChanged<String> setAggregationMethod;
final String plotType;
final ValueChanged<String> setPlotType;

const ExerciseStoreHomePage({
Key? key,
required this.appTheme,
required this.updateTheme,
required this.isKg,
required this.toggleUnit,
required this.aggregationMethod,
required this.setAggregationMethod,
required this.plotType,
required this.setPlotType,
}) : super(key: key);

@override
Expand Down Expand Up @@ -156,6 +176,8 @@ class _ExerciseStoreHomePageState extends State<ExerciseStoreHomePage>
child: VisualizationTab(
key: PageStorageKey('visualizationTab'),
isKg: widget.isKg,
defaultAggregationMethod: widget.aggregationMethod,
defaultChartType: widget.plotType,
),
),
TableTab(isKg: widget.isKg),
Expand Down
30 changes: 27 additions & 3 deletions lib/tabs/visualization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ import '../core/theme.dart';

class VisualizationTab extends StatefulWidget {
final bool isKg;
final String defaultAggregationMethod;
final String defaultChartType;

const VisualizationTab({Key? key, required this.isKg}) : super(key: key);
const VisualizationTab({
Key? key,
required this.isKg,
required this.defaultAggregationMethod,
required this.defaultChartType,
}) : super(key: key);

@override
_VisualizationTabState createState() => _VisualizationTabState();
Expand All @@ -16,8 +23,8 @@ class VisualizationTab extends StatefulWidget {
class _VisualizationTabState extends State<VisualizationTab> {
String? _selectedExercise;
String _selectedTable = 'Exercise'; // Default selected table
String _aggregationMethod = 'Max'; // Default aggregation method
String _chartType = 'Line'; // Default chart type
late String _aggregationMethod;
late String _chartType;
String _dataType = 'Weight'; // Default data type for Fitness table
List<String> _exerciseNames = [];
List<ScatterSpot> _dataPoints = [];
Expand All @@ -32,9 +39,26 @@ class _VisualizationTabState extends State<VisualizationTab> {
@override
void initState() {
super.initState();
_initializeDefaults();
_fetchExerciseNames(); // Fetch exercise names initially
}

void _initializeDefaults() {
setState(() {
_aggregationMethod = widget.defaultAggregationMethod;
_chartType = widget.defaultChartType;
});
}

@override
void didUpdateWidget(covariant VisualizationTab oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.defaultAggregationMethod != oldWidget.defaultAggregationMethod ||
widget.defaultChartType != oldWidget.defaultChartType) {
_initializeDefaults();
}
}

@override
void didChangeDependencies() {
super.didChangeDependencies();
Expand Down
91 changes: 91 additions & 0 deletions lib/widgets/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ class _SettingsModalState extends State<SettingsModal> {
final DatabaseHelper _dbHelper = DatabaseHelper();
late AppTheme _appTheme;
late bool _isKg;
late String _aggregationMethod;
late String _plotType;
late Future<String> _appVersion;

@override
void initState() {
super.initState();
_appTheme = widget.appTheme;
_isKg = widget.isKg;
_aggregationMethod = 'Max'; // Default value
_plotType = 'Line'; // Default value
_appVersion = _getAppVersion();
_loadSettings();
}
Expand All @@ -42,13 +46,18 @@ class _SettingsModalState extends State<SettingsModal> {
setState(() {
_appTheme = AppTheme.values[prefs.getInt('appTheme') ?? _appTheme.index];
_isKg = prefs.getBool('isKg') ?? _isKg;
_aggregationMethod =
prefs.getString('aggregationMethod') ?? _aggregationMethod;
_plotType = prefs.getString('plotType') ?? _plotType;
});
}

Future<void> _saveSettings() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('appTheme', _appTheme.index);
await prefs.setBool('isKg', _isKg);
await prefs.setString('aggregationMethod', _aggregationMethod);
await prefs.setString('plotType', _plotType);
}

Future<String> _getAppVersion() async {
Expand All @@ -74,6 +83,24 @@ class _SettingsModalState extends State<SettingsModal> {
_saveSettings();
}

void _handleAggregationMethodChange(String? newMethod) {
if (newMethod != null) {
setState(() {
_aggregationMethod = newMethod;
});
_saveSettings();
}
}

void _handlePlotTypeChange(String? newType) {
if (newType != null) {
setState(() {
_plotType = newType;
});
_saveSettings();
}
}

Future<void> _showConfirmationDialogs(String tableName) async {
final bool? firstDialogConfirmed = await showDialog<bool>(
context: context,
Expand Down Expand Up @@ -242,6 +269,70 @@ class _SettingsModalState extends State<SettingsModal> {
),
),
),
// Aggregation method selection
ListTile(
title: Text(
'Aggregation Method',
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(fontSize: 14),
),
trailing: DropdownButton<String>(
value: _aggregationMethod,
style: TextStyle(
fontSize: 14,
color: isDarkMode
? Colors.purple // Use primary color for dark mode
: Colors.black, // Set text color for light mode
),
items: [
DropdownMenuItem(
value: 'Total',
child: Text('Total'),
),
DropdownMenuItem(
value: 'Max',
child: Text('Max'),
),
DropdownMenuItem(
value: 'Average',
child: Text('Average'),
),
],
onChanged: _handleAggregationMethodChange,
),
),
// Plot type selection
ListTile(
title: Text(
'Plot Type',
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(fontSize: 14),
),
trailing: DropdownButton<String>(
value: _plotType,
style: TextStyle(
fontSize: 14,
color: isDarkMode
? Colors.purple // Use primary color for dark mode
: Colors.black, // Set text color for light mode
),
items: [
DropdownMenuItem(
value: 'Line',
child: Text('Line'),
),
DropdownMenuItem(
value: 'Scatter',
child: Text('Scatter'),
),
],
onChanged: _handlePlotTypeChange,
),
),
Divider(),
// Clear exercises database
ListTile(
Expand Down

0 comments on commit 51aa0c8

Please sign in to comment.