286 lines
11 KiB
Dart
286 lines
11 KiB
Dart
import 'package:diameter/components/dialogs.dart';
|
|
import 'package:diameter/components/dropdown.dart';
|
|
import 'package:diameter/components/forms.dart';
|
|
import 'package:diameter/models/settings.dart';
|
|
import 'package:diameter/navigation.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class SettingsScreen extends StatefulWidget {
|
|
static const String routeName = '/settings';
|
|
|
|
const SettingsScreen({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_SettingsScreenState createState() => _SettingsScreenState();
|
|
}
|
|
|
|
class _SettingsScreenState extends State<SettingsScreen> {
|
|
late Settings _settings;
|
|
|
|
final TextEditingController _nutritionMeasurementLabelController = TextEditingController(text: '');
|
|
final TextEditingController _glucoseMeasurementLabelController = TextEditingController(text: '');
|
|
|
|
late bool _onlyDisplayActiveGlucoseMeasurement;
|
|
late bool _displayBothGlucoseMeasurementsInDetailView;
|
|
late bool _displayBothGlucoseMeasurementsInListView;
|
|
|
|
// 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();
|
|
_settings = Settings.get();
|
|
_nutritionMeasurementLabelController.text =
|
|
nutritionMeasurementLabels[_settings.nutritionMeasurementIndex];
|
|
_glucoseMeasurementLabelController.text =
|
|
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;
|
|
}
|
|
|
|
void reload({String? message}) {
|
|
setState(() {
|
|
_settings = Settings.get();
|
|
});
|
|
|
|
setState(() {
|
|
if (message != null) {
|
|
var snackBar = SnackBar(
|
|
content: Text(message),
|
|
duration: const Duration(seconds: 2),
|
|
);
|
|
ScaffoldMessenger.of(context)
|
|
..removeCurrentSnackBar()
|
|
..showSnackBar(snackBar);
|
|
}
|
|
});
|
|
}
|
|
|
|
void saveSettings() {
|
|
Settings.put(Settings(
|
|
id: _settings.id,
|
|
nutritionMeasurementIndex:
|
|
nutritionMeasurementLabels.indexOf(_nutritionMeasurementLabelController.text),
|
|
glucoseMeasurementIndex:
|
|
glucoseMeasurementLabels.indexOf(_glucoseMeasurementLabelController.text),
|
|
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');
|
|
}
|
|
|
|
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(
|
|
padding: const EdgeInsets.all(10.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 10.0),
|
|
child: Row(
|
|
children: [
|
|
Text(
|
|
'MEASUREMENTS',
|
|
style: Theme.of(context).textTheme.subtitle2,
|
|
),
|
|
const Spacer(),
|
|
],
|
|
),
|
|
),
|
|
AutoCompleteDropdownButton<String>(
|
|
controller: _nutritionMeasurementLabelController,
|
|
selectedItem: _nutritionMeasurementLabelController.text,
|
|
label: 'Preferred Nutrition Measurement',
|
|
items: nutritionMeasurementLabels,
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_nutritionMeasurementLabelController.text = value ?? '';
|
|
});
|
|
saveSettings();
|
|
},
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 10.0),
|
|
child: AutoCompleteDropdownButton<String>(
|
|
controller: _glucoseMeasurementLabelController,
|
|
selectedItem: _glucoseMeasurementLabelController.text,
|
|
label: 'Preferred Glucose Measurement',
|
|
items: glucoseMeasurementLabels,
|
|
onChanged: (value) {
|
|
setState(() {
|
|
_glucoseMeasurementLabelController.text = value ?? '';
|
|
});
|
|
saveSettings();
|
|
},
|
|
),
|
|
),
|
|
BooleanFormField(
|
|
value: _onlyDisplayActiveGlucoseMeasurement,
|
|
label: 'only display active glucose measurement',
|
|
onChanged: (value) {
|
|
_onlyDisplayActiveGlucoseMeasurement = value;
|
|
saveSettings();
|
|
},
|
|
),
|
|
BooleanFormField(
|
|
value: _displayBothGlucoseMeasurementsInDetailView,
|
|
enabled: !_onlyDisplayActiveGlucoseMeasurement,
|
|
label: 'display both glucose measurements in detail view',
|
|
onChanged: (value) {
|
|
_displayBothGlucoseMeasurementsInDetailView = value;
|
|
saveSettings();
|
|
},
|
|
),
|
|
BooleanFormField(
|
|
value: _displayBothGlucoseMeasurementsInListView,
|
|
enabled: !_onlyDisplayActiveGlucoseMeasurement,
|
|
label: 'display both glucose measurements in list view',
|
|
onChanged: (value) {
|
|
_displayBothGlucoseMeasurementsInListView = value;
|
|
saveSettings();
|
|
},
|
|
),
|
|
const Divider(),
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 10.0),
|
|
child: Row(
|
|
children: [
|
|
Text(
|
|
'CONFIRMATION PROMPTS',
|
|
style: Theme.of(context).textTheme.subtitle2,
|
|
),
|
|
const Spacer(),
|
|
],
|
|
),
|
|
),
|
|
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();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
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(),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|