diff options
Diffstat (limited to 'lib/models/data/recipe_data_class.dart')
-rw-r--r-- | lib/models/data/recipe_data_class.dart | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/models/data/recipe_data_class.dart b/lib/models/data/recipe_data_class.dart new file mode 100644 index 0000000..ebbc5e1 --- /dev/null +++ b/lib/models/data/recipe_data_class.dart @@ -0,0 +1,52 @@ +import 'dart:convert'; + +import 'package:kulinar_app/models/recipe_class.dart'; +import 'package:kulinar_app/util/storage_handler.dart'; + +class RecipeData { + // TODO: What is this? + static List<Recipe> remoteRecipeList = []; + static List<Recipe> recipeList = []; + + static Future<void> save() async { + await StorageHandler.store("recipes", encode()); + } + + static Future<void> load() async { + decode(await StorageHandler.fetch("recipes") ?? "[]"); + } + + static String encode() { + List<Map<String, dynamic>> _tempList = []; + + recipeList.forEach((element) { + Map<String, dynamic> _map = Map<String, dynamic>(); + + _map["title"] = element.title; + _map["description"] = element.description; + _map["favorite"] = element.favorite; + _map["rating"] = element.rating; + _map["image"] = element.image; + + _tempList.add(_map); + }); + + return jsonEncode(_tempList); + } + + static void decode(String data) { + final _result = jsonDecode(data); + + if (_result.isEmpty) return; + + _result.forEach((item) { + recipeList.add(Recipe( + title: item["title"], + description: item["description"], + favorite: item["favorite"], + rating: item["rating"], + image: item["image"], + )); + }); + } +} |