forked from FohoDuo/FoHo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeSearchData.swift
More file actions
executable file
·152 lines (129 loc) · 4.2 KB
/
RecipeSearchData.swift
File metadata and controls
executable file
·152 lines (129 loc) · 4.2 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// RecipeSearchData.swift
// FoHo
//
// Created by Scott Williams on 4/12/17.
// Copyright © 2017 FohoDuo. All rights reserved.
//
import UIKit
//Object containing specific information for a selected recipe
class RecipeSearchData: NSObject {
var recipe: AnyObject
//constructor
init(recipeData: Any) {
//make constructor happy
recipe = recipeData as AnyObject
//probably redundent but eh...
if let dictionary = recipeData as? Dictionary<String, AnyObject> {
recipe = dictionary as! AnyObject
}
super.init()
}
//gets the name of the recipe
func recipeName() -> String? {
if let name = recipe["name"] {
return name as? String
}
return nil
}
//gets time needed to prep
func prepTime() -> String? {
if let time = recipe["prepTime"] {
return time as? String
}
return nil
}
//gets time needed to total time
func totalTime() -> String? {
//print(recipe["totalTime"])
if let time = recipe["totalTime"] {
return time as? String
}
return nil
}
//gets cook time
func cookTime() -> String? {
if let time = recipe["cookTime"] {
return time as? String
}
return nil
}
//returns number of serving recipe makes
func numberOfServings() -> Int? {
if let number = recipe["numberOfServings"] {
return number as? Int
}
return nil
}
//gets 1-5 star rating
func rating() -> Int? {
if let rate = recipe["rating"] {
return rate as? Int
}
return nil
}
//returns an array of ingredients
///TO-DO test if this returns corrctly
func ingredients() -> [String] {
if let list = recipe["ingredientLines"] {
return (list as? [String])!
}
return [] //return an empty array
}
func webUrl() -> String? {
if let dict = recipe["source"] {
if let dictionary = dict as? Dictionary<String, AnyObject> {
return dictionary["sourceRecipeUrl"] as? String
}
}
return nil
}
//Looks through the available images, and returns the largest image available
func recipeImage() -> UIImage {
if let arr = recipe["images"] {
if let array = arr as? [Dictionary<String, AnyObject>] {
if let uri = array[0]["hostedLargeUrl"]{
print(uri)
if let url = URL(string: uri as! String),
let data = try? Data(contentsOf: url),
let image = UIImage(data: data) {
return image
}
}
else if let uri = array[0]["hostedMediumUrl"] {
if let url = URL(string: uri as! String),
let data = try? Data(contentsOf: url),
let image = UIImage(data: data) {
return image
}
}
else if let uri = array[0]["hostedSmallUrl"] {
if let url = URL(string: uri as! String),
let data = try? Data(contentsOf: url),
let image = UIImage(data: data) {
return image
}
}
}
}
//return some hard-coded thing if there isnt an image
return UIImage(named: "heart.png")!
}
//returns the uri to the image used
func recipeUri() -> String{
if let arr = recipe["images"] {
if let array = arr as? [Dictionary<String, AnyObject>] {
if let uri = array[0]["hostedLargeUrl"]{
return uri as! String
}
else if let uri = array[0]["hostedMediumUrl"] {
return uri as! String
}
else if let uri = array[0]["hostedSmallUrl"] {
return uri as! String
}
}
}
return "heart.png"
}
}