-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathAuthorizationSequenceCodeEntryController.swift
287 lines (229 loc) · 11.7 KB
/
AuthorizationSequenceCodeEntryController.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
import Foundation
import UIKit
import Display
import AsyncDisplayKit
import SwiftSignalKit
import TelegramCore
import TelegramPresentationData
import ProgressNavigationButtonNode
public final class AuthorizationSequenceCodeEntryController: ViewController {
private var controllerNode: AuthorizationSequenceCodeEntryControllerNode {
return self.displayNode as! AuthorizationSequenceCodeEntryControllerNode
}
private var validLayout: ContainerViewLayout?
private let strings: PresentationStrings
private let theme: PresentationTheme
public var loginWithCode: ((String) -> Void)?
public var signInWithApple: (() -> Void)?
public var openFragment: ((String) -> Void)?
var reset: (() -> Void)?
public var requestNextOption: (() -> Void)?
public var requestPreviousOption: (() -> Void)?
var resetEmail: (() -> Void)?
var retryResetEmail: (() -> Void)?
var data: (String, String?, SentAuthorizationCodeType, AuthorizationCodeNextType?, Int32?, SentAuthorizationCodeType?, Bool)?
var termsOfService: (UnauthorizedAccountTermsOfService, Bool)?
private let hapticFeedback = HapticFeedback()
private var appleSignInAllowed = false
public var inProgress: Bool = false {
didSet {
self.updateNavigationItems()
self.controllerNode.inProgress = self.inProgress
}
}
var isPrevious: Bool {
return self.data?.6 ?? false
}
public init(presentationData: PresentationData, back: @escaping () -> Void) {
self.strings = presentationData.strings
self.theme = presentationData.theme
super.init(navigationBarPresentationData: NavigationBarPresentationData(theme: AuthorizationSequenceController.navigationBarTheme(theme), strings: NavigationBarStrings(presentationStrings: strings)))
self.supportedOrientations = ViewControllerSupportedOrientations(regularSize: .all, compactSize: .portrait)
self.hasActiveInput = true
self.statusBar.statusBarStyle = theme.intro.statusBarStyle.style
self.attemptNavigation = { _ in
return false
}
self.navigationBar?.backPressed = { [weak self] in
let text: String
let proceed: String
let stop: String
if let (_, _, type, _, _, _, _) = self?.data, case .email = type {
text = presentationData.strings.Login_CancelEmailVerification
proceed = presentationData.strings.Login_CancelEmailVerificationContinue
stop = presentationData.strings.Login_CancelEmailVerificationStop
} else {
text = presentationData.strings.Login_CancelPhoneVerification
proceed = presentationData.strings.Login_CancelPhoneVerificationContinue
stop = presentationData.strings.Login_CancelPhoneVerificationStop
}
self?.present(standardTextAlertController(theme: AlertControllerTheme(presentationData: presentationData), title: nil, text: text, actions: [TextAlertAction(type: .genericAction, title: proceed, action: {
}), TextAlertAction(type: .defaultAction, title: stop, action: {
back()
})]), in: .window(.root))
}
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func loadDisplayNode() {
self.displayNode = AuthorizationSequenceCodeEntryControllerNode(strings: self.strings, theme: self.theme)
self.displayNodeDidLoad()
self.controllerNode.view.disableAutomaticKeyboardHandling = [.forward, .backward]
self.controllerNode.loginWithCode = { [weak self] code in
self?.continueWithCode(code)
}
self.controllerNode.signInWithApple = { [weak self] in
self?.signInWithApple?()
}
self.controllerNode.openFragment = { [weak self] url in
self?.openFragment?(url)
}
self.controllerNode.requestNextOption = { [weak self] in
self?.requestNextOption?()
}
self.controllerNode.requestAnotherOption = { [weak self] in
self?.requestNextOption?()
}
self.controllerNode.requestPreviousOption = { [weak self] in
self?.requestPreviousOption?()
}
self.controllerNode.updateNextEnabled = { [weak self] value in
self?.navigationItem.rightBarButtonItem?.isEnabled = value
}
self.controllerNode.reset = { [weak self] in
self?.resetEmail?()
}
self.controllerNode.retryReset = { [weak self] in
self?.retryResetEmail?()
}
self.controllerNode.present = { [weak self] c, a in
self?.present(c, in: .window(.root), with: a)
}
if let (number, email, codeType, nextType, timeout, previousCodeType, isPrevious) = self.data {
var appleSignInAllowed = false
if case let .email(_, _, _, _, appleSignInAllowedValue, _) = codeType {
appleSignInAllowed = appleSignInAllowedValue
}
self.controllerNode.updateData(number: number, email: email, codeType: codeType, nextType: nextType, timeout: timeout, appleSignInAllowed: appleSignInAllowed, previousCodeType: previousCodeType, isPrevious: isPrevious)
}
}
override public func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let navigationController = self.navigationController as? NavigationController, let layout = self.validLayout {
addTemporaryKeyboardSnapshotView(navigationController: navigationController, layout: layout)
}
self.controllerNode.activateInput()
}
public func resetCode() {
self.controllerNode.resetCode()
}
public func animateSuccess() {
self.controllerNode.animateSuccess()
}
public func selectIncorrectPart() {
self.controllerNode.selectIncorrectPart()
}
public func animateError(text: String) {
self.hapticFeedback.error()
self.controllerNode.animateError(text: text)
}
func updateAppIsActive(_ isActive: Bool) {
self.controllerNode.updatePasteVisibility()
}
func updateNavigationItems() {
guard let layout = self.validLayout, layout.size.width < 360.0 else {
return
}
if self.inProgress {
let item = UIBarButtonItem(customDisplayNode: ProgressNavigationButtonNode(color: self.theme.rootController.navigationBar.accentTextColor))
self.navigationItem.rightBarButtonItem = item
} else {
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: self.strings.Common_Next, style: .done, target: self, action: #selector(self.nextPressed))
}
}
public func updateData(number: String, email: String?, codeType: SentAuthorizationCodeType, nextType: AuthorizationCodeNextType?, timeout: Int32?, termsOfService: (UnauthorizedAccountTermsOfService, Bool)?, previousCodeType: SentAuthorizationCodeType?, isPrevious: Bool) {
self.termsOfService = termsOfService
if self.data?.0 != number || self.data?.1 != email || self.data?.2 != codeType || self.data?.3 != nextType || self.data?.4 != timeout || self.data?.5 != previousCodeType || self.data?.6 != isPrevious {
self.data = (number, email, codeType, nextType, timeout, previousCodeType, isPrevious)
var appleSignInAllowed = false
if case let .email(_, _, _, _, appleSignInAllowedValue, _) = codeType {
appleSignInAllowed = appleSignInAllowedValue
}
if self.isNodeLoaded {
self.controllerNode.updateData(number: number, email: email, codeType: codeType, nextType: nextType, timeout: timeout, appleSignInAllowed: appleSignInAllowed, previousCodeType: previousCodeType, isPrevious: isPrevious)
self.requestLayout(transition: .immediate)
}
}
}
override public func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
super.containerLayoutUpdated(layout, transition: transition)
let hadLayout = self.validLayout != nil
self.validLayout = layout
if !hadLayout {
self.updateNavigationItems()
if let navigationController = self.navigationController as? NavigationController {
addTemporaryKeyboardSnapshotView(navigationController: navigationController, layout: layout, local: true)
}
}
self.controllerNode.containerLayoutUpdated(layout, navigationBarHeight: self.navigationLayout(layout: layout).navigationFrame.maxY, transition: transition)
}
@objc private func nextPressed() {
guard let (_, _, type, _, _, _, _) = self.data else {
return
}
var minimalCodeLength = 1
switch type {
case let .otherSession(length):
minimalCodeLength = Int(length)
case let .sms(length):
minimalCodeLength = Int(length)
case let .call(length):
minimalCodeLength = Int(length)
case let .missedCall(_, length):
minimalCodeLength = Int(length)
case let .email(_, length, _, _, _, _):
minimalCodeLength = Int(length)
case let .fragment(_, length):
minimalCodeLength = Int(length)
case let .firebase(_, length):
minimalCodeLength = Int(length)
case .flashCall, .emailSetupRequired, .word, .phrase:
break
}
if self.controllerNode.currentCode.count < minimalCodeLength {
self.hapticFeedback.error()
self.controllerNode.animateError()
} else {
self.continueWithCode(self.controllerNode.currentCode)
}
}
private func continueWithCode(_ code: String) {
self.loginWithCode?(code)
}
public func applyConfirmationCode(_ code: Int) {
self.controllerNode.updateCode("\(code)")
}
}
func addTemporaryKeyboardSnapshotView(navigationController: NavigationController, layout: ContainerViewLayout, local: Bool = false) {
if case .compact = layout.metrics.widthClass, let statusBarHost = navigationController.statusBarHost {
if let keyboardView = statusBarHost.keyboardView {
keyboardView.layer.removeAllAnimations()
if let snapshotView = keyboardView.snapshotView(afterScreenUpdates: false) {
UIView.performWithoutAnimation {
snapshotView.frame = CGRect(origin: CGPoint(x: 0.0, y: layout.size.height - snapshotView.frame.size.height), size: snapshotView.frame.size)
if local {
navigationController.view.addSubview(snapshotView)
} else if let keyboardWindow = statusBarHost.keyboardWindow {
keyboardWindow.addSubview(snapshotView)
}
Queue.mainQueue().after(local ? 0.8 : 0.7, {
snapshotView.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2, removeOnCompletion: false, completion: { [weak snapshotView] _ in
snapshotView?.removeFromSuperview()
})
})
}
}
}
}
}