barely functional database with hive
This commit is contained in:
@@ -45,7 +45,7 @@ android {
|
|||||||
applicationId "com.example.rezepte"
|
applicationId "com.example.rezepte"
|
||||||
// You can update the following values to match your application needs.
|
// 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.
|
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
|
||||||
minSdkVersion flutter.minSdkVersion
|
minSdkVersion 23
|
||||||
targetSdkVersion flutter.targetSdkVersion
|
targetSdkVersion flutter.targetSdkVersion
|
||||||
versionCode flutterVersionCode.toInteger()
|
versionCode flutterVersionCode.toInteger()
|
||||||
versionName flutterVersionName
|
versionName flutterVersionName
|
||||||
|
|||||||
@@ -6,9 +6,12 @@ import 'package:rezepte/services/providers/recipe_list_provider.dart';
|
|||||||
import 'package:rezepte/services/providers/recipe_provider.dart';
|
import 'package:rezepte/services/providers/recipe_provider.dart';
|
||||||
|
|
||||||
import 'pages/recipe_detail_page.dart';
|
import 'pages/recipe_detail_page.dart';
|
||||||
|
import 'services/providers/db/dbhelper.dart';
|
||||||
import 'theme.dart';
|
import 'theme.dart';
|
||||||
|
|
||||||
void main() {
|
void main() async {
|
||||||
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
await DbHelper.init();
|
||||||
runApp(MultiProvider(providers: [
|
runApp(MultiProvider(providers: [
|
||||||
ChangeNotifierProvider(
|
ChangeNotifierProvider(
|
||||||
create: (_) => RecipeListProvider(),
|
create: (_) => RecipeListProvider(),
|
||||||
|
|||||||
@@ -3,4 +3,14 @@ class CookingStep {
|
|||||||
final String description;
|
final String description;
|
||||||
|
|
||||||
CookingStep({required this.title, this.description = ''});
|
CookingStep({required this.title, this.description = ''});
|
||||||
|
|
||||||
|
factory CookingStep.fromJson(Map<String, dynamic> json) => CookingStep(
|
||||||
|
title: json['title'] as String,
|
||||||
|
description: json['description'] as String,
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'title': title,
|
||||||
|
'description': description,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,18 @@ class Ingredient {
|
|||||||
this.preferredBrands = const [],
|
this.preferredBrands = const [],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
factory Ingredient.fromJson(Map<String, dynamic> json) => Ingredient(
|
||||||
|
title: json['title'] as String,
|
||||||
|
possibleUnits: _unitsFromJson(json['possibleUnits']),
|
||||||
|
preferredBrands: json['preferredBrands'] as List<String>,
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'title': title,
|
||||||
|
'possibleUnits': possibleUnits.map((e) => e.toJson()).toList(),
|
||||||
|
'preferredBrands': preferredBrands,
|
||||||
|
};
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool operator ==(other) {
|
bool operator ==(other) {
|
||||||
Ingredient i = other as Ingredient;
|
Ingredient i = other as Ingredient;
|
||||||
@@ -21,4 +33,7 @@ class Ingredient {
|
|||||||
int get hashCode {
|
int get hashCode {
|
||||||
return Object.hash(title, null);
|
return Object.hash(title, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static List<Unit> _unitsFromJson(List<Map<String, dynamic>> jsonList) =>
|
||||||
|
jsonList.map((e) => Unit.fromJson(e)).toList();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,21 @@ class IngredientListEntry {
|
|||||||
|
|
||||||
IngredientListEntry(this.ingredient, this.amount, this.unit, this.optional);
|
IngredientListEntry(this.ingredient, this.amount, this.unit, this.optional);
|
||||||
|
|
||||||
|
factory IngredientListEntry.fromJson(Map<String, dynamic> json) =>
|
||||||
|
IngredientListEntry(
|
||||||
|
Ingredient.fromJson(json['ingredient']),
|
||||||
|
json['amount'] as int,
|
||||||
|
Unit.fromJson(json['unit']),
|
||||||
|
json['optional'] as bool,
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'ingredient': ingredient.toJson(),
|
||||||
|
'amount': amount,
|
||||||
|
'unit': unit.toJson(),
|
||||||
|
'optional': optional,
|
||||||
|
};
|
||||||
|
|
||||||
@override
|
@override
|
||||||
operator ==(Object other) {
|
operator ==(Object other) {
|
||||||
final i = other as IngredientListEntry;
|
final i = other as IngredientListEntry;
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ class Recipe {
|
|||||||
String title;
|
String title;
|
||||||
String description;
|
String description;
|
||||||
Difficulty? difficulty;
|
Difficulty? difficulty;
|
||||||
final List<IngredientListEntry> ingredients = [];
|
List<IngredientListEntry> ingredients = [];
|
||||||
final List<CookingStep> steps = [];
|
List<CookingStep> steps = [];
|
||||||
|
|
||||||
Recipe({
|
Recipe({
|
||||||
required this.title,
|
required this.title,
|
||||||
@@ -15,6 +15,23 @@ class Recipe {
|
|||||||
this.difficulty,
|
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<Map<String, dynamic>>)
|
||||||
|
// ..steps = _stepsFromMap(json['steps']);
|
||||||
|
|
||||||
|
Map<String, dynamic> 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) =>
|
void addIngredient(IngredientListEntry ingredient) =>
|
||||||
ingredients.add(ingredient);
|
ingredients.add(ingredient);
|
||||||
|
|
||||||
@@ -51,4 +68,11 @@ class Recipe {
|
|||||||
ingredients.clear();
|
ingredients.clear();
|
||||||
steps.clear();
|
steps.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static List<IngredientListEntry> _ingredientsFromMap(
|
||||||
|
List<Map<String, dynamic>> jsonList) =>
|
||||||
|
jsonList.map((e) => IngredientListEntry.fromJson(e)).toList();
|
||||||
|
|
||||||
|
static List<CookingStep> _stepsFromMap(List<Map<String, dynamic>> jsonList) =>
|
||||||
|
jsonList.map((e) => CookingStep.fromJson(e)).toList();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,18 @@ class Unit {
|
|||||||
final System system;
|
final System system;
|
||||||
|
|
||||||
Unit(this.name, this.type, {this.system = System.metric});
|
Unit(this.name, this.type, {this.system = System.metric});
|
||||||
|
|
||||||
|
factory Unit.fromJson(Map<String, dynamic> json) => Unit(
|
||||||
|
json['name'] as String,
|
||||||
|
json['type'] as UnitType,
|
||||||
|
system: json['system'] as System,
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'name': name,
|
||||||
|
'type': type,
|
||||||
|
'system': system,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
enum System { metric, imperial, neutral }
|
enum System { metric, imperial, neutral }
|
||||||
|
|||||||
29
lib/services/providers/db/dbhelper.dart
Normal file
29
lib/services/providers/db/dbhelper.dart
Normal file
@@ -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<Recipe> get _recipesBox => Hive.box(name: 'recipes');
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:rezepte/services/providers/db/dbhelper.dart';
|
||||||
|
|
||||||
import '../../models/recipe.dart';
|
import '../../models/recipe.dart';
|
||||||
import 'package:rezepte/example_data.dart' as e;
|
import 'package:rezepte/example_data.dart' as e;
|
||||||
@@ -12,7 +13,7 @@ class RecipeListProvider extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Recipe> get recipes => _recipes;
|
List<Recipe> get recipes => DbHelper.fetchRecipes();
|
||||||
|
|
||||||
void clearRecipes({silent = false}) {
|
void clearRecipes({silent = false}) {
|
||||||
_recipes.clear();
|
_recipes.clear();
|
||||||
|
|||||||
Reference in New Issue
Block a user