From 17d3a125fb6070de397cb7cac042210d4a701152 Mon Sep 17 00:00:00 2001 From: SomnusVeritas Date: Tue, 7 Nov 2023 23:24:18 +0100 Subject: [PATCH] barely working example with Isar --- .gitignore | 1 + lib/example_data.dart | 3 ++ lib/models/cooking_step.dart | 5 +++ lib/models/difficulty.dart | 2 +- lib/models/ingredient.dart | 5 +++ lib/models/ingredient_list_entry.dart | 5 +++ lib/models/recipe.dart | 35 +++++++++++------ lib/models/unit.dart | 5 +++ lib/pages/create_recipe_page.dart | 5 ++- lib/services/providers/db/dbhelper.dart | 39 +++++++++++-------- .../providers/recipe_list_provider.dart | 2 +- lib/services/providers/recipe_provider.dart | 2 +- pubspec.yaml | 3 +- 13 files changed, 78 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 580259d..def5b98 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ .buildlog/ .history .svn/ +/lib/models/*.g.dart # IntelliJ related *.iml diff --git a/lib/example_data.dart b/lib/example_data.dart index 08754e1..58958a9 100644 --- a/lib/example_data.dart +++ b/lib/example_data.dart @@ -1,5 +1,6 @@ import 'package:rezepte/models/difficulty.dart'; import 'package:rezepte/models/unit.dart'; +import 'package:rezepte/services/providers/db/dbhelper.dart'; import 'models/ingredient.dart'; import 'constants.dart' as constants; @@ -30,10 +31,12 @@ final List exampleIngredients = [ final List exampleRecipes = [ Recipe( + id: DbHelper.nextRecipeId, title: 'Wraps', description: 'Nur ein paar Wraps', difficulty: Difficulty.hard), Recipe( + id: DbHelper.nextRecipeId, title: 'Burritos', description: 'Nur ein paar Burritos', difficulty: Difficulty.easy), diff --git a/lib/models/cooking_step.dart b/lib/models/cooking_step.dart index df8e549..453d017 100644 --- a/lib/models/cooking_step.dart +++ b/lib/models/cooking_step.dart @@ -1,3 +1,8 @@ +import 'package:isar/isar.dart'; + +part 'cooking_step.g.dart'; + +@embedded class CookingStep { final String title; final String description; diff --git a/lib/models/difficulty.dart b/lib/models/difficulty.dart index 8465717..b49eca8 100644 --- a/lib/models/difficulty.dart +++ b/lib/models/difficulty.dart @@ -21,4 +21,4 @@ class DifficultyUtil { } // Only use camelCase or UpperCamelCase for names -enum Difficulty { veryEasy, easy, intermediate, hard, veryHard } +enum Difficulty { notSelected, veryEasy, easy, intermediate, hard, veryHard } diff --git a/lib/models/ingredient.dart b/lib/models/ingredient.dart index d7244ba..9efaae8 100644 --- a/lib/models/ingredient.dart +++ b/lib/models/ingredient.dart @@ -1,5 +1,10 @@ +import 'package:isar/isar.dart'; + import 'unit.dart'; +part 'ingredient.g.dart'; + +@embedded class Ingredient { final String title; List possibleUnits; diff --git a/lib/models/ingredient_list_entry.dart b/lib/models/ingredient_list_entry.dart index 03888f1..769f5bc 100644 --- a/lib/models/ingredient_list_entry.dart +++ b/lib/models/ingredient_list_entry.dart @@ -1,6 +1,11 @@ +import 'package:isar/isar.dart'; + import 'ingredient.dart'; import 'unit.dart'; +part 'ingredient_list_entry.g.dart'; + +@embedded class IngredientListEntry { final Ingredient ingredient; final int amount; diff --git a/lib/models/recipe.dart b/lib/models/recipe.dart index d8485f4..8bf135f 100644 --- a/lib/models/recipe.dart +++ b/lib/models/recipe.dart @@ -1,35 +1,46 @@ +import 'package:isar/isar.dart'; +import 'package:rezepte/models/ingredient.dart'; +import 'package:rezepte/models/unit.dart'; + import 'difficulty.dart'; import 'cooking_step.dart'; import 'ingredient_list_entry.dart'; +part 'recipe.g.dart'; + +@collection class Recipe { + final int id; String title; String description; - Difficulty? difficulty; + Difficulty difficulty; List ingredients = []; List steps = []; Recipe({ + required this.id, required this.title, this.description = '', - this.difficulty, + this.difficulty = Difficulty.notSelected, }); factory Recipe.fromJson(json) => Recipe( + id: json['id'] as int, 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']); + difficulty: json['difficulty'] as Difficulty, + ) + ..ingredients = _ingredientsFromMap( + json['ingredients'] as List>) + ..steps = _stepsFromMap(json['steps']); Map toJson() => { + 'id': id, 'title': title, 'description': description, - // 'difficulty': difficulty, - // 'ingredients': ingredients.map((e) => e.toJson()).toList(), - // 'steps': steps.map((e) => e.toJson()).toList(), + 'difficulty': difficulty, + 'ingredients': ingredients.map((e) => e.toJson()).toList(), + 'steps': steps.map((e) => e.toJson()).toList(), }; void addIngredient(IngredientListEntry ingredient) => @@ -52,7 +63,7 @@ class Recipe { bool get isEmpty { return title.isEmpty && description.isEmpty && - difficulty == null && + difficulty == Difficulty.notSelected && ingredients.isEmpty && steps.isEmpty; } @@ -64,7 +75,7 @@ class Recipe { void clear() { title = ''; description = ''; - difficulty = null; + difficulty = Difficulty.notSelected; ingredients.clear(); steps.clear(); } diff --git a/lib/models/unit.dart b/lib/models/unit.dart index e287460..54ea17f 100644 --- a/lib/models/unit.dart +++ b/lib/models/unit.dart @@ -1,3 +1,8 @@ +import 'package:isar/isar.dart'; + +part 'unit.g.dart'; + +@embedded class Unit { final String name; final UnitType type; diff --git a/lib/pages/create_recipe_page.dart b/lib/pages/create_recipe_page.dart index 15c048e..217b474 100644 --- a/lib/pages/create_recipe_page.dart +++ b/lib/pages/create_recipe_page.dart @@ -76,9 +76,10 @@ class _CreateRecipeState extends State { style: TextStyle( color: Theme.of(context).colorScheme.onBackground), ), - DropdownMenu( + DropdownMenu( dropdownMenuEntries: DifficultyUtil.getDropdownList(), - onSelected: (value) => recipe.difficulty = value, + onSelected: (value) => + recipe.difficulty = value ?? Difficulty.notSelected, label: const Text('Difficulty'), textStyle: TextStyle( color: Theme.of(context).colorScheme.onBackground), diff --git a/lib/services/providers/db/dbhelper.dart b/lib/services/providers/db/dbhelper.dart index 2169b08..53b12e3 100644 --- a/lib/services/providers/db/dbhelper.dart +++ b/lib/services/providers/db/dbhelper.dart @@ -1,29 +1,36 @@ -import 'package:hive/hive.dart'; -import 'package:path_provider/path_provider.dart'; +import 'dart:io'; +import 'package:flutter/foundation.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:isar/isar.dart'; import '../../../models/recipe.dart'; import '../../../example_data.dart' as e; class DbHelper { - static Box get _recipesBox => Hive.box(name: 'recipes'); + static late Directory _dir; + static Isar get _isar => Isar.open( + schemas: _schemas, + directory: _dir.path, + ); + + static const List _schemas = [ + RecipeSchema, + ]; + static int get nextRecipeId => _isar.recipes.autoIncrement(); 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); + _dir = await getApplicationDocumentsDirectory(); + + if (kDebugMode) { + _isar.write((isar) => _isar.recipes.putAll(e.exampleRecipes)); } } static List fetchRecipes() { - List recipes = _recipesBox.getAll(['0', '1']).cast(); - - return recipes; - } - - static void insertRecipe(Recipe recipe) { - _recipesBox.put(_recipesBox.length.toString(), recipe); + return _isar.recipes + .getAll([1, 2]) + .where((element) => element != null) + .cast() + .toList(); } } diff --git a/lib/services/providers/recipe_list_provider.dart b/lib/services/providers/recipe_list_provider.dart index ddd8f68..73f59d9 100644 --- a/lib/services/providers/recipe_list_provider.dart +++ b/lib/services/providers/recipe_list_provider.dart @@ -13,7 +13,7 @@ class RecipeListProvider extends ChangeNotifier { notifyListeners(); } - List get recipes => DbHelper.fetchRecipes(); + List get recipes => DbHelper.fetchRecipes(); // _recipes; void clearRecipes({silent = false}) { _recipes.clear(); diff --git a/lib/services/providers/recipe_provider.dart b/lib/services/providers/recipe_provider.dart index f811207..e396221 100644 --- a/lib/services/providers/recipe_provider.dart +++ b/lib/services/providers/recipe_provider.dart @@ -5,7 +5,7 @@ import '../../models/recipe.dart'; class RecipeProvider extends ChangeNotifier { Recipe? _recipe; - Recipe get recipe => _recipe ??= Recipe(title: ''); + Recipe get recipe => _recipe ??= Recipe(id: 0, title: ''); set recipe(Recipe recipe) { _recipe = recipe; diff --git a/pubspec.yaml b/pubspec.yaml index 71cc39f..4deb5ab 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,7 +11,7 @@ dependencies: sdk: flutter provider: ^6.0.5 - hive: ^4.0.0-dev.2 + isar: ^4.0.0-dev.13 isar_flutter_libs: ^4.0.0-dev.13 path_provider: ^2.1.0 @@ -20,6 +20,7 @@ dev_dependencies: sdk: flutter flutter_lints: ^2.0.0 + build_runner: ^2.4.6 flutter: