aboutsummaryrefslogtreecommitdiff
path: root/lib/util
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util')
-rw-r--r--lib/util/isar_handler.dart41
-rw-r--r--lib/util/storage_handler.dart2
2 files changed, 42 insertions, 1 deletions
diff --git a/lib/util/isar_handler.dart b/lib/util/isar_handler.dart
new file mode 100644
index 0000000..8e26fc9
--- /dev/null
+++ b/lib/util/isar_handler.dart
@@ -0,0 +1,41 @@
+import 'package:isar/isar.dart';
+
+import 'package:kulinar_app/models/recipe_class.dart';
+
+/// Handles user data persistence related app storage
+class IsarHandler {
+ late Future<Isar> db;
+
+ IsarHandler() {
+ db = _openDB();
+ }
+
+ /// Stores the given `data` as a string with the `key`.
+ Future<int> save(Recipe recipe) async {
+ final isar = await db;
+ return await isar.writeTxn<int>(() => isar.recipes.put(recipe));
+ }
+
+ /// Fetches all recipes
+ Future<List<Recipe>> load() async {
+ final isar = await db;
+ return await isar.recipes.where().findAll();
+ }
+
+ /// Fetches the Recipe related to the given `title`.
+ Future<Recipe?> fetch(String title) async {
+ final isar = await db;
+ return await isar.recipes.filter().titleEqualTo(title).findFirst();
+ }
+
+ Future<Isar> _openDB() async {
+ if (Isar.instanceNames.isEmpty) {
+ return await Isar.open(
+ [RecipeSchema],
+ inspector: true,
+ );
+ }
+
+ return Future.value(Isar.getInstance());
+ }
+}
diff --git a/lib/util/storage_handler.dart b/lib/util/storage_handler.dart
index 1e12ed7..fe27b93 100644
--- a/lib/util/storage_handler.dart
+++ b/lib/util/storage_handler.dart
@@ -1,6 +1,6 @@
import 'package:shared_preferences/shared_preferences.dart';
-/// Handles all persistance related app storage
+/// Handles app data persistence related app storage
class StorageHandler {
/// Stores the given `data` as a string with the `key`.
static Future<String> store(String key, String data) async {