aboutsummaryrefslogtreecommitdiff
path: root/lib/models/recipe_class.dart
blob: 6873a1f6e8a6d3ee5d70865c4ed28f922ebac0fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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++;
    }
  }
}