70 lines
1.7 KiB
Dart
70 lines
1.7 KiB
Dart
import 'package:diameter/main.dart';
|
|
import 'package:diameter/utils/date_time_utils.dart';
|
|
import 'package:objectbox/objectbox.dart';
|
|
import 'package:diameter/objectbox.g.dart' show LogEntry_;
|
|
|
|
@Entity(uid: 752131069307970560)
|
|
class LogEntry {
|
|
static final Box<LogEntry> box = objectBox.store.box<LogEntry>();
|
|
|
|
// properties
|
|
int id;
|
|
bool deleted;
|
|
@Property(type: PropertyType.date)
|
|
DateTime time;
|
|
int? mgPerDl;
|
|
double? mmolPerL;
|
|
double? bolusGlucose;
|
|
int? delayedBolusDuration;
|
|
double? delayedBolusRate;
|
|
String? notes;
|
|
|
|
// constructor
|
|
LogEntry({
|
|
this.id = 0,
|
|
this.deleted = false,
|
|
required this.time,
|
|
this.mgPerDl,
|
|
this.mmolPerL,
|
|
this.bolusGlucose,
|
|
this.delayedBolusDuration,
|
|
this.delayedBolusRate,
|
|
this.notes,
|
|
});
|
|
|
|
// methods
|
|
static LogEntry? get(int id) => id == 0 ? null : box.get(id);
|
|
static List<LogEntry> getAll() => box.getAll();
|
|
static void put(LogEntry logEntry) => box.put(logEntry);
|
|
|
|
static void remove(int id) {
|
|
final item = box.get(id);
|
|
if (item != null) {
|
|
item.deleted = true;
|
|
box.put(item);
|
|
}
|
|
}
|
|
|
|
static Map<DateTime, List<LogEntry>> getDailyEntryMap() {
|
|
Map<DateTime, List<LogEntry>> dateMap = <DateTime, List<LogEntry>>{};
|
|
|
|
QueryBuilder<LogEntry> allByDate = box.query(LogEntry_.deleted.equals(false))
|
|
..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;
|
|
}
|
|
|
|
|
|
@override
|
|
String toString() {
|
|
return DateTimeUtils.displayDateTime(time);
|
|
}
|
|
}
|