-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnackVM.swift
More file actions
351 lines (283 loc) · 14.1 KB
/
SnackVM.swift
File metadata and controls
351 lines (283 loc) · 14.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
// SnackVM.swift
// GRPatienLogAndPlan
//
// Created by James Klein on 5/20/15.
// Copyright (c) 2015 ObjectPrism Corp. All rights reserved.
//
import Foundation
public class SnackVM: MealViewModel, MealViewModelDelegate, UITableViewDataSource, UITableViewDelegate, ChoiceItemSelectedDelegate, MedicineItemSelectedDelegate,
AddOnItemSelectedDelegate, LocationSelectedDelegate, ParentInitialsSelectedDelegate, TimeSelectedDelegate, NoteChangedDelegate
{
var snackTime: SnackTime
//let dataStore: DataStore
let snackItemArray: [FoodItem]
var currentSnackItemArray: [FoodItem] = [FoodItem]()
let fruitArray: [FoodItem]
var currentFruitArray: [FoodItem] = [FoodItem]()
var snack: VMSnack
var noteText: String? { return snack.note }
init(dataStore: DataStore, snackTime: SnackTime)
{
self.snackTime = snackTime
switch snackTime {
case SnackTime.Morning:
if let entry = dataStore.currentJournalEntry {
self.snack = VMSnack(fromDataObject: entry.morningSnack )
} else {
self.snack = dataStore.getSnack_Today(snackTime)
}
case .Afternoon:
if let entry = dataStore.currentJournalEntry {
self.snack = VMSnack(fromDataObject: entry.afternoonSnack )
} else {
self.snack = dataStore.getSnack_Today(snackTime)
}
case .Evening:
if let entry = dataStore.currentJournalEntry {
self.snack = VMSnack(fromDataObject: entry.eveningSnack )
} else {
self.snack = dataStore.getSnack_Today(snackTime)
}
}
//self.snack = dataStore.getSnack_Today(snackTime)
self.snackItemArray = dataStore.buildFoodItemArray(filterString: "SnackItem")
self.fruitArray = dataStore.buildFoodItemArray(filterString: "FruitItem")
super.init(dataStore: dataStore)
if snack.snackChoice != nil {
currentSnackItemArray = getFoodItem(snack.snackChoice!, foodItemArray: snackItemArray)
}
else {
currentSnackItemArray = snackItemArray
}
if snack.fruitChoice != nil {
currentFruitArray = getFoodItem(snack.fruitChoice!, foodItemArray: fruitArray)
}
else {
currentFruitArray = fruitArray
}
//TODO: Initialize Parent Array from Profile. No do this in the datastore
}
public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return SnackMenuCategory.count(self.snackTime)
}
public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let menuSection = SnackMenuCategory(value: section, snackTime: self.snackTime){
return menuSection.unselectedHeaderTitle()
} else {
//TODO: handle Index Out of Range error
return nil
}
}
@objc public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let menuSection = SnackMenuCategory(value: section, snackTime: self.snackTime){
switch menuSection {
case .SnackChoice:
return self.currentSnackItemArray.count
// case .Fruit:
// return self.currentFruitArray.count
case .Medicine:
return 1
case .AddOn:
return 1
case .AdditionalInfo:
return 4
}
}
else
{
//TODO: handle Index Out of Range error
//assert(false, "Unimplemented error handler for Index Out of Range.")
return 0
}
}
@objc public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let menuSection = SnackMenuCategory(value: indexPath.section, snackTime: self.snackTime)
switch menuSection! {
case .SnackChoice:
let cell = self.tableCell(tableView, cellForFoodItemAtIndexPath: indexPath, inArray: currentSnackItemArray, foodItemName: self.snack.snackChoice, viewModel: self)
if let choiceCell = cell as? NewChoiceTableViewCell {
//cellForFoodItemAtIndexPath returns a NewChoice... cell or a plain cell, add delegate only to choice cell
choiceCell.segmentSelectionHandler = self
}
return cell
// case .Fruit:
// let cell = self.tableCell(tableView, cellForFoodItemAtIndexPath: indexPath, inArray: currentFruitArray, foodItemName: self.snack.fruitChoice, viewModel: self)
// if let choiceCell = cell as? NewChoiceTableViewCell {
// choiceCell.segmentSelectionHandler = self
// }
// return cell
case .Medicine:
let cell: MedicineTableViewCell = self.tableCell(tableView, cellForMedicineItem: indexPath, medicineText: snack.medicineText!, switchState: self.snack.medicineConsumed!)
cell.medicineTakenHandler = self
return cell
case .AddOn:
//let handler: AddOnItemSelectedDelegate = (self as? AddOnItemSelectedDelegate)!
return tableCell(tableView, cellForAddOnItem: indexPath, addOnText: self.snack.addOnText!, switchState: self.snack.addOnConsumed!, switchSelectionHandler: self)
case .AdditionalInfo:
switch indexPath.row {
case 0:
return tableCell(tableView, cellForParentInitialsItem: indexPath, parentInitialsText: &self.snack.parentInitials, parentSelectionHandler: self)
case 1:
//if let location = {
return tableCell(tableView , cellForLocationItem: indexPath, locationText: &self.snack.location, locationSelectionHandler: self)
// }
// else{
// //set it to
// var defaultLocation = LocationForMeal(rawValue: 0)?.name()
// return tableCell(tableView , cellForLocationItem: indexPath, locationText: defaultLocation, locationSelectionHandler: self)
// }
case 2:
return tableCell(tableView, cellForTimeItem: indexPath, time: &snack.time, timeSelectionHandler: self)
default:
return tableCell(tableView, cellForNoteItem: indexPath)
}
}
}
@objc public func didDeselectRowAtIndexPath (indexPath: NSIndexPath, viewController: UIViewController, choiceTableCell: NewChoiceTableViewCell?) {
//selectedItemTitle = dataSource[indexPath.row]
let menuSection = SnackMenuCategory( value: indexPath.section, snackTime: self.snackTime)
switch menuSection!{
case .SnackChoice:
toggleSelectionArrayAndPropertyInModel(
indexPath,
mutableArray: &self.currentSnackItemArray,
immutableArray: self.snackItemArray,
propertyInModel: &snack.snackChoice,
choiceCell: choiceTableCell
)
case .AdditionalInfo:
if indexPath.row == 3 {
let vc : NoteViewController = viewController.storyboard?.instantiateViewControllerWithIdentifier("NoteViewController") as! NoteViewController
vc.vm = self
vc.noteDelegate = self
viewController.showViewController(vc as UIViewController, sender: vc)
}
default:
return
}
}
//MARK: Data Update Delgate Methods
func choiceItemSelectedHandler(childItemIndex: Int, indexPath: NSIndexPath){
//update current meal item
let menuSection = SnackMenuCategory(value: indexPath.section, snackTime: self.snackTime)
switch menuSection! {
case .SnackChoice:
self.toggleSelectionArrayAndPropertyInModelForSegmentedControl(selectedIndexPath: indexPath, selectedSegment: childItemIndex, mutableArray: &self.currentSnackItemArray, immutableArray: self.snackItemArray, propertyInModel: &self.snack.snackChoice)
// case .Fruit:
// let itemNameAndSelectionName = self.getItemNameAndChoiceItemIndex(selectedIndexPath: indexPath, selectedSegment: childItemIndex, mutableArray: self.currentFruitArray)
// setPropertyInModel(value: itemNameAndSelectionName, propertyInModel: &self.snack.fruitChoice)
default:
return
}
}
func choiceItemSelectedHandler(medicineConsumed: Bool){
setPropertyInModel(boolValue: medicineConsumed, boolPropertyInModel: &self.snack.medicineConsumed)
}
//MARK: Cell entry delegates
func addOnItemSelectedHandler(addOnConsumed: Bool)
{
setPropertyInModel(boolValue: addOnConsumed, boolPropertyInModel: &self.snack.addOnConsumed)
}
func locationSelectedHandler(){
let locations = Location.place
let buttonList = locations
let title = "Location"
// let cancel = "Cancel"
// let firstButtonItem = buttonList[0]
// create controller
let alertController = UIAlertController(title: nil, message: title, preferredStyle: .ActionSheet)
//let button = sender as! UIButton
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
var newTitle : String = ""
// add Action buttons for each set of initials in the list
for s in 0..<buttonList.count {
// var buttonIndex = s
let action = UIAlertAction(title: buttonList[s], style: .Default, handler: {
(alert: UIAlertAction) -> Void in
//send initials updated event
newTitle = buttonList[s]
self.setPropertyInModel(value: newTitle, propertyInModel: &self.snack.location)
self.tableView.reloadData()
})
alertController.addAction(action)
}
self.tableviewController.presentViewController(alertController, animated: true, completion: nil)
}
func parentInitialsSelectedHandler(){
let title = "Parent Initials"
let initialsArray = getParentInitials()
//let returnString: () = self.showAlertForPropertyInput(title, buttonValues: initialsArray, modelProperty: &self.snack.parentInitials)
self.showAlertForPropertyInput(title, buttonValues: initialsArray, modelProperty: &self.snack.parentInitials)
}
//Note Handler
func noteHandler(noteText: String){
setPropertyInModel(value: noteText, propertyInModel: &self.snack.note)
}
func timeSelectedHandler(selectedTime : NSDate){
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = .ShortStyle
let time = dateFormatter.stringFromDate(selectedTime)
setPropertyInModel(value: time, propertyInModel: &self.snack.time)
//setPropertyInModel(dateValue: selectedTime, datePropertyInModel: &self.snack.time)
}
//MARK: Alert View methods
func showAlertForPropertyInput(title: String, buttonValues: [String], inout modelProperty: String?){
// let cancel = "Cancel"
// create controller
let alertController = UIAlertController(title: nil, message: title, preferredStyle: .ActionSheet)
//let button = sender as! UIButton
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
for i in 0 ..< buttonValues.count {
let action = UIAlertAction(title: buttonValues[i], style: .Default, handler: {
(alert: UIAlertAction) -> Void in
self.setPropertyInModel(value: buttonValues[i], propertyInModel: &self.snack.parentInitials)//modelProperty)
//self.setPropertyInModel(value: b!, propertyInModel: &b)//modelProperty)//&self.snack.parentInitials)
self.tableView.reloadData()
})
alertController.addAction(action)
}
self.tableviewController.presentViewController(alertController, animated: true, completion: nil)
}
func showAlertForSaveValidation(title: String, buttonValues: [String]){
var message: String = ""
for i in 0 ..< buttonValues.count {
let newLine = "\n \(buttonValues[i])"
message += newLine
}
// create controller
let alertController = UIAlertController(title: "Meal Log Not Complete", message: message, preferredStyle: .ActionSheet)
//let button = sender as! UIButton
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
let saveAnywayAction = UIAlertAction(title: "Save Anyway", style: .Default) {
(alert: UIAlertAction) -> Void in
self.dataStore.saveSnack_Today(self.snack, snackTime: self.snackTime)
self.tableviewController.navigationController?.popViewControllerAnimated(true)
}
alertController.addAction(saveAnywayAction)
self.tableviewController.presentViewController(alertController, animated: true, completion: nil)
}
//MARK: Save button
func saveButtonTapped() {//-> ValidationResult {
//validate
let result = self.snack.validate()
switch result{
case .Success:
// send save message to data store
//dismiss view
dataStore.saveSnack_Today(self.snack, snackTime: self.snackTime)
tableviewController.navigationController?.popViewControllerAnimated(true)
case let .Failure(errorCodes):
showAlertForSaveValidation("Save Error", buttonValues: errorCodes)
}
}
}