-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerViewController.swift
More file actions
240 lines (168 loc) · 8.25 KB
/
TimerViewController.swift
File metadata and controls
240 lines (168 loc) · 8.25 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
//
// TimerViewController.swift
// RowTime
//
// Created by Paul Ventisei on 23/05/2016.
// Copyright © 2016 Paul Ventisei. All rights reserved.
//
import UIKit
protocol InCellTextfieldProtocol {
func didEndEditingCellTextfield(_ cell: TimeTableViewCell)
}
class TimerViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate, InCellTextfieldProtocol {
@IBOutlet weak var DigitalReadout: UILabel!
@IBOutlet weak var CrewNumber: UITextField!
@IBOutlet weak var CrewName: UILabel!
@IBOutlet weak var TimeTable: UITableView!
@IBOutlet weak var RecordButton: UIButton!
let notificationCenter = NotificationCenter.default
var eventId: String = ""
var crew: Crew?
var readOnly: Bool = false //passed from crew list view if crew selected
var timeNow: String = "00:00:00.00" //String for the DigitalReadout
var times = [RecordedTime]() //data for the TimeTableView
override func viewDidLoad() {
super.viewDidLoad()
//handle the text fields through delegates
CrewNumber.delegate = self
if !readOnly {
//set the readout to the curent time
//set interval to keep Digital Readout up to date with current time
updateTime()
Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
} else {
DigitalReadout.text = crew!.elapsedTime
times = (crew!.recordedTimes!)
}
// Do any additional setup after loading the view.
}
@objc func updateTime(){
//set the readout to the curent time
let timeFormatForReadout = DateFormatter()
timeFormatForReadout.dateFormat = "HH:mm:ss"
timeNow = timeFormatForReadout.string(from: Date())
DigitalReadout.text = timeNow
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view delegate and data source function protocols
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
//PV: updated to 1 section
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return times.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
let cellIdentifier = "TimeTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! TimeTableViewCell
//get the time record assigned to this cell
let time = times[(indexPath as NSIndexPath).row]
//print (" print: \(String(describing: time))")
//update the cell properties using the times array
if crew!.stages != nil{
cell.crewStage.text = (crew!.stages![time.stage] as AnyObject).label
}else {
cell.crewStage.text = String(time.stage)
}
// if the crew number field is empty set up the highlight the textfield to be completed
if time.crewNumber == nil {
cell.crewNumberLabel.text = "Add Crew -->"
cell.crewNumberInput.text = ""
cell.crewNumberLabel.textColor = UIColor.red
cell.crewNumberInput.borderStyle = UITextField.BorderStyle.roundedRect
} else {
cell.crewNumberInput.borderStyle = UITextField.BorderStyle.none
cell.crewNumberInput.text = String(time.crewNumber!)
}
let timeFormat = DateFormatter()
timeFormat.dateFormat = "HH:mm:ss.SSS"
timeNow = timeFormat.string(from: time.time as Date)
cell.crewTime.text = timeNow
//update cell properties if observation type is 2 which means it is a cencelled time
if time.obsType == 2{
cell.backgroundColor = UIColor.gray
cell.crewNumberLabel.text = "cancelled time"
}else if time.obsType == 1{
cell.backgroundColor = UIColor.white
cell.crewNumberLabel.text = "cancel request"
}else{
cell.backgroundColor = UIColor.white
}
// Set a cell delegate for the fieldtext input
if cell.delegate == nil {
cell.delegate = self
}
return cell
}
//MARK: UITextFieldDelegate methods
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//Hide the keyboard
textField.resignFirstResponder()
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
notificationCenter.addObserver(
self,
selector: #selector(TimerViewController.textFieldTextChanged(_:)),
name:UITextField.textDidChangeNotification,
object: textField)
}
@objc func textFieldTextChanged (_ notification: Notification) {
let textField = notification.object as! UITextField
if textField === CrewNumber {
CrewName.text = "You chose crew \(String(describing: CrewNumber.text))"
}
}
func textFieldDidEndEditing(_ textField: UITextField) {
notificationCenter.removeObserver(self, name: UITextField.textDidChangeNotification, object: textField)
}
//MARK: InCellTextfieldProtocol functions. called when the textfield inside one of the cells has been updated
func didEndEditingCellTextfield(_ cell: TimeTableViewCell) {
//get the index path for the cell updated and update the times array
// MARK: TODO here is where to add the code to record the time if not already recorded. (add a status field to the RecordedTime class
let indexPathAtTap = TimeTable.indexPath(for: cell)
if cell.crewNumberInput.text != "" {
times[(indexPathAtTap! as NSIndexPath).row].crewNumber = Int(cell.crewNumberInput.text!)
}else{
times[(indexPathAtTap! as NSIndexPath).row].crewNumber = nil
}
print("got to cell delegate with index \(String(describing: indexPathAtTap))")
}
//MARK: Actions
@IBAction func RecordTime(_ sender: UIButton) {
// hide any keyboard
CrewNumber.resignFirstResponder()
//get the time the button was pressed for showing it in the readout
let timeButtonPressed = Date()
let timeFormatForReadout = DateFormatter()
timeFormatForReadout.dateFormat = "HH:mm:ss.SSS"
timeNow = timeFormatForReadout.string(from: timeButtonPressed)
//Stop the clock counting and show the time just recorded, restart the Clock in 3 seconds after the freezing of it
//MARK: TODO put in the stop of the clock and set interval to restart it
DigitalReadout.text = timeNow
//Add a time to the times array and refresh the timeTableView
recordTimeInTableAndServer(timeButtonPressed)
//clear the crewNumber field to prevent accidental re-entry of same crew
CrewNumber.text = ""
}
func recordTimeInTableAndServer(_ time: Date) {
let recordedTime1 = RecordedTime(crewNumber: Int(CrewNumber.text!), eventId: eventId, obsType: 0, stage: 0, time: time, timeId: "RC1", timestamp: "tis")
times.insert(recordedTime1!, at: 0)
let newIndexPath = IndexPath(row: 0, section: 0)
TimeTable.insertRows(at: [newIndexPath], with: .bottom)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}