diameter/lib/models/settings.dart

163 lines
4.8 KiB
Dart

import 'package:diameter/main.dart';
import 'package:flutter/material.dart';
import 'package:objectbox/objectbox.dart';
enum GlucoseDisplayMode { activeOnly, bothForList, bothForDetail, both }
List<String> glucoseDisplayModeLabels = [
'activeOnly',
'bothForList',
'bothForDetail',
'both',
];
enum GlucoseMeasurement {
mgPerDl,
mmolPerL,
}
List<String> glucoseMeasurementSuffixes = [
'mg/dl',
'mmol/l',
];
List<String> glucoseMeasurementLabels = [
'mgPerDl',
'mmolPerL',
];
enum NutritionMeasurement {
grams,
ounces,
lbs,
}
List<String> nutritionMeasurementSuffixes = [
'g',
'oz',
'lbs',
];
List<String> nutritionMeasurementLabels = [
'grams',
'ounces',
'lbs',
];
@Entity(uid: 3989341091218179227)
@Sync()
class Settings {
static final Box<Settings> box = objectBox.store.box<Settings>();
// properties
int id;
int nutritionMeasurementIndex;
int glucoseDisplayModeIndex;
int glucoseMeasurementIndex;
int targetGlucoseMgPerDl;
double targetGlucoseMmolPerL;
double insulinIncrements;
double nutritionIncrements;
double mmolPerLIncrements;
double amountIncrements;
String dateFormat;
String? longDateFormat;
String timeFormat;
String? longTimeFormat;
bool showConfirmationDialogOnCancel;
bool showConfirmationDialogOnDelete;
bool showConfirmationDialogOnStopEvent;
bool useDarkTheme;
DateTime? lastExportTimestamp;
// constructor
Settings({
this.id = 0,
this.nutritionMeasurementIndex = 0,
this.glucoseDisplayModeIndex = 0,
this.glucoseMeasurementIndex = 0,
this.insulinIncrements = 0.05,
this.nutritionIncrements = 0.01,
this.mmolPerLIncrements = 0.1,
this.amountIncrements = 0.05,
this.dateFormat = 'MM/dd/yy',
this.longDateFormat = 'MMMM dd, yyyy',
this.timeFormat = 'HH:mm',
this.longTimeFormat = 'HH:mm:ss',
this.showConfirmationDialogOnCancel = true,
this.showConfirmationDialogOnDelete = true,
this.showConfirmationDialogOnStopEvent = true,
this.targetGlucoseMgPerDl = 100,
this.targetGlucoseMmolPerL = 5.5,
this.useDarkTheme = false,
this.lastExportTimestamp,
});
// methods
static Settings get() {
if (box.getAll().length != 1) {
reset();
}
return box.getAll().single;
}
static NutritionMeasurement get nutritionMeasurement =>
NutritionMeasurement.values[get().nutritionMeasurementIndex];
static GlucoseMeasurement get glucoseMeasurement =>
GlucoseMeasurement.values[get().glucoseMeasurementIndex];
static GlucoseDisplayMode get glucoseDisplayMode =>
GlucoseDisplayMode.values[get().glucoseDisplayModeIndex];
static String get nutritionMeasurementSuffix =>
nutritionMeasurementSuffixes[get().nutritionMeasurementIndex];
static String get glucoseMeasurementSuffix =>
glucoseMeasurementSuffixes[get().glucoseMeasurementIndex];
static int get targetMgPerDl => get().targetGlucoseMgPerDl;
static double get targetMmolPerL => get().targetGlucoseMmolPerL;
static double get insulinSteps => get().insulinIncrements;
static double get nutritionSteps => get().nutritionIncrements;
static double get mmolPerLSteps => get().mmolPerLIncrements;
static ThemeMode get themeMode =>
get().useDarkTheme ? ThemeMode.dark : ThemeMode.light;
static DateTime? get lastExportTimeStamp => get().lastExportTimestamp;
static void put(Settings settings) => box.put(settings);
static void reset() {
box.removeAll();
box.put(Settings(useDarkTheme: ThemeMode.system == ThemeMode.dark));
}
static Map<String, dynamic> toJson() {
Settings settings = get();
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = settings.id;
data['nutritionMeasurementIndex'] = settings.nutritionMeasurementIndex;
data['glucoseDisplayModeIndex'] = settings.glucoseDisplayModeIndex;
data['glucoseMeasurementIndex'] = settings.glucoseMeasurementIndex;
data['targetGlucoseMgPerDl'] = settings.targetGlucoseMgPerDl;
data['targetGlucoseMmolPerL'] = settings.targetGlucoseMmolPerL;
data['insulinIncrements'] = settings.insulinIncrements;
data['nutritionIncrements'] = settings.nutritionIncrements;
data['mmolPerLIncrements'] = settings.mmolPerLIncrements;
data['amountIncrements'] = settings.amountIncrements;
data['dateFormat'] = settings.dateFormat;
data['longDateFormat'] = settings.longDateFormat;
data['timeFormat'] = settings.timeFormat;
data['longTimeFormat'] = settings.longTimeFormat;
data['showConfirmationDialogOnCancel'] =
settings.showConfirmationDialogOnCancel;
data['showConfirmationDialogOnDelete'] =
settings.showConfirmationDialogOnDelete;
data['showConfirmationDialogOnStopEvent'] =
settings.showConfirmationDialogOnStopEvent;
data['useDarkTheme'] = settings.useDarkTheme;
return data;
}
}