2021-12-11 01:30:46 +00:00
|
|
|
import 'package:diameter/main.dart';
|
2021-12-11 21:02:35 +00:00
|
|
|
import 'package:diameter/models/ingredient.dart';
|
2021-12-11 01:30:46 +00:00
|
|
|
import 'package:diameter/models/meal.dart';
|
2021-12-11 21:02:35 +00:00
|
|
|
import 'package:diameter/utils/utils.dart';
|
2021-12-11 01:30:46 +00:00
|
|
|
import 'package:objectbox/objectbox.dart';
|
|
|
|
import 'package:diameter/objectbox.g.dart' show Recipe_;
|
|
|
|
|
|
|
|
@Entity(uid: 6497942314956341514)
|
|
|
|
@Sync()
|
|
|
|
class Recipe {
|
|
|
|
static final Box<Recipe> box = objectBox.store.box<Recipe>();
|
|
|
|
|
|
|
|
// properties
|
|
|
|
int id;
|
|
|
|
bool deleted;
|
|
|
|
String name;
|
2021-12-11 21:02:35 +00:00
|
|
|
double? servings;
|
2021-12-11 01:30:46 +00:00
|
|
|
String? notes;
|
|
|
|
|
|
|
|
// relations
|
|
|
|
final portion = ToOne<Meal>();
|
2021-12-11 21:02:35 +00:00
|
|
|
|
2021-12-11 01:30:46 +00:00
|
|
|
// constructor
|
|
|
|
Recipe({
|
|
|
|
this.id = 0,
|
|
|
|
this.deleted = false,
|
|
|
|
this.name = '',
|
2021-12-11 21:02:35 +00:00
|
|
|
this.servings,
|
2021-12-11 01:30:46 +00:00
|
|
|
this.notes,
|
|
|
|
});
|
|
|
|
|
|
|
|
// methods
|
|
|
|
static Recipe? get(int id) => box.get(id);
|
|
|
|
static void put(Recipe recipe) => box.put(recipe);
|
2021-12-11 21:02:35 +00:00
|
|
|
|
2021-12-11 01:30:46 +00:00
|
|
|
static List<Recipe> getAll() {
|
2021-12-11 21:02:35 +00:00
|
|
|
QueryBuilder<Recipe> builder = box.query(Recipe_.deleted.equals(false))
|
|
|
|
..order(Recipe_.name);
|
2021-12-11 01:30:46 +00:00
|
|
|
return builder.build().find();
|
|
|
|
}
|
2021-12-11 21:02:35 +00:00
|
|
|
|
|
|
|
static double? getCarbsPerPortion(int id) {
|
|
|
|
final servings = Recipe.get(id)?.servings;
|
|
|
|
final totalWeight = Ingredient.getTotalWeightForRecipe(id);
|
|
|
|
final carbsRatio = Ingredient.getCarbsRatioForRecipe(id);
|
|
|
|
|
|
|
|
if (servings != null && totalWeight != null && carbsRatio != null) {
|
|
|
|
final portionSize = totalWeight / servings;
|
|
|
|
return Utils.calculateCarbs(carbsRatio, portionSize);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-12-11 01:30:46 +00:00
|
|
|
static void remove(int id) {
|
|
|
|
final item = box.get(id);
|
|
|
|
if (item != null) {
|
|
|
|
item.deleted = true;
|
|
|
|
box.put(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|