diameter/lib/models/log_entry.dart

62 lines
1.6 KiB
Dart

import 'package:diameter/main.dart';
import 'package:diameter/models/log_event.dart';
import 'package:diameter/models/log_meal.dart';
import 'package:diameter/objectbox.g.dart';
@Entity(uid: 752131069307970560)
class LogEntry {
static final Box<LogEntry> box = objectBox.store.box<LogEntry>();
int id;
@Property(type: PropertyType.date)
DateTime time;
int? mgPerDl;
double? mmolPerL;
double? bolusGlucose;
int? delayedBolusDuration;
double? delayedBolusRate;
String? notes;
@Backlink('logEntry')
final events = ToMany<LogEvent>();
@Backlink('endLogEntry')
final endedEvents = ToMany<LogEvent>();
@Backlink('logEntry')
final meals = ToMany<LogMeal>();
LogEntry({
this.id = 0,
required this.time,
this.mgPerDl,
this.mmolPerL,
this.bolusGlucose,
this.delayedBolusDuration,
this.delayedBolusRate,
this.notes,
});
static LogEntry? get(int id) => box.get(id);
static List<LogEntry> getAll() => box.getAll();
static void put(LogEntry logEntry) => box.put(logEntry);
static void remove(int id) => box.remove(id);
static Map<DateTime, List<LogEntry>> getDailyEntryMap() {
Map<DateTime, List<LogEntry>> dateMap = <DateTime, List<LogEntry>>{};
QueryBuilder<LogEntry> allByDate = box.query()..order(LogEntry_.time, flags: Order.descending);
List<LogEntry> entries = allByDate.build().find();
DateTime? date;
for (LogEntry entry in entries) {
date = DateTime.utc(entry.time.year, entry.time.month, entry.time.day);
dateMap.putIfAbsent(date, () => <LogEntry>[]).add(entry);
}
return dateMap;
}
}