-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
SPNativeLoginCodeViewController.swift
162 lines (132 loc) · 6.01 KB
/
SPNativeLoginCodeViewController.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
// 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 SPNativeLoginCodeViewController: SPNativeTableViewController {
weak var delegate: SPLoginCodeControllerDelegate?
var content: SPNativeLoginNavigationController.LoginCodeContent!
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self.navigationController as! SPNativeLoginNavigationController
self.content = self.delegate?.loginCodeContent
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 1
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.codeTitle] {
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.codeTitle
cell.textField.placeholder = self.content.codePlaceholder
cell.textField.keyboardType = self.content.codeKeyboardType
cell.textField.delegate = self
cell.textField.returnKeyType = .done
cell.textField.autocapitalizationType = .none
cell.textField.autocorrectionType = .no
cell.textField.isSecureTextEntry = true
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 сodeCell: SPFormTextFiledTableViewCell? = nil
var buttonCell: SPFormButtonTableViewCell? = nil
if let cell = self.tableView.cellForRow(at: IndexPath.init(row: 0, section: 0)) as? SPFormTextFiledTableViewCell {
сodeCell = cell
}
if сodeCell?.textField.isEmptyText ?? false {
сodeCell?.highlight()
return
}
if let cell = self.tableView.cellForRow(at: IndexPath.init(row: 0, section: 1)) as? SPFormButtonTableViewCell {
buttonCell = cell
}
buttonCell?.button.startLoading()
сodeCell?.textField.isEnabled = false
self.delegate?.login(with: сodeCell?.textField.text ?? "") { (oAuthState) in
buttonCell?.button.stopLoading()
if oAuthState != SPOauthState.succsess {
UIAlertController.show(
title: self.content.errorOauthTitle,
message: self.content.errorOauthSubtitle,
buttonTitle: self.content.errorOauthButtonTitle,
on: self
)
сodeCell?.textField.isEnabled = true
сodeCell?.textField.becomeFirstResponder()
}
}
}
}
extension SPNativeLoginCodeViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
self.enterAction()
return true
}
}
protocol SPLoginCodeControllerDelegate: class {
var loginCodeContent: SPNativeLoginNavigationController.LoginCodeContent {get}
func login(with code: String, complection: @escaping (SPOauthState)->())
}