-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
SPNativeLoginViewController.swift
202 lines (171 loc) · 7.87 KB
/
SPNativeLoginViewController.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
// The MIT License (MIT)
// Copyright © 2017 Ivan Vorobei (hello@ivanvorobei.by)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
class SPNativeLoginViewController: SPNativeTableViewController {
weak var delegate: SPLoginControllerDelegate?
var content: SPNativeLoginNavigationController.LoginContent!
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self.navigationController as! SPNativeLoginNavigationController
self.content = self.delegate?.loginContent
self.setPrefersLargeNavigationTitle(self.content.navigationTitle)
self.dismissKeyboardWhenTappedAround()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let cell = self.tableView.cellForRow(at: IndexPath.init(row: 0, section: 0)) as? SPFormTextFiledTableViewCell {
cell.textField.becomeFirstResponder()
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return 2
case 1:
return 1
default:
return 0
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var labelWidth: CGFloat = 0
for text in [self.content.loginTitle, self.content.passwordTitle] {
let font = UIFont.system(type: .Regular, size: 17)
let fontAttributes = [NSAttributedString.Key.font: font]
let calculatedSize = NSString.init(string: text).size(withAttributes: fontAttributes)
labelWidth.setIfFewer(when: calculatedSize.width + 1)
}
switch indexPath {
case IndexPath.init(row: 0, section: 0):
let cell = self.dequeueTextFiledTableViewCell(indexPath: indexPath)
cell.label.text = self.content.loginTitle
cell.textField.placeholder = self.content.loginPlaceholder
cell.textField.keyboardType = self.content.loginKeyboardType
cell.textField.delegate = self
cell.textField.returnKeyType = .next
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
cell.fixWidthLabel = labelWidth
cell.textAligmentToSide = false
return cell
case IndexPath.init(row: 1, section: 0):
let cell = self.dequeueTextFiledTableViewCell(indexPath: indexPath)
cell.label.text = self.content.passwordTitle
cell.textField.placeholder = self.content.passwordPlaceholder
cell.textField.isSecureTextEntry = true
cell.textField.delegate = self
cell.textField.returnKeyType = .done
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
cell.separatorInset.left = 0
cell.fixWidthLabel = labelWidth
cell.separatorInsetStyle = .all
cell.textAligmentToSide = false
return cell
case IndexPath.init(row: 0, section: 1):
let cell = self.dequeueButtonTableViewCell(indexPath: indexPath)
cell.button.setTitle(self.content.buttonTitle, for: .normal)
cell.separatorInset.left = 0
cell.button.addTarget(self, action: #selector(self.enterAction), for: .touchUpInside)
return cell
default:
return UITableViewCell()
}
}
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
switch section {
case 0:
return self.content.commentTitle
default:
return nil
}
}
@objc func enterAction() {
var loginCell: SPFormTextFiledTableViewCell? = nil
var passwordCell: SPFormTextFiledTableViewCell? = nil
var buttonCell: SPFormButtonTableViewCell? = nil
if let cell = self.tableView.cellForRow(at: IndexPath.init(row: 0, section: 0)) as? SPFormTextFiledTableViewCell {
loginCell = cell
}
if let cell = self.tableView.cellForRow(at: IndexPath.init(row: 1, section: 0)) as? SPFormTextFiledTableViewCell {
passwordCell = cell
}
if let cell = self.tableView.cellForRow(at: IndexPath.init(row: 0, section: 1)) as? SPFormButtonTableViewCell {
buttonCell = cell
}
if loginCell?.textField.isEmptyText ?? false {
loginCell?.highlight()
return
}
if passwordCell?.textField.isEmptyText ?? false {
passwordCell?.highlight()
return
}
buttonCell?.button.startLoading()
loginCell?.textField.isEnabled = false
passwordCell?.textField.isEnabled = false
self.delegate?.login(with: loginCell?.textField.text ?? "", password: passwordCell?.textField.text ?? "") { (oAuthState) in
buttonCell?.button.stopLoading()
switch oAuthState {
case .succsess:
break
case .needTwoFactor:
self.delegate?.needRequestCode()
break
default:
loginCell?.textField.isEnabled = true
passwordCell?.textField.isEnabled = true
passwordCell?.textField.becomeFirstResponder()
UIAlertController.show(
title: self.content.errorOauthTitle,
message: self.content.errorOauthSubtitle,
buttonTitle: self.content.errorOauthButtonTitle,
on: self
)
break
}
}
}
}
extension SPNativeLoginViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.superview == self.tableView.cellForRow(at: IndexPath.init(row: 0, section: 0))?.contentView {
if let passwordCell = self.tableView.cellForRow(at: IndexPath.init(row: 1, section: 0)) as? SPFormTextFiledTableViewCell {
passwordCell.textField.becomeFirstResponder()
return false
}
}
if textField.superview == self.tableView.cellForRow(at: IndexPath.init(row: 1, section: 0))?.contentView {
textField.resignFirstResponder()
self.enterAction()
return true
}
return true
}
}
protocol SPLoginControllerDelegate: class {
var loginContent: SPNativeLoginNavigationController.LoginContent {get}
func login(with login: String, password: String, complection: @escaping (SPOauthState)->())
func needRequestCode() -> ()
}