barely functional database with hive

This commit is contained in:
SomnusVeritas
2023-11-06 18:19:52 +01:00
parent da4dc83193
commit d1da474998
9 changed files with 114 additions and 5 deletions

View File

@@ -3,4 +3,14 @@ class CookingStep {
final String description;
CookingStep({required this.title, this.description = ''});
factory CookingStep.fromJson(Map<String, dynamic> json) => CookingStep(
title: json['title'] as String,
description: json['description'] as String,
);
Map<String, dynamic> toJson() => {
'title': title,
'description': description,
};
}

View File

@@ -11,6 +11,18 @@ class Ingredient {
this.preferredBrands = const [],
});
factory Ingredient.fromJson(Map<String, dynamic> json) => Ingredient(
title: json['title'] as String,
possibleUnits: _unitsFromJson(json['possibleUnits']),
preferredBrands: json['preferredBrands'] as List<String>,
);
Map<String, dynamic> toJson() => {
'title': title,
'possibleUnits': possibleUnits.map((e) => e.toJson()).toList(),
'preferredBrands': preferredBrands,
};
@override
bool operator ==(other) {
Ingredient i = other as Ingredient;
@@ -21,4 +33,7 @@ class Ingredient {
int get hashCode {
return Object.hash(title, null);
}
static List<Unit> _unitsFromJson(List<Map<String, dynamic>> jsonList) =>
jsonList.map((e) => Unit.fromJson(e)).toList();
}

View File

@@ -9,6 +9,21 @@ class IngredientListEntry {
IngredientListEntry(this.ingredient, this.amount, this.unit, this.optional);
factory IngredientListEntry.fromJson(Map<String, dynamic> json) =>
IngredientListEntry(
Ingredient.fromJson(json['ingredient']),
json['amount'] as int,
Unit.fromJson(json['unit']),
json['optional'] as bool,
);
Map<String, dynamic> toJson() => {
'ingredient': ingredient.toJson(),
'amount': amount,
'unit': unit.toJson(),
'optional': optional,
};
@override
operator ==(Object other) {
final i = other as IngredientListEntry;

View File

@@ -6,8 +6,8 @@ class Recipe {
String title;
String description;
Difficulty? difficulty;
final List<IngredientListEntry> ingredients = [];
final List<CookingStep> steps = [];
List<IngredientListEntry> ingredients = [];
List<CookingStep> steps = [];
Recipe({
required this.title,
@@ -15,6 +15,23 @@ class Recipe {
this.difficulty,
});
factory Recipe.fromJson(json) => Recipe(
title: json['title'] as String,
description: json['description'] as String,
// difficulty: json['difficulty'] as Difficulty?,
);
// ..ingredients = _ingredientsFromMap(
// json['ingredients'] as List<Map<String, dynamic>>)
// ..steps = _stepsFromMap(json['steps']);
Map<String, dynamic> toJson() => {
'title': title,
'description': description,
// 'difficulty': difficulty,
// 'ingredients': ingredients.map((e) => e.toJson()).toList(),
// 'steps': steps.map((e) => e.toJson()).toList(),
};
void addIngredient(IngredientListEntry ingredient) =>
ingredients.add(ingredient);
@@ -51,4 +68,11 @@ class Recipe {
ingredients.clear();
steps.clear();
}
static List<IngredientListEntry> _ingredientsFromMap(
List<Map<String, dynamic>> jsonList) =>
jsonList.map((e) => IngredientListEntry.fromJson(e)).toList();
static List<CookingStep> _stepsFromMap(List<Map<String, dynamic>> jsonList) =>
jsonList.map((e) => CookingStep.fromJson(e)).toList();
}

View File

@@ -4,6 +4,18 @@ class Unit {
final System system;
Unit(this.name, this.type, {this.system = System.metric});
factory Unit.fromJson(Map<String, dynamic> json) => Unit(
json['name'] as String,
json['type'] as UnitType,
system: json['system'] as System,
);
Map<String, dynamic> toJson() => {
'name': name,
'type': type,
'system': system,
};
}
enum System { metric, imperial, neutral }