2021-11-07 20:13:28 +00:00
|
|
|
import 'package:diameter/main.dart';
|
2021-11-25 18:25:13 +00:00
|
|
|
import 'package:objectbox/objectbox.dart';
|
2021-11-26 23:01:12 +00:00
|
|
|
import 'package:diameter/objectbox.g.dart' show Accuracy_;
|
2021-10-22 23:08:09 +00:00
|
|
|
|
2021-11-25 18:25:13 +00:00
|
|
|
@Entity(uid: 291512798403320400)
|
2021-10-22 23:08:09 +00:00
|
|
|
class Accuracy {
|
2021-11-07 20:13:28 +00:00
|
|
|
static final Box<Accuracy> box = objectBox.store.box<Accuracy>();
|
|
|
|
|
2021-11-26 23:01:12 +00:00
|
|
|
// properties
|
2021-11-07 20:13:28 +00:00
|
|
|
int id;
|
2021-11-26 23:01:12 +00:00
|
|
|
bool deleted;
|
2021-11-07 20:13:28 +00:00
|
|
|
String value;
|
|
|
|
bool forCarbsRatio;
|
|
|
|
bool forPortionSize;
|
|
|
|
int? confidenceRating;
|
|
|
|
String? notes;
|
|
|
|
|
2021-11-26 23:01:12 +00:00
|
|
|
// constructor
|
2021-11-07 20:13:28 +00:00
|
|
|
Accuracy({
|
|
|
|
this.id = 0,
|
2021-11-26 23:01:12 +00:00
|
|
|
this.deleted = false,
|
2021-11-07 20:13:28 +00:00
|
|
|
this.value = '',
|
|
|
|
this.forCarbsRatio = false,
|
|
|
|
this.forPortionSize = false,
|
|
|
|
this.confidenceRating,
|
|
|
|
this.notes,
|
|
|
|
});
|
|
|
|
|
2021-11-26 23:01:12 +00:00
|
|
|
// methods
|
2021-11-07 20:13:28 +00:00
|
|
|
static Accuracy? get(int id) => box.get(id);
|
2021-11-26 23:01:12 +00:00
|
|
|
static void put(Accuracy accuracy) => box.put(accuracy);
|
|
|
|
|
2021-11-07 20:13:28 +00:00
|
|
|
static List<Accuracy> getAll() {
|
2021-12-04 23:44:46 +00:00
|
|
|
QueryBuilder<Accuracy> all = box.query(Accuracy_.deleted.equals(false))
|
2021-11-26 23:01:12 +00:00
|
|
|
..order(Accuracy_.confidenceRating);
|
2021-11-07 20:13:28 +00:00
|
|
|
return all.build().find();
|
|
|
|
}
|
2021-12-04 23:44:46 +00:00
|
|
|
|
2021-11-26 23:01:12 +00:00
|
|
|
static void remove(int id) {
|
|
|
|
final item = box.get(id);
|
|
|
|
if (item != null) {
|
|
|
|
item.deleted = true;
|
|
|
|
box.put(item);
|
|
|
|
}
|
|
|
|
}
|
2021-12-04 23:44:46 +00:00
|
|
|
|
2021-11-07 20:13:28 +00:00
|
|
|
static List<Accuracy> getAllForPortionSize() {
|
2021-12-04 23:44:46 +00:00
|
|
|
QueryBuilder<Accuracy> allForPortionSize = box.query(
|
|
|
|
Accuracy_.forPortionSize.equals(true) & Accuracy_.deleted.equals(false))
|
2021-11-07 20:13:28 +00:00
|
|
|
..order(Accuracy_.confidenceRating);
|
|
|
|
return allForPortionSize.build().find();
|
|
|
|
}
|
|
|
|
|
|
|
|
static List<Accuracy> getAllForCarbsRatio() {
|
2021-12-04 23:44:46 +00:00
|
|
|
QueryBuilder<Accuracy> allForCarbsRatio = box.query(
|
|
|
|
Accuracy_.forCarbsRatio.equals(true) & Accuracy_.deleted.equals(false))
|
2021-11-07 20:13:28 +00:00
|
|
|
..order(Accuracy_.confidenceRating);
|
|
|
|
return allForCarbsRatio.build().find();
|
2021-10-22 23:08:09 +00:00
|
|
|
}
|
2021-11-26 23:01:12 +00:00
|
|
|
|
2021-12-04 23:44:46 +00:00
|
|
|
static void reorder(Accuracy accuracy, int? newPosition) {
|
|
|
|
QueryBuilder<Accuracy> all = box.query(Accuracy_.deleted.equals(false).and(Accuracy_.id.notEquals(accuracy.id)))
|
|
|
|
..order(Accuracy_.confidenceRating);
|
|
|
|
List<Accuracy> accuracies = all.build().find();
|
|
|
|
newPosition == null || newPosition >= accuracies.length ? accuracies.add(accuracy) : accuracies.insert(newPosition, accuracy);
|
|
|
|
box.putMany(accuracies.map((item) {
|
|
|
|
item.confidenceRating = accuracies.indexOf(item);
|
|
|
|
return item;
|
|
|
|
}).toList());
|
|
|
|
}
|
|
|
|
|
2021-11-26 23:01:12 +00:00
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return value;
|
|
|
|
}
|
2021-10-22 23:08:09 +00:00
|
|
|
}
|