-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScheduleCall.swift
382 lines (334 loc) · 12.5 KB
/
ScheduleCall.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// ScheduleCall.swift
// PreSales-Huddle
//
// Created by Himanshu Phirke on 26/07/15.
// Copyright (c) 2015 synerzip. All rights reserved.
//
import UIKit
class ScheduleCall: UIViewController, UITableViewDataSource, UITableViewDelegate, DateSelectorDelegate {
var hud:MBProgressHUD?
var allParticipants = [Participant]()
var prospectID: Int?
let viewParticipantsURL = "participant/view/prospectid/"
let updateParticipantURL = "participant/update/"
let updateProspectURL = "prospect/update/"
var toDate: NSDate = NSDate()
var fromDate: NSDate = NSDate()
// MARK: Outlets
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var participants_selected_count: UILabel!
@IBOutlet weak var selection_note: UILabel!
@IBOutlet weak var done: UIBarButtonItem!
@IBOutlet weak var from_date_label: UILabel!
@IBOutlet weak var to_date_label: UILabel!
@IBOutlet weak var duration_label: UILabel!
@IBOutlet weak var currentTableView: UITableView!
@IBOutlet weak var noParticipantsLabel: UILabel!
// MARK: view functions
override func viewDidLoad() {
super.viewDidLoad()
stylizeControls()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
fetchData()
}
// MARK: action functions
@IBAction func done(sender: AnyObject) {
let from = (DateHandler.getDBDate(fromDate) as NSString).doubleValue
let to = (DateHandler.getDBDate(toDate) as NSString).doubleValue
if to > from {
updateProspectToWebService(updateProspectURL)
} else {
showMessage("Date Error",
message: "Select valid From and To date")
}
}
@IBAction func from_date(sender: UITapGestureRecognizer) {
loadDateSelectorNIB("From")
}
@IBAction func to_date(sender: UITapGestureRecognizer) {
loadDateSelectorNIB("To")
}
// MARK: tableView functions
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allParticipants.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("participant") as! UITableViewCell
let participant = allParticipants[indexPath.row]
populateCellData(cell, withParticipant: participant)
configureSelectionLabel()
stylizeCell(cell,index: indexPath.row)
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
let participant = allParticipants[indexPath.row]
participant.toggleInclusion()
configureCheckmarkForCell(cell, included: participant.isIncluded_)
saveParticipantData(participant)
}
// Deselects the row
tableView.deselectRowAtIndexPath(indexPath, animated: true)
configureSelectionLabel()
tableView.reloadData()
}
// MARK: Internal functions
private func loadDateSelectorNIB(type: String) {
let dateVC = DateSelector(nibName: "DateSelector", bundle: nil)
dateVC.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
dateVC.delegate = self
dateVC.type = type
presentViewController(dateVC, animated: true, completion: nil)
}
private func configureSelectionLabel() {
var count = 0
for entry in allParticipants {
count = entry.isIncluded_ == "Yes" ? count + 1 : count
}
if count == 0 {
done.enabled = false
} else {
done.enabled = true
}
participants_selected_count.text = "Selected Participants: \(count)"
}
private func getNSData(prospectDict: [String: AnyObject]) -> NSData? {
var jsonError:NSError?
var jsonData:NSData? = NSJSONSerialization.dataWithJSONObject(
prospectDict, options: nil, error: &jsonError)
return jsonData
}
private func saveParticipantData(participant: Participant) {
var dict = participant.getDict()
if let id = prospectID {
dict["ProspectID"] = id
}
println(dict)
if let data = getNSData(dict) {
saveToWebService(data, operation: updateParticipantURL)
} else {
showMessage("Failure", message: "Failed to convert data")
}
}
private func configureCheckmarkForCell(cell: UITableViewCell, included: String) {
let label = cell.viewWithTag(301) as! UILabel
if included == "Yes" {
label.text = "√"
label.textColor = view.tintColor
} else {
label.text = "X"
label.textColor = UIColor(red: 204.0/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)
}
}
private func setTextInTableCell(cell: UITableViewCell, name: String) {
let label = cell.viewWithTag(302) as! UILabel
label.text = name
}
private func populateCellData(cell: UITableViewCell,
withParticipant participant: Participant) {
configureCheckmarkForCell(cell, included: participant.isIncluded_)
setTextInTableCell(cell, name: participant.userID_)
}
private func stylizeControls() {
navigationController?.navigationBar.backgroundColor = Theme.Prospects.navBarBG
tableView.separatorColor = Theme.Prospects.tableViewSeparator
tableView.backgroundColor = Theme.Prospects.cellBGOddCell
view.backgroundColor = Theme.Prospects.cellBGOddCell
// Theme.applyLabelBorder(from_date_label) Edge are rounded and background color is not edged
// Theme.applyLabelBorder(to_date_label)
// Theme.applyLabelBorder(duration_label)
to_date_label.backgroundColor = Theme.Prospects.textFieldBG
from_date_label.backgroundColor = Theme.Prospects.textFieldBG
duration_label.backgroundColor = Theme.Prospects.textFieldBG
}
private func fetchData() {
dispatch_async(dispatch_get_main_queue()) {
self.hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
self.hud?.labelText = "Loading.."
}
if let id = prospectID {
let url = viewParticipantsURL + "\(id)"
println("Participant fetch: \(url)")
let nc = NetworkCommunication()
let retValue = nc.fetchData(url,
successHandler: fetch_success, serviceErrorHandler: service_error,
errorHandler: network_error)
}
}
private func commonHandler() {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
dispatch_async(dispatch_get_main_queue()) {
self.hud?.hide(true)
}
}
func fetch_success(data: NSData) -> Void {
commonHandler()
var error: NSError?
if let dict_array = NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions.MutableContainers, error: &error) as? [AnyObject] {
allParticipants = []
for item in dict_array {
let dict = item as! [String: AnyObject]
let isIncluded = dict["Included"] as! String
let userID = dict["UserID"] as! String
let partipant = Participant(userID: userID, isIncluded: isIncluded)
allParticipants.append(partipant)
}
allParticipants = sorted(allParticipants)
dispatch_async(dispatch_get_main_queue()) {
self.currentTableView.reloadData()
}
} else {
if (allParticipants.count == 0) {
dispatch_async(dispatch_get_main_queue()) {
self.noParticipantsLabel.hidden = false
self.currentTableView.hidden = true
self.done.enabled = false
self.selection_note.hidden = true
}
}
}
}
func network_error( error: NSError) -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
self.showMessage("Network error",
message: "Code: \(error.code)\n\(error.localizedDescription)")
}
}
func service_error(response: NSHTTPURLResponse) -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
self.showMessage("Webservice Error",
message: "Error received from webservice: \(response.statusCode)")
}
}
private func showMessage(title:String, message: String) {
let alert = UIAlertController(title: title, message: message,
preferredStyle: .Alert)
let action = UIAlertAction(title: "Ok", style: .Default, handler: {
action in
// self.navigationController?.popViewControllerAnimated(true)
self.dismissViewControllerAnimated(false,completion: nil)
})
alert.addAction(action)
presentViewController(alert, animated: true, completion: nil)
}
func selectionSaveSuccess(data: NSData) -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
let hudMessage = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
hudMessage.mode = MBProgressHUDMode.Text
hudMessage.labelText = "Saved..."
hudMessage.hide(true, afterDelay: 0.7)
hudMessage.opacity = 0.25
hudMessage.yOffset = Float(self.view.frame.size.height/2 - 100)
}
}
func selectionNetworkError( error: NSError) -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
self.showMessage("Network error",
message: "Code: \(error.code)\n\(error.localizedDescription)")
}
}
func selectionServiceError(response: NSHTTPURLResponse) -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
self.showMessage("Webservice Error",
message: "Error received from webservice: \(response.statusCode)")
}
}
private func saveToWebService(data: NSData, operation: String) {
println("Operation: \(operation)")
let nc = NetworkCommunication()
nc.postData(operation, data: data,
successHandler: selectionSaveSuccess,
serviceErrorHandler: selectionServiceError,
errorHandler: selectionNetworkError)
}
private func getFormData() -> [String: AnyObject] {
var prospect = [String: AnyObject]()
prospect["ConfDateStart"] = DateHandler.getDBDate(fromDate)
prospect["ConfDateEnd"] = DateHandler.getDBDate(toDate)
if let id = prospectID {
prospect["ProspectID"] = id
}
return prospect
}
func saveProspectSuccess(data: NSData) -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
self.showMessage("Data saved",
message: "Data saved succesfully.")
}
}
func networkError( error: NSError) -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
self.showMessage("Network error",
message: "Code: \(error.code)\n\(error.localizedDescription)")
}
}
func serviceError(response: NSHTTPURLResponse) -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
self.showMessage("Webservice Error",
message: "Error received from webservice: \(response.statusCode)")
}
}
private func saveProspectToWebService(dict: [String: AnyObject], method: String) {
println("Prospect save: \(dict)")
if let data = getNSData(dict) {
let nc = NetworkCommunication()
nc.postData(method, data: data,
successHandler: saveProspectSuccess,
serviceErrorHandler: serviceError,
errorHandler: networkError)
} else {
showMessage("Failure", message: "Failed to convert data")
}
}
private func updateProspectToWebService(operation: String) {
var prospect = getFormData()
saveProspectToWebService(prospect, method:operation)
}
private func stylizeCell(cell: UITableViewCell, index: Int) {
if index % 2 != 0 {
cell.backgroundColor = Theme.Prospects.cellBGOddCell
tableView.backgroundColor = Theme.Prospects.cellBGEvenCell
} else {
cell.backgroundColor = Theme.Prospects.cellBGEvenCell
tableView.backgroundColor = Theme.Prospects.cellBGOddCell
}
cell.textLabel?.backgroundColor = UIColor.clearColor()
cell.detailTextLabel?.backgroundColor = UIColor.clearColor()
}
// MARK: Delegate Functions
func dateSelectorDidFinish(controller: DateSelector, type: String?) {
if let type = type {
if type == "From" {
fromDate = controller.datePicker.date
from_date_label.text = DateHandler.getPrintDate(fromDate)
} else if type == "To" {
toDate = controller.datePicker.date
to_date_label.text = DateHandler.getPrintDate(toDate)
duration_label.text = Int(((toDate.timeIntervalSince1970 - fromDate.timeIntervalSince1970) / 60)).description + " minutes"
}
}
}
func convertClientFinish() {
dispatch_async(dispatch_get_main_queue()) {
let hudMessage = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
hudMessage.mode = MBProgressHUDMode.Text
hudMessage.labelText = "Save successful"
hudMessage.hide(true, afterDelay: 1.5)
hudMessage.opacity = 0.4
hudMessage.yOffset = Float(self.view.frame.size.height/2 - 100)
}
dismissViewControllerAnimated(true, completion: nil)
}
}