diff options
author | davidpkj <davidpenkow1@gmail.com> | 2022-07-17 19:25:26 +0200 |
---|---|---|
committer | davidpkj <davidpenkow1@gmail.com> | 2022-07-17 19:25:26 +0200 |
commit | d282f4bb380ce9c445d6bd3a4c9f001bb6b5f501 (patch) | |
tree | 023428b7fa249b66a34d0d83c2f0df0ea572ba75 /lib/models |
Initial Commit
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/data/recipe_data_class.dart | 52 | ||||
-rw-r--r-- | lib/models/data/settings_data_class.dart | 33 | ||||
-rw-r--r-- | lib/models/data/shoplist_data_class.dart | 31 | ||||
-rw-r--r-- | lib/models/recipe_class.dart | 63 |
4 files changed, 179 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"], + )); + }); + } +} diff --git a/lib/models/data/settings_data_class.dart b/lib/models/data/settings_data_class.dart new file mode 100644 index 0000000..e8cd604 --- /dev/null +++ b/lib/models/data/settings_data_class.dart @@ -0,0 +1,33 @@ +import 'dart:convert'; + +import 'package:kulinar_app/util/storage_handler.dart'; + +class SettingsData { + static Map<String, String> settings = { + "showPhotos": "0", + "photoSource": "1", + "serverURL": "", + }; + + static Future<void> save() async { + await StorageHandler.store("settings", encode()); + } + + static Future<void> load() async { + decode(await StorageHandler.fetch("settings") ?? "{}"); + } + + static String encode() { + return jsonEncode(settings); + } + + static void decode(String data) { + final _result = jsonDecode(data); + + if (_result.isEmpty) return; + + _result.forEach((key, value) { + settings[key] = value; + }); + } +} diff --git a/lib/models/data/shoplist_data_class.dart b/lib/models/data/shoplist_data_class.dart new file mode 100644 index 0000000..3956282 --- /dev/null +++ b/lib/models/data/shoplist_data_class.dart @@ -0,0 +1,31 @@ +import 'dart:convert'; + +import 'package:kulinar_app/util/storage_handler.dart'; + +class ShoplistData { + static List<String> shoplist = []; + static List<String> removed = []; + + static Future<void> save() async { + print("asd"); + await StorageHandler.store("shoplist", encode()); + } + + static Future<void> load() async { + decode(await StorageHandler.fetch("shoplist") ?? "[]"); + } + + static String encode() { + return jsonEncode(shoplist); + } + + static void decode(String data) { + final _result = jsonDecode(data); + + if (_result.isEmpty) return; + + _result.forEach((item) { + shoplist.add(item); + }); + } +} diff --git a/lib/models/recipe_class.dart b/lib/models/recipe_class.dart new file mode 100644 index 0000000..6873a1f --- /dev/null +++ b/lib/models/recipe_class.dart @@ -0,0 +1,63 @@ +import 'dart:convert'; + +import 'package:kulinar_app/models/data/recipe_data_class.dart'; + +class Recipe { + String? title; + String? image; + String? description; + bool favorite; + int rating; + + Recipe({this.title, this.image, this.description, this.favorite = false, this.rating = 0}); + + bool isListed({bool remote = false}) { + List<Recipe> _list = remote ? RecipeData.remoteRecipeList : RecipeData.recipeList; + + for (Recipe recipe in _list) { + if (this.title == recipe.title && this.image == recipe.image && this.description == recipe.description) { + return true; + } + } + + return false; + } + + bool isDefault() { + if (this.title != null) return false; + if (this.image != null) return false; + if (this.description != null) return false; + if (this.favorite != false) return false; + if (this.rating != 0) return false; + + return true; + } + + static Recipe fromJson(String string) { + final json = jsonDecode(string); + + return Recipe(title: json["title"], image: json["image"], description: json["description"]); + } + + String toJsonString() { + Map<String, String> map = { + "title": this.title ?? "", + "image": this.image ?? "", + "description": this.description ?? "", + }; + + return jsonEncode(map); + } + + void toggleFavorite() { + this.favorite = !this.favorite; + } + + void updateRating() { + if (this.rating == 5) { + this.rating = 0; + } else { + this.rating++; + } + } +} |