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'; import 'package:objectbox/objectbox.dart'; import 'package:diameter/objectbox.g.dart' show LogBolus_, LogEntry_, LogMeal_; @Entity(uid: 8033487006694871160) @Sync() class LogBolus { static final Box box = objectBox.store.box(); // properties int id; bool deleted; double units; double? carbs; int? delay; int? mgPerDlCurrent; int? mgPerDlTarget; int? mgPerDlCorrection; double? mmolPerLCurrent; double? mmolPerLTarget; double? mmolPerLCorrection; bool setManually; String? notes; String? source; // relations final logEntry = ToOne(); final rate = ToOne(); final meal = ToOne(); // constructor LogBolus({ this.id = 0, this.deleted = false, this.units = 0, this.carbs, this.delay, this.mgPerDlCurrent, this.mgPerDlTarget, this.mgPerDlCorrection, this.mmolPerLCurrent, this.mmolPerLTarget, this.mmolPerLCorrection, this.setManually = false, this.notes, this.source, }); // methods static LogBolus? get(int id) => box.get(id); static void put(LogBolus logBolus) => box.put(logBolus); static List getAllForEntry(int id) { QueryBuilder builder = box.query(LogBolus_.deleted.equals(false)); builder.link(LogBolus_.logEntry, LogEntry_.id.equals(id)); return builder.build().find(); } static double getTotalBolusForEntry(int id) { QueryBuilder builder = box.query(LogBolus_.deleted.equals(false)); builder.link(LogBolus_.logEntry, LogEntry_.id.equals(id)); return builder.build().property(LogBolus_.units).sum(); } static bool glucoseBolusForEntryExists(int id) { QueryBuilder builder = box.query(LogBolus_.deleted .equals(false) .and(LogBolus_.mgPerDlCorrection.notNull())); builder.link(LogBolus_.meal, LogMeal_.id.equals(id)); return builder.build().find().isNotEmpty; } static bool bolusForMealExists(int id) { QueryBuilder builder = box.query(LogBolus_.deleted.equals(false)); builder.link(LogBolus_.meal, LogMeal_.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); } } static void removeAllForEntry(int id) { box.putMany(getAllForEntry(id).map((item) { item.deleted = true; return item; }).toList()); } @override String toString() { return units.toString(); } Map toJson() { final Map data = {}; data['id'] = id; data['deleted'] = deleted; data['units'] = units; data['carbs'] = carbs; data['delay'] = delay; data['mgPerDlCurrent'] = mgPerDlCurrent; data['mgPerDlTarget'] = mgPerDlTarget; data['mgPerDlCorrection'] = mgPerDlCorrection; data['mmolPerLCurrent'] = mmolPerLCurrent; data['mmolPerLTarget'] = mmolPerLTarget; data['mmolPerLCorrection'] = mmolPerLCorrection; data['setManually'] = setManually; data['notes'] = notes; data['logEntry'] = logEntry.targetId; data['rate'] = rate.targetId; data['meal'] = meal.targetId; return data; } static String? putFromJson(Map json, bool overrideExisting, String? source) { final logBolus = LogBolus( id: overrideExisting ? json['id'] : 0, deleted: json['deleted'], units: json['units'], carbs: json['carbs'], delay: json['delay'], mgPerDlCurrent: json['mgPerDlCurrent'], mgPerDlTarget: json['mgPerDlTarget'], mgPerDlCorrection: json['mgPerDlCorrection'], mmolPerLCurrent: json['mmolPerLCurrent'], mmolPerLTarget: json['mmolPerLTarget'], mmolPerLCorrection: json['mmolPerLCorrection'], setManually: json['setManually'], notes: json['notes'], source: source, ); logBolus.logEntry.targetId = json['logEntry']; logBolus.rate.targetId = json['rate']; logBolus.meal.targetId = json['meal']; LogBolus.put(logBolus); return null; } }