barely working example with Isar
This commit is contained in:
@@ -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<Ingredient> exampleIngredients = [
|
||||
|
||||
final List<Recipe> 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),
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
part 'cooking_step.g.dart';
|
||||
|
||||
@embedded
|
||||
class CookingStep {
|
||||
final String title;
|
||||
final String description;
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
import 'unit.dart';
|
||||
|
||||
part 'ingredient.g.dart';
|
||||
|
||||
@embedded
|
||||
class Ingredient {
|
||||
final String title;
|
||||
List<Unit> possibleUnits;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<IngredientListEntry> ingredients = [];
|
||||
List<CookingStep> 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<Map<String, dynamic>>)
|
||||
// ..steps = _stepsFromMap(json['steps']);
|
||||
difficulty: json['difficulty'] as Difficulty,
|
||||
)
|
||||
..ingredients = _ingredientsFromMap(
|
||||
json['ingredients'] as List<Map<String, dynamic>>)
|
||||
..steps = _stepsFromMap(json['steps']);
|
||||
|
||||
Map<String, dynamic> 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();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
import 'package:isar/isar.dart';
|
||||
|
||||
part 'unit.g.dart';
|
||||
|
||||
@embedded
|
||||
class Unit {
|
||||
final String name;
|
||||
final UnitType type;
|
||||
|
||||
@@ -76,9 +76,10 @@ class _CreateRecipeState extends State<CreateRecipe> {
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.onBackground),
|
||||
),
|
||||
DropdownMenu<Difficulty?>(
|
||||
DropdownMenu<Difficulty>(
|
||||
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),
|
||||
|
||||
@@ -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<Recipe> get _recipesBox => Hive.box(name: 'recipes');
|
||||
static late Directory _dir;
|
||||
static Isar get _isar => Isar.open(
|
||||
schemas: _schemas,
|
||||
directory: _dir.path,
|
||||
);
|
||||
|
||||
static const List<IsarGeneratedSchema> _schemas = [
|
||||
RecipeSchema,
|
||||
];
|
||||
static int get nextRecipeId => _isar.recipes.autoIncrement();
|
||||
|
||||
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);
|
||||
_dir = await getApplicationDocumentsDirectory();
|
||||
|
||||
if (kDebugMode) {
|
||||
_isar.write((isar) => _isar.recipes.putAll(e.exampleRecipes));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
return _isar.recipes
|
||||
.getAll([1, 2])
|
||||
.where((element) => element != null)
|
||||
.cast<Recipe>()
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class RecipeListProvider extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
List<Recipe> get recipes => DbHelper.fetchRecipes();
|
||||
List<Recipe> get recipes => DbHelper.fetchRecipes(); // _recipes;
|
||||
|
||||
void clearRecipes({silent = false}) {
|
||||
_recipes.clear();
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user