diameter/lib/settings.dart

260 lines
9.6 KiB
Dart
Raw Normal View History

2021-10-22 23:08:09 +00:00
import 'package:diameter/components/dialogs.dart';
import 'package:diameter/components/dropdown.dart';
2021-10-22 23:08:09 +00:00
import 'package:diameter/components/forms.dart';
import 'package:diameter/models/settings.dart';
2021-10-22 23:08:09 +00:00
import 'package:diameter/navigation.dart';
import 'package:flutter/material.dart';
class SettingsScreen extends StatefulWidget {
static const String routeName = '/settings';
2021-10-22 23:08:09 +00:00
const SettingsScreen({Key? key}) : super(key: key);
2021-10-22 23:08:09 +00:00
@override
_SettingsScreenState createState() => _SettingsScreenState();
2021-10-22 23:08:09 +00:00
}
class _SettingsScreenState extends State<SettingsScreen> {
late Settings _settings;
2021-10-22 23:08:09 +00:00
2021-12-06 00:11:22 +00:00
late String _nutritionMeasurementLabel;
late String _glucoseMeasurementLabel;
late bool _onlyDisplayActiveGlucoseMeasurement;
late bool _displayBothGlucoseMeasurementsInDetailView;
late bool _displayBothGlucoseMeasurementsInListView;
2021-10-22 23:08:09 +00:00
2021-12-06 00:11:22 +00:00
// late String _dateFormat;
// late String? _longDateFormat;
// late String _timeFormat;
// late String? _longTimeFormat;
late bool _showConfirmationDialogOnCancel;
late bool _showConfirmationDialogOnDelete;
late bool _showConfirmationDialogOnStopEvent;
// late int _lowGlucoseMgPerDl;
// late int _moderateGlucoseMgPerDl;
// late int _highGlucoseMgPerDl;
// late double _lowGlucoseMmolPerL;
// late double _moderateGlucoseMmolPerL;
// late double _highGlucoseMmolPerDl;
@override
void initState() {
super.initState();
2021-12-06 00:11:22 +00:00
_settings = Settings.get();
_nutritionMeasurementLabel =
nutritionMeasurementLabels[_settings.nutritionMeasurementIndex];
_glucoseMeasurementLabel =
glucoseMeasurementLabels[_settings.glucoseMeasurementIndex];
_onlyDisplayActiveGlucoseMeasurement = _settings.glucoseDisplayModeIndex == GlucoseDisplayMode.activeOnly.index;
_displayBothGlucoseMeasurementsInDetailView =
_settings.glucoseDisplayModeIndex == GlucoseDisplayMode.both.index ||
_settings.glucoseDisplayModeIndex == GlucoseDisplayMode.bothForDetail.index;
_displayBothGlucoseMeasurementsInListView =
_settings.glucoseDisplayModeIndex == GlucoseDisplayMode.both.index ||
_settings.glucoseDisplayModeIndex == GlucoseDisplayMode.bothForList.index;
// _dateFormat = _settings.dateFormat;
// _longDateFormat = _settings.longDateFormat;
// _timeFormat = _settings.timeFormat;
// _longTimeFormat = _settings.longTimeFormat;
_showConfirmationDialogOnCancel = _settings.showConfirmationDialogOnCancel;
_showConfirmationDialogOnDelete = _settings.showConfirmationDialogOnDelete;
_showConfirmationDialogOnStopEvent =
_settings.showConfirmationDialogOnStopEvent;
// _lowGlucoseMgPerDl = _settings.lowGlucoseMgPerDl;
// _moderateGlucoseMgPerDl = _settings.moderateGlucoseMgPerDl;
// _highGlucoseMgPerDl = _settings.highGlucoseMgPerDl;
// _lowGlucoseMmolPerL = _settings.lowGlucoseMmolPerL;
// _moderateGlucoseMmolPerL = _settings.moderateGlucoseMmolPerL;
// _highGlucoseMmolPerDl = _settings.highGlucoseMmolPerDl;
2021-10-22 23:08:09 +00:00
}
void reload({String? message}) {
setState(() {
_settings = Settings.get();
});
2021-10-22 23:08:09 +00:00
setState(() {
if (message != null) {
var snackBar = SnackBar(
content: Text(message),
duration: const Duration(seconds: 2),
);
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(snackBar);
}
2021-10-22 23:08:09 +00:00
});
}
2021-12-06 00:11:22 +00:00
void saveSettings() {
Settings.put(Settings(
id: _settings.id,
nutritionMeasurementIndex:
nutritionMeasurementLabels.indexOf(_nutritionMeasurementLabel),
glucoseMeasurementIndex:
glucoseMeasurementLabels.indexOf(_glucoseMeasurementLabel),
glucoseDisplayModeIndex: _onlyDisplayActiveGlucoseMeasurement
? GlucoseDisplayMode.activeOnly.index
: _displayBothGlucoseMeasurementsInDetailView && _displayBothGlucoseMeasurementsInListView
? GlucoseDisplayMode.both.index
: _displayBothGlucoseMeasurementsInDetailView
? GlucoseDisplayMode.bothForDetail.index
: GlucoseDisplayMode.bothForList.index,
// dateFormat: _dateFormat,
// longDateFormat: _longDateFormat,
// timeFormat: _timeFormat,
// longTimeFormat: _longTimeFormat,
// showConfirmationDialogOnCancel: _showConfirmationDialogOnCancel,
// showConfirmationDialogOnDelete: _showConfirmationDialogOnDelete,
// showConfirmationDialogOnStopEvent: _showConfirmationDialogOnStopEvent,
// lowGlucoseMgPerDl: _dateFormat: _dateFormat,
// longDateFormat: _longDateFormat,
// timeFormat: _timeFormat,
// longTimeFormat: _longTimeFormat,
showConfirmationDialogOnCancel: _showConfirmationDialogOnCancel,
showConfirmationDialogOnDelete: _showConfirmationDialogOnDelete,
showConfirmationDialogOnStopEvent: _showConfirmationDialogOnStopEvent,
// lowGlucoseMgPerDl: _lowGlucoseMgPerDl,
// moderateGlucoseMgPerDl: _moderateGlucoseMgPerDl,
// highGlucoseMgPerDl: _highGlucoseMgPerDl,
// lowGlucoseMmolPerL: _lowGlucoseMmolPerL,
// moderateGlucoseMmolPerL: _moderateGlucoseMmolPerL,
// highGlucoseMmolPerDl: _highGlucoseMmolPerDl,lowGlucoseMgPerDl,
// moderateGlucoseMgPerDl: _moderateGlucoseMgPerDl,
// highGlucoseMgPerDl: _highGlucoseMgPerDl,
// lowGlucoseMmolPerL: _lowGlucoseMmolPerL,
// moderateGlucoseMmolPerL: _moderateGlucoseMmolPerL,
// highGlucoseMmolPerDl: _highGlucoseMmolPerDl,
));
reload(message: 'Settings updated');
}
void onReset() {
Settings.reset();
reload(message: 'Settings have been reset to default');
}
2021-10-22 23:08:09 +00:00
void handleResetAction() async {
Dialogs.showConfirmationDialog(
context: context,
onConfirm: onReset,
message: 'Are you sure you want to reset all settings?',
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Application Settings'),
),
drawer: const Navigation(currentLocation: SettingsScreen.routeName),
body: SingleChildScrollView(
2021-12-06 00:11:22 +00:00
padding: const EdgeInsets.all(10.0),
2021-10-22 23:08:09 +00:00
child: Column(
2021-12-06 00:11:22 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
2021-10-22 23:08:09 +00:00
children: <Widget>[
AutoCompleteDropdownButton<String>(
2021-12-06 00:11:22 +00:00
selectedItem: _nutritionMeasurementLabel,
label: 'Preferred Nutrition Measurement',
items: nutritionMeasurementLabels,
onChanged: (value) {
if (value != null) {
2021-12-06 00:11:22 +00:00
_nutritionMeasurementLabel = value;
saveSettings();
}
},
),
AutoCompleteDropdownButton<String>(
2021-12-06 00:11:22 +00:00
selectedItem: _glucoseMeasurementLabel,
label: 'Preferred Glucose Measurement',
items: glucoseMeasurementLabels,
onChanged: (value) {
if (value != null) {
2021-12-06 00:11:22 +00:00
_glucoseMeasurementLabel = value;
saveSettings();
}
},
),
BooleanFormField(
value: _onlyDisplayActiveGlucoseMeasurement,
label: 'only display active glucose measurement',
2021-12-06 00:11:22 +00:00
onChanged: (value) {
_onlyDisplayActiveGlucoseMeasurement = value;
saveSettings();
},
),
BooleanFormField(
value: _displayBothGlucoseMeasurementsInDetailView,
2021-12-06 00:11:22 +00:00
enabled: !_onlyDisplayActiveGlucoseMeasurement,
label: 'display both glucose measurements in detail view',
2021-12-06 00:11:22 +00:00
onChanged: (value) {
_displayBothGlucoseMeasurementsInDetailView = value;
saveSettings();
},
),
BooleanFormField(
value: _displayBothGlucoseMeasurementsInListView,
2021-12-06 00:11:22 +00:00
enabled: !_onlyDisplayActiveGlucoseMeasurement,
label: 'display both glucose measurements in list view',
2021-12-06 00:11:22 +00:00
onChanged: (value) {
_displayBothGlucoseMeasurementsInListView = value;
saveSettings();
},
),
const Padding(
padding: EdgeInsets.only(top: 10.0),
child: Text('Confirmation prompts'),
),
BooleanFormField(
value: _showConfirmationDialogOnCancel,
label: 'on cancelling edit or creation of a record if changes have already been made',
onChanged: (value) {
_showConfirmationDialogOnCancel = value;
saveSettings();
},
),
BooleanFormField(
value: _showConfirmationDialogOnDelete,
label: 'on deleting a record',
onChanged: (value) {
_showConfirmationDialogOnDelete = value;
saveSettings();
},
),
BooleanFormField(
value: _showConfirmationDialogOnStopEvent,
label: 'on stopping (ending) an event',
onChanged: (value) {
_showConfirmationDialogOnStopEvent = value;
saveSettings();
},
2021-10-22 23:08:09 +00:00
),
],
),
),
bottomNavigationBar: BottomAppBar(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
children: [
ElevatedButton.icon(
onPressed: handleResetAction,
icon: const Icon(
Icons.settings_backup_restore,
size: 18.0,
),
label: const Text('RESET ALL'),
),
const Spacer(),
],
),
),
),
);
}
}