2022-03-21 00:07:29 +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';
|
|
|
|
import 'package:objectbox/objectbox.dart';
|
|
|
|
import 'package:diameter/objectbox.g.dart' show LogBolus_, LogEntry_, LogMeal_;
|
|
|
|
|
|
|
|
@Entity(uid: 8033487006694871160)
|
|
|
|
@Sync()
|
|
|
|
class LogBolus {
|
|
|
|
static final Box<LogBolus> box = objectBox.store.box<LogBolus>();
|
|
|
|
|
|
|
|
// 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;
|
2022-07-08 02:08:50 +00:00
|
|
|
String? source;
|
2022-03-21 00:07:29 +00:00
|
|
|
|
|
|
|
// relations
|
|
|
|
final logEntry = ToOne<LogEntry>();
|
|
|
|
final rate = ToOne<Bolus>();
|
|
|
|
final meal = ToOne<LogMeal>();
|
|
|
|
|
|
|
|
// 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,
|
2022-07-08 02:08:50 +00:00
|
|
|
this.source,
|
2022-03-21 00:07:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// methods
|
|
|
|
static LogBolus? get(int id) => box.get(id);
|
|
|
|
static void put(LogBolus logBolus) => box.put(logBolus);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool glucoseBolusForEntryExists(int id) {
|
|
|
|
QueryBuilder<LogBolus> 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) {
|
2022-07-08 02:08:50 +00:00
|
|
|
QueryBuilder<LogBolus> builder = box.query(LogBolus_.deleted.equals(false));
|
2022-03-21 00:07:29 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 02:08:50 +00:00
|
|
|
static void removeAllForEntry(int id) {
|
|
|
|
box.putMany(getAllForEntry(id).map((item) {
|
|
|
|
item.deleted = true;
|
|
|
|
return item;
|
|
|
|
}).toList());
|
|
|
|
}
|
|
|
|
|
2022-03-21 00:07:29 +00:00
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return units.toString();
|
|
|
|
}
|
2022-07-08 02:08:50 +00:00
|
|
|
|
|
|
|
Map<String, dynamic> toJson() {
|
|
|
|
final Map<String, dynamic> data = <String, dynamic>{};
|
|
|
|
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<String, dynamic> 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;
|
|
|
|
}
|
2022-03-21 00:07:29 +00:00
|
|
|
}
|