diameter/lib/models/log_bolus.dart

78 lines
2.0 KiB
Dart
Raw Normal View History

import 'package:diameter/main.dart';
import 'package:diameter/models/bolus.dart';
import 'package:diameter/models/log_entry.dart';
import 'package:diameter/models/log_meal.dart';
2021-11-25 18:25:13 +00:00
import 'package:objectbox/objectbox.dart';
import 'package:diameter/objectbox.g.dart' show LogBolus_, LogEntry_;
2021-11-25 18:25:13 +00:00
@Entity(uid: 8033487006694871160)
class LogBolus {
static final Box<LogBolus> box = objectBox.store.box<LogBolus>();
// properties
int id;
bool deleted;
double units;
double? carbs;
int? delay;
2021-11-30 03:16:28 +00:00
int? mgPerDlCurrent;
int? mgPerDlTarget;
int? mgPerDlCorrection;
double? mmolPerLCurrent;
double? mmolPerLTarget;
double? mmolPerLCorrection;
2021-11-25 18:25:13 +00:00
bool setManually;
String? notes;
// relations
final logEntry = ToOne<LogEntry>();
final rate = ToOne<Bolus>();
2021-11-30 03:16:28 +00:00
final meal = ToOne<LogMeal>();
// constructor
LogBolus({
this.id = 0,
this.deleted = false,
this.units = 0,
this.carbs,
this.delay,
2021-11-30 03:16:28 +00:00
this.mgPerDlCurrent,
this.mgPerDlTarget,
this.mgPerDlCorrection,
this.mmolPerLCurrent,
this.mmolPerLTarget,
this.mmolPerLCorrection,
2021-11-25 18:25:13 +00:00
this.setManually = false,
this.notes,
});
// methods
static LogBolus? get(int id) => box.get(id);
static void put(LogBolus logBolus) => box.put(logBolus);
2021-11-30 03:16:28 +00:00
static List<LogBolus> getAllForEntry(int id) {
QueryBuilder<LogBolus> builder = box.query(LogBolus_.deleted.equals(false));
builder.link(LogBolus_.logEntry, LogEntry_.id.equals(id));
return builder.build().find();
}
2021-11-30 03:16:28 +00:00
static bool glucoseBolusForEntryExists(int id) {
QueryBuilder<LogBolus> builder = box.query(LogBolus_.deleted.equals(false).and(LogBolus_.mgPerDlCorrection.notNull()));
builder.link(LogBolus_.logEntry, LogEntry_.id.equals(id));
return builder.build().find().isNotEmpty;
}
static void remove(int id) {
final item = box.get(id);
if (item != null) {
item.deleted = true;
box.put(item);
}
}
@override
String toString() {
return units.toString();
}
}