diameter/lib/models/bolus_profile.dart

121 lines
3.1 KiB
Dart
Raw Normal View History

import 'package:diameter/main.dart';
import 'package:diameter/models/bolus.dart';
2022-03-21 00:07:29 +00:00
import 'package:diameter/models/log_event.dart';
import 'package:objectbox/objectbox.dart';
import 'package:diameter/objectbox.g.dart' show BolusProfile_;
2021-10-22 23:08:09 +00:00
2022-03-21 00:07:29 +00:00
@Entity(uid: 8812452529027052317)
@Sync()
2021-10-22 23:08:09 +00:00
class BolusProfile {
static final Box<BolusProfile> box = objectBox.store.box<BolusProfile>();
2022-03-21 00:07:29 +00:00
// properties
int id;
2022-03-21 00:07:29 +00:00
bool deleted;
@Unique()
String name;
bool active;
String? notes;
String? source;
2022-03-21 00:07:29 +00:00
// constructor
BolusProfile({
this.id = 0,
2022-03-21 00:07:29 +00:00
this.deleted = false,
this.name = '',
this.active = false,
this.notes,
this.source,
});
2022-03-21 00:07:29 +00:00
// methods
static BolusProfile? get(int id) => box.get(id);
static void put(BolusProfile bolusProfile) => box.put(bolusProfile);
2022-03-21 00:07:29 +00:00
static List<BolusProfile> getAll() {
QueryBuilder<BolusProfile> all =
box.query(BolusProfile_.deleted.equals(false))..order(BolusProfile_.name);
return all.build().find();
}
static void remove(int id) {
final item = box.get(id);
if (item != null) {
item.deleted = true;
Bolus.removeAllForProfile(id);
2022-03-21 00:07:29 +00:00
box.put(item);
}
}
static int activeCount() {
2022-03-21 00:07:29 +00:00
Query<BolusProfile> query = box
.query(BolusProfile_.active
.equals(true)
.and(BolusProfile_.deleted.equals(false)))
.build();
return query.find().length;
}
static void setAllInactive() {
box.putMany(box.getAll().map((element) {
element.active = false;
return element;
}).toList());
2021-10-22 23:08:09 +00:00
}
2022-03-21 00:07:29 +00:00
static BolusProfile? getActive(DateTime? dateTime) {
if (dateTime != null) {
List<LogEvent> activeEvents = LogEvent.getAllActiveForTime(dateTime)
.where((event) => event.bolusProfile.target != null).toList();
if (activeEvents.length > 1) {
final now = DateTime.now();
activeEvents =
activeEvents.where((item) => !activeEvents.any((other) =>
item.time.isBefore(other.time) || (item.endTime ?? now).isAfter(other.endTime ?? now)
)).toList();
}
if (activeEvents.length == 1) {
return activeEvents.single.bolusProfile.target;
}
}
Query<BolusProfile> query = box
.query(BolusProfile_.active
.equals(true)
.and(BolusProfile_.deleted.equals(false)))
.build();
final result = query.find();
return result.length != 1 ? null : result.single;
}
@override
String toString() {
return name;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['deleted'] = deleted;
data['name'] = name;
data['active'] = active;
data['notes'] = notes;
return data;
}
static String? putFromJson(
Map<String, dynamic> json, bool overrideExisting, String? source) {
final bolusProfile = BolusProfile(
id: overrideExisting ? json['id'] : 0,
deleted: json['deleted'] == 'true',
name: json['name'],
active: json['active'],
notes: json['notes'],
source: source,
);
BolusProfile.put(bolusProfile);
return null;
}
2021-10-22 23:08:09 +00:00
}