|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | import UIKit
|
16 |
| -import Firebase |
| 16 | +import FirebaseRemoteConfig |
| 17 | +import FirebaseRemoteConfigSwift |
17 | 18 |
|
18 | 19 | class RemoteConfigViewController: UIViewController {
|
19 | 20 | private var remoteConfig: RemoteConfig!
|
20 | 21 | private var remoteConfigView: RemoteConfigView { view as! RemoteConfigView }
|
21 | 22 |
|
22 | 23 | private let topLabelKey = "topLabelKey"
|
23 | 24 | private let recipeKey = "recipeKey"
|
| 25 | + // The JSON value for typedRecipeKey match recipeKey except ints are Ints instead of String. |
| 26 | + private let typedRecipeKey = "typedRecipeKey" |
24 | 27 | private let bottomLabelKey = "bottomLabelKey"
|
25 | 28 |
|
26 | 29 | override func loadView() {
|
@@ -136,20 +139,35 @@ class RemoteConfigViewController: UIViewController {
|
136 | 139 | "serving_size",
|
137 | 140 | "notes",
|
138 | 141 | ]
|
139 |
| - guard let recipeDictionary = remoteConfig[recipeKey].jsonValue as? [String: Any] else { return } |
140 |
| - |
141 |
| - keys.enumerated().forEach { index, key in |
142 |
| - guard var value = recipeDictionary[key] else { return } |
| 142 | + struct Recipe: Decodable { |
| 143 | + var recipe_name: String |
| 144 | + var ingredients: [String] |
| 145 | + var prep_time: Int |
| 146 | + var cook_time: Int |
| 147 | + var instructions: [String] |
| 148 | + var yield: String |
| 149 | + var serving_size: Int |
| 150 | + var notes: String |
| 151 | + } |
143 | 152 |
|
| 153 | + guard let recipe: Recipe = try? remoteConfig[typedRecipeKey].decoded() else { |
| 154 | + fatalError("Failed to decode JSON for \(typedRecipeKey)") |
| 155 | + } |
| 156 | + let mirror = Mirror(reflecting: recipe) |
| 157 | + var index = 0 |
| 158 | + for (property, value) in mirror.children { |
| 159 | + guard let key = property else { continue } |
| 160 | + var stringValue: String |
144 | 161 | if let list = value as? [String] {
|
145 |
| - let joinedValue = list.joined(separator: " • ") |
146 |
| - value = joinedValue |
147 |
| - } else if let stringValue = value as? String { |
148 |
| - value = stringValue |
| 162 | + stringValue = list.joined(separator: " • ") |
| 163 | + } else if let intVal = value as? Int { |
| 164 | + stringValue = String(intVal) |
| 165 | + } else if let val = value as? String { |
| 166 | + stringValue = val |
| 167 | + } else { |
| 168 | + fatalError("Unrecognized type for \(key)") |
149 | 169 | }
|
150 | 170 |
|
151 |
| - guard let stringValue = value as? String else { return } |
152 |
| - |
153 | 171 | let formattedKey = key
|
154 | 172 | .capitalized
|
155 | 173 | .replacingOccurrences(of: "_", with: " ")
|
@@ -182,6 +200,7 @@ class RemoteConfigViewController: UIViewController {
|
182 | 200 | let y: CGFloat = step * CGFloat(index) + offset
|
183 | 201 |
|
184 | 202 | label.frame.origin = CGPoint(x: x, y: y)
|
| 203 | + index += 1 |
185 | 204 | }
|
186 | 205 | }
|
187 | 206 |
|
|
0 commit comments