diameter/lib/models/bolus.dart

61 lines
1.8 KiB
Dart

import 'package:diameter/main.dart';
import 'package:diameter/models/bolus_profile.dart';
import 'package:diameter/objectbox.g.dart';
import 'package:diameter/utils/date_time_utils.dart';
import 'package:flutter/material.dart';
@Entity(uid: 3417770529060202389)
class Bolus {
static final Box<Bolus> box = objectBox.store.box<Bolus>();
int id;
@Property(type: PropertyType.date)
DateTime startTime;
@Property(type: PropertyType.date)
DateTime endTime;
double units;
double carbs;
int? mgPerDl;
double? mmolPerL;
final bolusProfile = ToOne<BolusProfile>();
Bolus({
this.id = 0,
required this.startTime,
required this.endTime,
this.units = 0,
this.carbs = 0,
this.mgPerDl,
this.mmolPerL,
});
static Bolus? get(int id) => box.get(id);
static void put(Bolus bolus) => box.put(bolus);
static void remove(int id) => box.remove(id);
static List<Bolus> getAllForProfile(int id) {
QueryBuilder<Bolus> builder = box.query()..order(Bolus_.startTime);
builder.link(Bolus_.bolusProfile, BolusProfile_.id.equals(id));
return builder.build().find();
}
static Bolus? getRateForTime(DateTime? dateTime) {
if (dateTime != null) {
// ignore: todo
// TODO: check if an event is active that would change the active profile
final bolusProfile = BolusProfile.getActive();
final time = DateTimeUtils.convertTimeOfDayToDateTime(TimeOfDay.fromDateTime(dateTime));
if (bolusProfile != null) {
final rates = Bolus.getAllForProfile(bolusProfile.id);
final result = rates.where((rate) =>
(time.isAfter(rate.startTime) ||
time.isAtSameMomentAs(rate.startTime)) &&
time.isBefore(rate.endTime));
return result.length != 1 ? null : result.single;
}
return null;
}
}
}