2021-11-24 23:19:27 +00:00
|
|
|
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';
|
2021-11-26 23:01:12 +00:00
|
|
|
import 'package:diameter/objectbox.g.dart' show LogBolus_, LogEntry_;
|
2021-11-24 23:19:27 +00:00
|
|
|
|
2021-11-25 18:25:13 +00:00
|
|
|
@Entity(uid: 8033487006694871160)
|
2021-11-24 23:19:27 +00:00
|
|
|
class LogBolus {
|
|
|
|
static final Box<LogBolus> box = objectBox.store.box<LogBolus>();
|
|
|
|
|
2021-11-26 23:01:12 +00:00
|
|
|
// properties
|
2021-11-24 23:19:27 +00:00
|
|
|
int id;
|
2021-11-26 23:01:12 +00:00
|
|
|
bool deleted;
|
2021-11-24 23:19:27 +00:00
|
|
|
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;
|
2021-11-24 23:19:27 +00:00
|
|
|
String? notes;
|
|
|
|
|
2021-11-26 23:01:12 +00:00
|
|
|
// relations
|
2021-11-24 23:19:27 +00:00
|
|
|
final logEntry = ToOne<LogEntry>();
|
|
|
|
final rate = ToOne<Bolus>();
|
2021-11-30 03:16:28 +00:00
|
|
|
final meal = ToOne<LogMeal>();
|
2021-11-24 23:19:27 +00:00
|
|
|
|
2021-11-26 23:01:12 +00:00
|
|
|
// constructor
|
2021-11-24 23:19:27 +00:00
|
|
|
LogBolus({
|
|
|
|
this.id = 0,
|
2021-11-26 23:01:12 +00:00
|
|
|
this.deleted = false,
|
2021-11-24 23:19:27 +00:00
|
|
|
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,
|
2021-11-24 23:19:27 +00:00
|
|
|
this.notes,
|
|
|
|
});
|
|
|
|
|
2021-11-26 23:01:12 +00:00
|
|
|
// methods
|
2021-11-24 23:19:27 +00:00
|
|
|
static LogBolus? get(int id) => box.get(id);
|
|
|
|
static void put(LogBolus logBolus) => box.put(logBolus);
|
2021-11-30 03:16:28 +00:00
|
|
|
|
2021-11-26 23:01:12 +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
|
|
|
|
2021-12-01 22:39:06 +00:00
|
|
|
static double getTotalBolusForEntry(int id) {
|
|
|
|
QueryBuilder<LogBolus> builder = box.query(LogBolus_.deleted.equals(false));
|
|
|
|
builder.link(LogBolus_.logEntry, LogEntry_.id.equals(id));
|
|
|
|
return builder.build().property(LogBolus_.units).sum();
|
|
|
|
}
|
|
|
|
|
2021-11-30 03:16:28 +00:00
|
|
|
static bool glucoseBolusForEntryExists(int id) {
|
2021-12-01 22:39:06 +00:00
|
|
|
QueryBuilder<LogBolus> builder = box.query(LogBolus_.deleted
|
|
|
|
.equals(false)
|
|
|
|
.and(LogBolus_.mgPerDlCorrection.notNull()));
|
2021-11-30 03:16:28 +00:00
|
|
|
builder.link(LogBolus_.logEntry, LogEntry_.id.equals(id));
|
|
|
|
return builder.build().find().isNotEmpty;
|
|
|
|
}
|
|
|
|
|
2021-11-26 23:01:12 +00:00
|
|
|
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();
|
|
|
|
}
|
2021-11-24 23:19:27 +00:00
|
|
|
}
|