From d1da47499811fb5dbad14ecf0f6b8c173bb02445 Mon Sep 17 00:00:00 2001 From: SomnusVeritas Date: Mon, 6 Nov 2023 18:19:52 +0100 Subject: [PATCH] barely functional database with hive --- android/app/build.gradle | 2 +- lib/main.dart | 5 +++- lib/models/cooking_step.dart | 10 +++++++ lib/models/ingredient.dart | 15 ++++++++++ lib/models/ingredient_list_entry.dart | 15 ++++++++++ lib/models/recipe.dart | 28 ++++++++++++++++-- lib/models/unit.dart | 12 ++++++++ lib/services/providers/db/dbhelper.dart | 29 +++++++++++++++++++ .../providers/recipe_list_provider.dart | 3 +- 9 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 lib/services/providers/db/dbhelper.dart diff --git a/android/app/build.gradle b/android/app/build.gradle index 145f023..a4dd702 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -45,7 +45,7 @@ android { applicationId "com.example.rezepte" // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration. - minSdkVersion flutter.minSdkVersion + minSdkVersion 23 targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/lib/main.dart b/lib/main.dart index 491726d..d625ea2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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(), diff --git a/lib/models/cooking_step.dart b/lib/models/cooking_step.dart index a1258f2..df8e549 100644 --- a/lib/models/cooking_step.dart +++ b/lib/models/cooking_step.dart @@ -3,4 +3,14 @@ class CookingStep { final String description; CookingStep({required this.title, this.description = ''}); + + factory CookingStep.fromJson(Map json) => CookingStep( + title: json['title'] as String, + description: json['description'] as String, + ); + + Map toJson() => { + 'title': title, + 'description': description, + }; } diff --git a/lib/models/ingredient.dart b/lib/models/ingredient.dart index 4870f13..d7244ba 100644 --- a/lib/models/ingredient.dart +++ b/lib/models/ingredient.dart @@ -11,6 +11,18 @@ class Ingredient { this.preferredBrands = const [], }); + factory Ingredient.fromJson(Map json) => Ingredient( + title: json['title'] as String, + possibleUnits: _unitsFromJson(json['possibleUnits']), + preferredBrands: json['preferredBrands'] as List, + ); + + Map 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 _unitsFromJson(List> jsonList) => + jsonList.map((e) => Unit.fromJson(e)).toList(); } diff --git a/lib/models/ingredient_list_entry.dart b/lib/models/ingredient_list_entry.dart index ccb5531..03888f1 100644 --- a/lib/models/ingredient_list_entry.dart +++ b/lib/models/ingredient_list_entry.dart @@ -9,6 +9,21 @@ class IngredientListEntry { IngredientListEntry(this.ingredient, this.amount, this.unit, this.optional); + factory IngredientListEntry.fromJson(Map json) => + IngredientListEntry( + Ingredient.fromJson(json['ingredient']), + json['amount'] as int, + Unit.fromJson(json['unit']), + json['optional'] as bool, + ); + + Map toJson() => { + 'ingredient': ingredient.toJson(), + 'amount': amount, + 'unit': unit.toJson(), + 'optional': optional, + }; + @override operator ==(Object other) { final i = other as IngredientListEntry; diff --git a/lib/models/recipe.dart b/lib/models/recipe.dart index 69ccaf0..d8485f4 100644 --- a/lib/models/recipe.dart +++ b/lib/models/recipe.dart @@ -6,8 +6,8 @@ class Recipe { String title; String description; Difficulty? difficulty; - final List ingredients = []; - final List steps = []; + List ingredients = []; + List 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>) + // ..steps = _stepsFromMap(json['steps']); + + Map 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 _ingredientsFromMap( + List> jsonList) => + jsonList.map((e) => IngredientListEntry.fromJson(e)).toList(); + + static List _stepsFromMap(List> jsonList) => + jsonList.map((e) => CookingStep.fromJson(e)).toList(); } diff --git a/lib/models/unit.dart b/lib/models/unit.dart index eb584ab..e287460 100644 --- a/lib/models/unit.dart +++ b/lib/models/unit.dart @@ -4,6 +4,18 @@ class Unit { final System system; Unit(this.name, this.type, {this.system = System.metric}); + + factory Unit.fromJson(Map json) => Unit( + json['name'] as String, + json['type'] as UnitType, + system: json['system'] as System, + ); + + Map toJson() => { + 'name': name, + 'type': type, + 'system': system, + }; } enum System { metric, imperial, neutral } diff --git a/lib/services/providers/db/dbhelper.dart b/lib/services/providers/db/dbhelper.dart new file mode 100644 index 0000000..2169b08 --- /dev/null +++ b/lib/services/providers/db/dbhelper.dart @@ -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 get _recipesBox => Hive.box(name: 'recipes'); + + static Future 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 fetchRecipes() { + List recipes = _recipesBox.getAll(['0', '1']).cast(); + + return recipes; + } + + static void insertRecipe(Recipe recipe) { + _recipesBox.put(_recipesBox.length.toString(), recipe); + } +} diff --git a/lib/services/providers/recipe_list_provider.dart b/lib/services/providers/recipe_list_provider.dart index b400d12..ddd8f68 100644 --- a/lib/services/providers/recipe_list_provider.dart +++ b/lib/services/providers/recipe_list_provider.dart @@ -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 get recipes => _recipes; + List get recipes => DbHelper.fetchRecipes(); void clearRecipes({silent = false}) { _recipes.clear();