diameter/lib/models/basal.dart

76 lines
1.9 KiB
Dart
Raw Normal View History

import 'package:diameter/main.dart';
2021-10-22 23:08:09 +00:00
import 'package:diameter/models/basal_profile.dart';
2022-03-21 00:07:29 +00:00
import 'package:diameter/utils/date_time_utils.dart';
import 'package:objectbox/objectbox.dart';
import 'package:diameter/objectbox.g.dart' show Basal_, BasalProfile_;
2022-03-21 00:07:29 +00:00
@Entity(uid: 1467758525778521891)
@Sync()
class Basal {
static final Box<Basal> box = objectBox.store.box<Basal>();
2022-03-21 00:07:29 +00:00
// properties
int id;
bool deleted;
@Property(type: PropertyType.date)
DateTime startTime;
@Property(type: PropertyType.date)
DateTime endTime;
double units;
2022-03-21 00:07:29 +00:00
// relations
final basalProfile = ToOne<BasalProfile>();
2022-03-21 00:07:29 +00:00
// constructor
Basal({
this.id = 0,
2022-03-21 00:07:29 +00:00
this.deleted = false,
required this.startTime,
required this.endTime,
this.units = 0,
});
2022-03-21 00:07:29 +00:00
// methods
static Basal? get(int id) => box.get(id);
static void put(Basal basal) => box.put(basal);
2022-03-21 00:07:29 +00:00
static void remove(int id) {
final item = box.get(id);
if (item != null) {
item.deleted = true;
box.put(item);
}
}
static List<Basal> getAllForProfile(int id) {
2022-03-21 00:07:29 +00:00
QueryBuilder<Basal> builder = box.query(Basal_.deleted.equals(false))
..order(Basal_.startTime);
builder.link(Basal_.basalProfile, BasalProfile_.id.equals(id));
return builder.build().find();
2021-10-22 23:08:09 +00:00
}
2022-03-21 00:07:29 +00:00
static double getDailyTotalForProfile(int id) {
double sum = 0.0;
QueryBuilder<Basal> builder = box.query(Basal_.deleted.equals(false));
builder.link(Basal_.basalProfile, BasalProfile_.id.equals(id));
List<Basal> basalRates = builder.build().find();
for (Basal basal in basalRates) {
double rateDuration =
basal.endTime.difference(basal.startTime).inMinutes / 60;
if (rateDuration < 0) {
rateDuration += 24;
}
sum += basal.units * rateDuration;
}
return sum;
}
@override
String toString() {
return DateTimeUtils.displayTime(startTime);
}
2021-10-22 23:08:09 +00:00
}