barely working example with Isar

This commit is contained in:
SomnusVeritas
2023-11-07 23:24:18 +01:00
parent d1da474998
commit 17d3a125fb
13 changed files with 78 additions and 34 deletions

1
.gitignore vendored
View File

@@ -10,6 +10,7 @@
.buildlog/ .buildlog/
.history .history
.svn/ .svn/
/lib/models/*.g.dart
# IntelliJ related # IntelliJ related
*.iml *.iml

View File

@@ -1,5 +1,6 @@
import 'package:rezepte/models/difficulty.dart'; import 'package:rezepte/models/difficulty.dart';
import 'package:rezepte/models/unit.dart'; import 'package:rezepte/models/unit.dart';
import 'package:rezepte/services/providers/db/dbhelper.dart';
import 'models/ingredient.dart'; import 'models/ingredient.dart';
import 'constants.dart' as constants; import 'constants.dart' as constants;
@@ -30,10 +31,12 @@ final List<Ingredient> exampleIngredients = [
final List<Recipe> exampleRecipes = [ final List<Recipe> exampleRecipes = [
Recipe( Recipe(
id: DbHelper.nextRecipeId,
title: 'Wraps', title: 'Wraps',
description: 'Nur ein paar Wraps', description: 'Nur ein paar Wraps',
difficulty: Difficulty.hard), difficulty: Difficulty.hard),
Recipe( Recipe(
id: DbHelper.nextRecipeId,
title: 'Burritos', title: 'Burritos',
description: 'Nur ein paar Burritos', description: 'Nur ein paar Burritos',
difficulty: Difficulty.easy), difficulty: Difficulty.easy),

View File

@@ -1,3 +1,8 @@
import 'package:isar/isar.dart';
part 'cooking_step.g.dart';
@embedded
class CookingStep { class CookingStep {
final String title; final String title;
final String description; final String description;

View File

@@ -21,4 +21,4 @@ class DifficultyUtil {
} }
// Only use camelCase or UpperCamelCase for names // Only use camelCase or UpperCamelCase for names
enum Difficulty { veryEasy, easy, intermediate, hard, veryHard } enum Difficulty { notSelected, veryEasy, easy, intermediate, hard, veryHard }

View File

@@ -1,5 +1,10 @@
import 'package:isar/isar.dart';
import 'unit.dart'; import 'unit.dart';
part 'ingredient.g.dart';
@embedded
class Ingredient { class Ingredient {
final String title; final String title;
List<Unit> possibleUnits; List<Unit> possibleUnits;

View File

@@ -1,6 +1,11 @@
import 'package:isar/isar.dart';
import 'ingredient.dart'; import 'ingredient.dart';
import 'unit.dart'; import 'unit.dart';
part 'ingredient_list_entry.g.dart';
@embedded
class IngredientListEntry { class IngredientListEntry {
final Ingredient ingredient; final Ingredient ingredient;
final int amount; final int amount;

View File

@@ -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 'difficulty.dart';
import 'cooking_step.dart'; import 'cooking_step.dart';
import 'ingredient_list_entry.dart'; import 'ingredient_list_entry.dart';
part 'recipe.g.dart';
@collection
class Recipe { class Recipe {
final int id;
String title; String title;
String description; String description;
Difficulty? difficulty; Difficulty difficulty;
List<IngredientListEntry> ingredients = []; List<IngredientListEntry> ingredients = [];
List<CookingStep> steps = []; List<CookingStep> steps = [];
Recipe({ Recipe({
required this.id,
required this.title, required this.title,
this.description = '', this.description = '',
this.difficulty, this.difficulty = Difficulty.notSelected,
}); });
factory Recipe.fromJson(json) => Recipe( factory Recipe.fromJson(json) => Recipe(
id: json['id'] as int,
title: json['title'] as String, title: json['title'] as String,
description: json['description'] as String, description: json['description'] as String,
// difficulty: json['difficulty'] as Difficulty?, difficulty: json['difficulty'] as Difficulty,
); )
// ..ingredients = _ingredientsFromMap( ..ingredients = _ingredientsFromMap(
// json['ingredients'] as List<Map<String, dynamic>>) json['ingredients'] as List<Map<String, dynamic>>)
// ..steps = _stepsFromMap(json['steps']); ..steps = _stepsFromMap(json['steps']);
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
'id': id,
'title': title, 'title': title,
'description': description, 'description': description,
// 'difficulty': difficulty, 'difficulty': difficulty,
// 'ingredients': ingredients.map((e) => e.toJson()).toList(), 'ingredients': ingredients.map((e) => e.toJson()).toList(),
// 'steps': steps.map((e) => e.toJson()).toList(), 'steps': steps.map((e) => e.toJson()).toList(),
}; };
void addIngredient(IngredientListEntry ingredient) => void addIngredient(IngredientListEntry ingredient) =>
@@ -52,7 +63,7 @@ class Recipe {
bool get isEmpty { bool get isEmpty {
return title.isEmpty && return title.isEmpty &&
description.isEmpty && description.isEmpty &&
difficulty == null && difficulty == Difficulty.notSelected &&
ingredients.isEmpty && ingredients.isEmpty &&
steps.isEmpty; steps.isEmpty;
} }
@@ -64,7 +75,7 @@ class Recipe {
void clear() { void clear() {
title = ''; title = '';
description = ''; description = '';
difficulty = null; difficulty = Difficulty.notSelected;
ingredients.clear(); ingredients.clear();
steps.clear(); steps.clear();
} }

View File

@@ -1,3 +1,8 @@
import 'package:isar/isar.dart';
part 'unit.g.dart';
@embedded
class Unit { class Unit {
final String name; final String name;
final UnitType type; final UnitType type;

View File

@@ -76,9 +76,10 @@ class _CreateRecipeState extends State<CreateRecipe> {
style: TextStyle( style: TextStyle(
color: Theme.of(context).colorScheme.onBackground), color: Theme.of(context).colorScheme.onBackground),
), ),
DropdownMenu<Difficulty?>( DropdownMenu<Difficulty>(
dropdownMenuEntries: DifficultyUtil.getDropdownList(), dropdownMenuEntries: DifficultyUtil.getDropdownList(),
onSelected: (value) => recipe.difficulty = value, onSelected: (value) =>
recipe.difficulty = value ?? Difficulty.notSelected,
label: const Text('Difficulty'), label: const Text('Difficulty'),
textStyle: TextStyle( textStyle: TextStyle(
color: Theme.of(context).colorScheme.onBackground), color: Theme.of(context).colorScheme.onBackground),

View File

@@ -1,29 +1,36 @@
import 'package:hive/hive.dart'; import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:isar/isar.dart';
import '../../../models/recipe.dart'; import '../../../models/recipe.dart';
import '../../../example_data.dart' as e; import '../../../example_data.dart' as e;
class DbHelper { 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 { static Future<void> init() async {
final dir = await getApplicationDocumentsDirectory(); _dir = await getApplicationDocumentsDirectory();
Hive.defaultDirectory = dir.path;
Hive.registerAdapter('Recipe', Recipe.fromJson); if (kDebugMode) {
_recipesBox.clear(); _isar.write((isar) => _isar.recipes.putAll(e.exampleRecipes));
for (final recipe in e.exampleRecipes) {
insertRecipe(recipe);
} }
} }
static List<Recipe> fetchRecipes() { static List<Recipe> fetchRecipes() {
List<Recipe> recipes = _recipesBox.getAll(['0', '1']).cast<Recipe>(); return _isar.recipes
.getAll([1, 2])
return recipes; .where((element) => element != null)
} .cast<Recipe>()
.toList();
static void insertRecipe(Recipe recipe) {
_recipesBox.put(_recipesBox.length.toString(), recipe);
} }
} }

View File

@@ -13,7 +13,7 @@ class RecipeListProvider extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
List<Recipe> get recipes => DbHelper.fetchRecipes(); List<Recipe> get recipes => DbHelper.fetchRecipes(); // _recipes;
void clearRecipes({silent = false}) { void clearRecipes({silent = false}) {
_recipes.clear(); _recipes.clear();

View File

@@ -5,7 +5,7 @@ import '../../models/recipe.dart';
class RecipeProvider extends ChangeNotifier { class RecipeProvider extends ChangeNotifier {
Recipe? _recipe; Recipe? _recipe;
Recipe get recipe => _recipe ??= Recipe(title: ''); Recipe get recipe => _recipe ??= Recipe(id: 0, title: '');
set recipe(Recipe recipe) { set recipe(Recipe recipe) {
_recipe = recipe; _recipe = recipe;

View File

@@ -11,7 +11,7 @@ dependencies:
sdk: flutter sdk: flutter
provider: ^6.0.5 provider: ^6.0.5
hive: ^4.0.0-dev.2 isar: ^4.0.0-dev.13
isar_flutter_libs: ^4.0.0-dev.13 isar_flutter_libs: ^4.0.0-dev.13
path_provider: ^2.1.0 path_provider: ^2.1.0
@@ -20,6 +20,7 @@ dev_dependencies:
sdk: flutter sdk: flutter
flutter_lints: ^2.0.0 flutter_lints: ^2.0.0
build_runner: ^2.4.6
flutter: flutter: