diameter/lib/models/basal_profile.dart

122 lines
3.1 KiB
Dart
Raw Normal View History

import 'package:diameter/main.dart';
import 'package:diameter/models/basal.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 BasalProfile_;
2021-10-22 23:08:09 +00:00
2022-03-21 00:07:29 +00:00
@Entity(uid: 3613736032926903785)
@Sync()
2021-10-22 23:08:09 +00:00
class BasalProfile {
static final Box<BasalProfile> box = objectBox.store.box<BasalProfile>();
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
BasalProfile({
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 BasalProfile? get(int id) => box.get(id);
static void put(BasalProfile basalProfile) => box.put(basalProfile);
2022-03-21 00:07:29 +00:00
static List<BasalProfile> getAll() {
QueryBuilder<BasalProfile> all = box
.query(BasalProfile_.deleted.equals(false))
2022-03-21 00:07:29 +00:00
..order(BasalProfile_.name);
return all.build().find();
}
static void remove(int id) {
final item = box.get(id);
if (item != null) {
item.deleted = true;
Basal.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<BasalProfile> query = box
.query(BasalProfile_.active.equals(true) &
BasalProfile_.deleted.equals(false))
.build();
return query.find().length;
}
static void setAllInactive() {
2022-03-21 00:07:29 +00:00
box.putMany(box.getAll().map((item) {
item.active = false;
return item;
}).toList());
2021-10-22 23:08:09 +00:00
}
2022-03-21 00:07:29 +00:00
static BasalProfile? getActive(DateTime? dateTime) {
if (dateTime != null) {
List<LogEvent> activeEvents = LogEvent.getAllActiveForTime(dateTime)
.where((event) => event.basalProfile.target != null)
.toList();
2022-03-21 00:07:29 +00:00
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();
2022-03-21 00:07:29 +00:00
}
if (activeEvents.length == 1) {
return activeEvents.single.basalProfile.target;
}
}
Query<BasalProfile> query = box
.query(BasalProfile_.active
.equals(true)
.and(BasalProfile_.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 basalProfile = BasalProfile(
id: overrideExisting ? json['id'] : 0,
deleted: json['deleted'] == 'true',
name: json['name'],
active: json['active'] == 'true',
notes: json['notes'],
source: source,
);
BasalProfile.put(basalProfile);
return null;
}
2021-10-22 23:08:09 +00:00
}