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

@@ -6,9 +6,12 @@ import 'package:rezepte/services/providers/recipe_list_provider.dart';
import 'package:rezepte/services/providers/recipe_provider.dart';
import 'pages/recipe_detail_page.dart';
import 'services/providers/db/dbhelper.dart';
import 'theme.dart';
void main() {
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await DbHelper.init();
runApp(MultiProvider(providers: [
ChangeNotifierProvider(
create: (_) => RecipeListProvider(),

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 }

View File

@@ -0,0 +1,29 @@
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
import '../../../models/recipe.dart';
import '../../../example_data.dart' as e;
class DbHelper {
static Box<Recipe> get _recipesBox => Hive.box(name: 'recipes');
static Future<void> init() async {
final dir = await getApplicationDocumentsDirectory();
Hive.defaultDirectory = dir.path;
Hive.registerAdapter('Recipe', Recipe.fromJson);
_recipesBox.clear();
for (final recipe in e.exampleRecipes) {
insertRecipe(recipe);
}
}
static List<Recipe> fetchRecipes() {
List<Recipe> recipes = _recipesBox.getAll(['0', '1']).cast<Recipe>();
return recipes;
}
static void insertRecipe(Recipe recipe) {
_recipesBox.put(_recipesBox.length.toString(), recipe);
}
}

View File

@@ -1,4 +1,5 @@
import 'package:flutter/foundation.dart';
import 'package:rezepte/services/providers/db/dbhelper.dart';
import '../../models/recipe.dart';
import 'package:rezepte/example_data.dart' as e;
@@ -12,7 +13,7 @@ class RecipeListProvider extends ChangeNotifier {
notifyListeners();
}
List<Recipe> get recipes => _recipes;
List<Recipe> get recipes => DbHelper.fetchRecipes();
void clearRecipes({silent = false}) {
_recipes.clear();