236 lines
8.2 KiB
Dart
236 lines
8.2 KiB
Dart
import 'package:diameter/components/dialogs.dart';
|
|
import 'package:diameter/components/dropdown.dart';
|
|
import 'package:diameter/components/forms.dart';
|
|
import 'package:diameter/config.dart';
|
|
import 'package:diameter/navigation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
enum GlucoseDisplayMode { activeOnly, bothForList, bothForDetail, both }
|
|
|
|
enum GlucoseMeasurement {
|
|
mgPerDl,
|
|
mmolPerL,
|
|
}
|
|
|
|
enum NutritionMeasurement {
|
|
grams,
|
|
ounces,
|
|
cups,
|
|
}
|
|
|
|
class Settings {
|
|
static void loadSettingsIntoConfig() async {
|
|
nutritionMeasurement = await getNutritionMeasurement();
|
|
glucoseMeasurement = await getGlucoseMeasurement();
|
|
glucoseDisplayMode = await getGlucoseDisplayMode();
|
|
}
|
|
|
|
static Future<GlucoseDisplayMode> getGlucoseDisplayMode() async {
|
|
final settings = await SharedPreferences.getInstance();
|
|
int? index = settings.getInt('glucoseDisplayMode');
|
|
return index != null && index < GlucoseDisplayMode.values.length
|
|
? GlucoseDisplayMode.values[index]
|
|
: GlucoseDisplayMode.bothForList;
|
|
}
|
|
|
|
static Future<GlucoseMeasurement> getGlucoseMeasurement() async {
|
|
final settings = await SharedPreferences.getInstance();
|
|
int? index = settings.getInt('glucoseMeasurement');
|
|
return index != null && index < GlucoseMeasurement.values.length
|
|
? GlucoseMeasurement.values[index]
|
|
: GlucoseMeasurement.mgPerDl;
|
|
}
|
|
|
|
static Future<NutritionMeasurement> getNutritionMeasurement() async {
|
|
final settings = await SharedPreferences.getInstance();
|
|
int? index = settings.getInt('nutritionMeasurement');
|
|
return index != null && index < NutritionMeasurement.values.length
|
|
? NutritionMeasurement.values[index]
|
|
: NutritionMeasurement.grams;
|
|
}
|
|
|
|
static void setGlucoseDisplayMode(
|
|
GlucoseDisplayMode? glucoseDisplayMode) async {
|
|
final settings = await SharedPreferences.getInstance();
|
|
if (glucoseDisplayMode != null) {
|
|
settings.setInt('glucoseDisplayMode', glucoseDisplayMode.index);
|
|
}
|
|
}
|
|
|
|
static void setGlucoseMeasurement(
|
|
GlucoseMeasurement? glucoseMeasurement) async {
|
|
final settings = await SharedPreferences.getInstance();
|
|
if (glucoseMeasurement != null) {
|
|
settings.setInt('preferredGlucoseMeasurement', glucoseMeasurement.index);
|
|
}
|
|
}
|
|
|
|
static void setNutritionMeasurement(
|
|
NutritionMeasurement? nutritionMeasurement) async {
|
|
final settings = await SharedPreferences.getInstance();
|
|
if (nutritionMeasurement != null) {
|
|
settings.setInt(
|
|
'preferredNutritionMeasurement', nutritionMeasurement.index);
|
|
}
|
|
}
|
|
|
|
static void resetAll() async {
|
|
final settings = await SharedPreferences.getInstance();
|
|
|
|
settings.remove('glucoseDisplayMode');
|
|
settings.remove('preferredGlucoseMeasurement');
|
|
settings.remove('preferredNutritionMeasurement');
|
|
}
|
|
}
|
|
|
|
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> {
|
|
final GlobalKey<FormState> _settingsForm = GlobalKey<FormState>();
|
|
|
|
void onReset() {
|
|
Settings.resetAll();
|
|
setState(() {
|
|
Settings.loadSettingsIntoConfig();
|
|
});
|
|
}
|
|
|
|
void handleResetAction() async {
|
|
Dialogs.showConfirmationDialog(
|
|
context: context,
|
|
onConfirm: onReset,
|
|
message: 'Are you sure you want to reset all settings?',
|
|
);
|
|
}
|
|
|
|
@override
|
|
initState() {
|
|
super.initState();
|
|
Settings.loadSettingsIntoConfig();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Application Settings'),
|
|
),
|
|
drawer: const Navigation(currentLocation: SettingsScreen.routeName),
|
|
body: SingleChildScrollView(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
FormWrapper(
|
|
formState: _settingsForm,
|
|
fields: [
|
|
AutoCompleteDropdownButton<NutritionMeasurement>(
|
|
selectedItem: nutritionMeasurement,
|
|
label: 'Preferred Nutrition Measurement',
|
|
items: NutritionMeasurement.values,
|
|
onChanged: (value) {
|
|
if (value != null) {
|
|
Settings.setNutritionMeasurement(value);
|
|
setState(() {
|
|
nutritionMeasurement = value;
|
|
});
|
|
}
|
|
},
|
|
),
|
|
AutoCompleteDropdownButton<GlucoseMeasurement>(
|
|
selectedItem: glucoseMeasurement,
|
|
label: 'Preferred Glucose Measurement',
|
|
items: GlucoseMeasurement.values,
|
|
onChanged: (value) {
|
|
if (value != null) {
|
|
Settings.setGlucoseMeasurement(value);
|
|
setState(() {
|
|
glucoseMeasurement = value;
|
|
});
|
|
}
|
|
},
|
|
),
|
|
BooleanFormField(
|
|
value: glucoseDisplayMode == GlucoseDisplayMode.activeOnly,
|
|
label: 'only display active glucose measurement',
|
|
onChanged: (_) {
|
|
GlucoseDisplayMode mode =
|
|
glucoseDisplayMode == GlucoseDisplayMode.activeOnly
|
|
? GlucoseDisplayMode.both
|
|
: GlucoseDisplayMode.activeOnly;
|
|
Settings.setGlucoseDisplayMode(mode);
|
|
setState(() {
|
|
glucoseDisplayMode = mode;
|
|
});
|
|
},
|
|
),
|
|
BooleanFormField(
|
|
value: glucoseDisplayMode == GlucoseDisplayMode.both ||
|
|
glucoseDisplayMode == GlucoseDisplayMode.bothForDetail,
|
|
enabled: glucoseDisplayMode != GlucoseDisplayMode.activeOnly,
|
|
label: 'display both glucose measurements in detail view',
|
|
onChanged: (_) {
|
|
GlucoseDisplayMode mode = glucoseDisplayMode ==
|
|
GlucoseDisplayMode.both
|
|
? GlucoseDisplayMode.bothForList
|
|
: glucoseDisplayMode == GlucoseDisplayMode.bothForList
|
|
? GlucoseDisplayMode.both
|
|
: GlucoseDisplayMode.activeOnly;
|
|
Settings.setGlucoseDisplayMode(mode);
|
|
setState(() {
|
|
glucoseDisplayMode = mode;
|
|
});
|
|
},
|
|
),
|
|
BooleanFormField(
|
|
value: glucoseDisplayMode == GlucoseDisplayMode.both ||
|
|
glucoseDisplayMode == GlucoseDisplayMode.bothForList,
|
|
enabled: glucoseDisplayMode != GlucoseDisplayMode.activeOnly,
|
|
label: 'display both glucose measurements in list view',
|
|
onChanged: (_) {
|
|
GlucoseDisplayMode mode = glucoseDisplayMode ==
|
|
GlucoseDisplayMode.both
|
|
? GlucoseDisplayMode.bothForDetail
|
|
: glucoseDisplayMode == GlucoseDisplayMode.bothForDetail
|
|
? GlucoseDisplayMode.both
|
|
: GlucoseDisplayMode.activeOnly;
|
|
Settings.setGlucoseDisplayMode(mode);
|
|
setState(() {
|
|
glucoseDisplayMode = mode;
|
|
});
|
|
},
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
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(),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|