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/
.history
.svn/
/lib/models/*.g.dart
# IntelliJ related
*.iml

View File

@@ -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),

View File

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

View File

@@ -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 }

View File

@@ -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;

View File

@@ -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;

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 '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();
}

View File

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

View File

@@ -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),

View File

@@ -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();
}
}

View File

@@ -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();

View File

@@ -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;

View File

@@ -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: