diameter/lib/models/bolus.dart

86 lines
2.3 KiB
Dart
Raw Normal View History

2021-11-25 18:25:13 +00:00
import 'package:diameter/config.dart';
import 'package:diameter/main.dart';
2021-10-22 23:08:09 +00:00
import 'package:diameter/models/bolus_profile.dart';
import 'package:diameter/models/log_event.dart';
import 'package:diameter/utils/date_time_utils.dart';
import 'package:flutter/material.dart';
2021-11-25 18:25:13 +00:00
import 'package:objectbox/objectbox.dart';
import 'package:diameter/objectbox.g.dart' show Bolus_, BolusProfile_;
@Entity(uid: 3417770529060202389)
class Bolus {
static final Box<Bolus> box = objectBox.store.box<Bolus>();
// properties
int id;
bool deleted;
@Property(type: PropertyType.date)
DateTime startTime;
@Property(type: PropertyType.date)
DateTime endTime;
double units;
double carbs;
int? mgPerDl;
double? mmolPerL;
// relations
final bolusProfile = ToOne<BolusProfile>();
// constructor
Bolus({
this.id = 0,
this.deleted = false,
required this.startTime,
required this.endTime,
this.units = 0,
this.carbs = 0,
this.mgPerDl,
this.mmolPerL,
});
// methods
static Bolus? get(int id) => box.get(id);
static void put(Bolus bolus) => box.put(bolus);
static List<Bolus> getAllForProfile(int id) {
QueryBuilder<Bolus> builder = box.query(Bolus_.deleted.equals(false))
..order(Bolus_.startTime);
builder.link(Bolus_.bolusProfile, BolusProfile_.id.equals(id));
return builder.build().find();
2021-10-22 23:08:09 +00:00
}
static void remove(int id) {
final item = box.get(id);
if (item != null) {
item.deleted = true;
box.put(item);
}
}
static Bolus? getRateForTime(DateTime? dateTime) {
if (dateTime != null) {
final bolusProfile = BolusProfile.getActive(dateTime);
2021-11-25 18:25:13 +00:00
final time = DateTimeUtils.convertTimeOfDayToDateTime(
TimeOfDay.fromDateTime(dateTime));
if (bolusProfile != null) {
final rates = Bolus.getAllForProfile(bolusProfile.id);
2021-11-25 18:25:13 +00:00
final result = rates.where((rate) {
DateTime endTime = rate.endTime == dummyDate
? rate.endTime.add(const Duration(days: 1))
: rate.endTime;
return (time.isAfter(rate.startTime) ||
time.isAtSameMomentAs(rate.startTime)) &&
time.isBefore(endTime);
});
return result.length != 1 ? null : result.single;
}
}
return null;
}
@override
String toString() {
return DateTimeUtils.displayTime(startTime);
}
2021-10-22 23:08:09 +00:00
}