-
Notifications
You must be signed in to change notification settings - Fork 1
/
EditHabitTableViewController.swift
186 lines (141 loc) · 6.46 KB
/
EditHabitTableViewController.swift
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
//
// EditHabitTableViewController.swift
// GreenHomes
//
// Created by Shao Qian MAH on 22/3/2016.
//
//
import UIKit
import CoreData
class EditHabitTableViewController: UITableViewController, UITextFieldDelegate, NSFetchedResultsControllerDelegate {
var indexPath: Int = 0
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var habitNameCell: UITableViewCell!
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var timeSelectLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var timePicker: UIDatePicker!
@IBAction func timePickerValue(sender: UIDatePicker) {
timePickerChanged()
}
var habit: Habit?
var category: String = "Water" {
didSet {
categoryLabel.text? = category
}
}
var timePickerHidden = false
override func viewDidLoad() {
super.viewDidLoad()
// Fetches CoreData object at indexPath
let fetchRequest = NSFetchRequest(entityName: "Habit")
do {
let results = try managedContext.executeFetchRequest(fetchRequest)
habits = results as! [NSManagedObject]
nameTextField.text = habits[indexPath].valueForKey("name") as? String
category = (habits[indexPath].valueForKey("category") as? String)!
timeLabel.text = habits[indexPath].valueForKey("time") as? String
// Set timePicker to time from fetched object
let timeAsString = habits[indexPath].valueForKey("time") as? String
let timeFormatter = NSDateFormatter()
timeFormatter.dateFormat = "h:mm a"
let time = timeFormatter.dateFromString(timeAsString!)
timePicker.date = time!
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
timePickerChanged()
self.nameTextField.delegate = self
nameTextField.returnKeyType = UIReturnKeyType.Done
// Set the background of the habit name cell and time picker cell to none
habitNameCell.selectionStyle = UITableViewCellSelectionStyle.None
toggleTimePickerColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Animates selection
tableView.deselectRowAtIndexPath(indexPath, animated: true)
// Sets keyboard to respond when cell for habit name is clicked
if indexPath.section == 0 {
nameTextField.becomeFirstResponder()
}
// Toggles date picker when when cell for time is clicked
if indexPath.section == 2 && indexPath.row == 0 {
// Shows and hides time picker: toggleTimePicker()
timePickerHidden = !timePickerHidden
tableView.beginUpdates()
tableView.endUpdates()
toggleTimePickerColor()
}
}
// Sets cell background colour according to active state
func toggleTimePickerColor() {
if timePickerHidden == false {
timeSelectLabel.textColor = UIColor(red: 21/255, green: 126/255, blue: 251/255, alpha: 1)
}
if timePickerHidden == true {
timeSelectLabel.textColor = UIColor.blackColor()
}
}
// Changes cell height according to whether or not time picker is hidden
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if timePickerHidden && indexPath.section == 2 && indexPath.row == 1 {
return 0
}
else {
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
}
// Set label text to time picked
func timePickerChanged () {
timeLabel.text = NSDateFormatter.localizedStringFromDate(timePicker.date, dateStyle: NSDateFormatterStyle.NoStyle, timeStyle: NSDateFormatterStyle.ShortStyle)
}
// Textfield editing ends on 'Done/Return'
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
// Category information is passed to this view controller
@IBAction func unwindWithSelectedCategory(segue:UIStoryboardSegue) {
if let categoryTableViewController = segue.sourceViewController as? CategoryTableViewController,
selectedCategory = categoryTableViewController.selectedCategory {
category = selectedCategory
}
}
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if identifier == "saveHabit" {
if (nameTextField.text!.isEmpty) {
let alert = UIAlertController(title: "Missing name", message: "Please add a name for the habit", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
}
alert.addAction(OKAction)
self.presentViewController(alert, animated: true) {
}
return false
}
if categoryLabel.text == "Select" {
let alert = UIAlertController(title: "Missing category", message: "Please select a category for the habit", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
}
alert.addAction(OKAction)
self.presentViewController(alert, animated: true) {
}
return false
}
}
return true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "editHabit" {
habit = Habit(name: nameTextField.text!, category: categoryLabel.text!, time: timeLabel.text)
}
if segue.identifier == "pickCategory" {
if let categoryTableViewController = segue.destinationViewController as? CategoryTableViewController {
categoryTableViewController.selectedCategory = category
}
}
}
}