-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertClient.swift
148 lines (128 loc) · 4.3 KB
/
ConvertClient.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
//
// ConvertClient.swift
// PreSales-Huddle
//
// Created by Himanshu Phirke on 27/07/15.
// Copyright (c) 2015 synerzip. All rights reserved.
//
import UIKit
class ConvertClient: UIViewController, DateSelectorDelegate {
var prospectID: Int?
var prospectName: String?
private var startDate = NSDate()
let updateProspectURL = "prospect/update/"
// MARK: Outlets
@IBOutlet weak var start_date: UITextField!
@IBOutlet weak var done: UIBarButtonItem!
@IBOutlet weak var bu_head: UITextField!
@IBOutlet weak var team_size: UITextField!
@IBOutlet weak var prospect_name: UITextField!
// MARK: view functions
override func viewDidLoad() {
super.viewDidLoad()
// stylizeControls()
if let name = prospectName {
prospect_name.text = name
}
team_size.becomeFirstResponder()
}
// MARK: action functions
@IBAction func done(sender: UIBarButtonItem) {
updateProspectToWebService(updateProspectURL)
}
@IBAction func date_click(sender: UITextField) {
let dateVC = DateSelector(nibName: "DateSelector", bundle: nil)
dateVC.modalPresentationStyle = UIModalPresentationStyle.OverFullScreen
dateVC.delegate = self
dateVC.type = "StartDate"
dateVC.pickerType = UIDatePickerMode.Date
dateVC.senderFrame = sender.frame
presentViewController(dateVC, animated: false, completion: nil)
}
// MARK : private functions
private func loadDateSelectorNIB(type: String) {
}
private func commonHandler() {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func saveProspectSuccess() -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
self.showMessage("Success",
message: "Prospect converted to Client.")
}
}
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) {
print("Prospect save: \(dict)")
saveProspectSuccess()
}
private func getFormData() -> [String: AnyObject] {
var prospect = [String: AnyObject]()
prospect["StartDate"] = DateHandler.getDBDate(startDate)
prospect["BUHead"] = bu_head.text
prospect["TeamSize"] = Int(team_size.text!)
prospect["Name"] = prospect_name.text
if let id = prospectID {
prospect["ProspectID"] = id
}
return prospect
}
private func getNSData(prospectDict: [String: AnyObject]) -> NSData? {
var jsonData:NSData?
do {
jsonData = try NSJSONSerialization.dataWithJSONObject(
prospectDict, options: [])
} catch {
jsonData = nil
}
return jsonData
}
private func updateProspectToWebService(operation: String) {
let prospect = getFormData()
saveProspectToWebService(prospect, method:operation)
}
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)
})
alert.addAction(action)
presentViewController(alert, animated: true, completion: nil)
}
private func stylizeControls() {
view.backgroundColor = Theme.Prospects.formBG
// Text Field BG Color
bu_head.backgroundColor = Theme.Prospects.textFieldBG
team_size.backgroundColor = Theme.Prospects.textFieldBG
prospect_name.backgroundColor = Theme.Prospects.textFieldBG
start_date.backgroundColor = Theme.Prospects.textFieldBG
// Theme.applyLabelBorder(start_date_label)
}
// MARK: Delegate Functions
func dateSelectorDidFinish(dateFromVC: NSDate, type: String?) {
if let type = type {
if type == "StartDate" {
startDate = dateFromVC
start_date.text = DateHandler.getPrintDate(startDate)
done.enabled = true
start_date.resignFirstResponder()
}
}
}
}