-
Notifications
You must be signed in to change notification settings - Fork 0
/
loginVC.swift
216 lines (163 loc) · 7.62 KB
/
loginVC.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
//
// loginVC.swift
//
//
// Created by amirhosein on 5/10/1396 AP.
//
//
import Foundation
import UIKit
import TransitionButton
class loginVC : UIViewController,UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
print("login viewDidLoad")
usernameTextField.delegate=self
passwordTextField.delegate=self
self.myButton = TransitionButton(frame: CGRect(x: 0 , y: 0, width: self.buttonView.frame.width, height: self.buttonView.frame.height))
self.buttonView.addSubview(myButton)
myButton.backgroundColor = UIColor.yellow
myButton.setTitle("Connect", for: .normal)
myButton.cornerRadius = 20
myButton.spinnerColor = .yellow
myButton.setTitleColor(UIColor.gray, for: .normal)
myButton.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
// let connectOnImg = UIImage(named : "connect2")
// connectButton.setImage(connectOnImg, for: .focused)
//
// let connectOffImg = UIImage(named : "connect1")
// connectButton.setImage(connectOnImg, for: .disabled)
}
override func viewWillAppear(_ animated: Bool) {
username = ""
password = ""
code = ""
errorDescription = ""
sessionKey = ""
}
@IBOutlet weak var imgLogin: UIImageView!
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var buttonView: UIView!
var myButton = TransitionButton()
var username = ""
var password = ""
var code = ""
var errorDescription = ""
var sessionKey = ""
var network = NetworkManager()
var serverDictionary = [String : Any]()
var dataDictionary = [String: Any]()
let loginRequestURL = "http://192.168.1.40/WebServices/Profile.svc/Login"
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.resignFirstResponder() ;
if( textField==usernameTextField) {
self.username = textField.text!
}else {
self.password = textField.text!
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if(textField == usernameTextField){
self.username = textField.text!
}else{
self.password = textField.text!
}
return true ;
}
override func shouldPerformSegue(withIdentifier identifier: String, sender: (Any)? ) -> Bool {
print("should performe segue ")
if(code==""){
print("code is not provided ! ")
return false
}
if(identifier=="loginSegue") {
if(self.code == "G00000"){
UserDefaults.standard.set(self.sessionKey , forKey : "sessionKey")
print("2")
OperationQueue.main.addOperation {
DispatchQueue.main.async(execute: { () -> Void in
// 4: Stop the animation, here you have three options for the `animationStyle` property:
// .expand: useful when the task has been compeletd successfully and you want to expand the button and transit to another view controller in the completion callback
// .shake: when you want to reflect to the user that the task did not complete successfly
// .normal
self.myButton.stopAnimation(animationStyle: .expand, completion: {
// let secondVC = SecondViewController()
// self.present(secondVC, animated: true, completion: nil)
self.performSegue(withIdentifier: "loginSegue", sender: self)
})
})
}
return true
}else {
print("3")
OperationQueue.main.addOperation {
DispatchQueue.main.async(execute: { () -> Void in
// 4: Stop the animation, here you have three options for the `animationStyle` property:
// .expand: useful when the task has been compeletd successfully and you want to expand the button and transit to another view controller in the completion callback
// .shake: when you want to reflect to the user that the task did not complete successfly
// .normal
self.myButton.stopAnimation(animationStyle: .shake, completion: {
let alertController = UIAlertController(title : "Wrong ! ",message : self.errorDescription ,preferredStyle : .alert)
let defaultAction = UIAlertAction(title:"ok" , style : .default , handler : nil) ;
alertController.addAction(defaultAction)
self.present(alertController,animated: true, completion: nil)
})
})
}
return false
}
}else{
print("1")
return false ;
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("4")
if segue.identifier == "loginSegue" {
print("5")
let vc = segue.destination as! lampTabBarController
vc.username = self.username ;
}
}
func manageResponse (_ json : [String : Any]) {
print("manage response")
guard let status = json["Status"] as? [String : Any] else {
return
}
guard let parameters = json["Parameters"] as? [String : Any] else {
self.code = status["Code"] as! String
self.errorDescription = status["Description"] as! String
if(!self.shouldPerformSegue(withIdentifier: "loginSegue",sender : self)){
print("perform segue failed")
}
return
}
self.sessionKey = parameters["SessionKey"] as! String
self.code = status["Code"] as! String
self.errorDescription = status["Description"] as! String
if(!self.shouldPerformSegue(withIdentifier: "loginSegue",sender : self)){
print("perform segue failed")
}
}
//MARK: Actions :
@IBAction func buttonAction(_ button: TransitionButton) {
button.startAnimation() // 2: Then start the animation when the user tap the button
let qualityOfServiceClass = DispatchQoS.QoSClass.background
let backgroundQueue = DispatchQueue.global(qos: qualityOfServiceClass)
backgroundQueue.async(execute: {
// sleep(2) // 3: Do your networking task or background work here.
self.dataDictionary["Username"] = self.usernameTextField.text
self.dataDictionary["Password"] = self.passwordTextField.text
self.serverDictionary["Parameters"] = self.dataDictionary ;
print("login server dictionary : ",self.serverDictionary)
if( !self.network.networkCall( self , self.loginRequestURL , self.serverDictionary as [String : AnyObject]) ){
print("login network call error ")
}
})
}
}