-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathAuthorizationSequenceEmailEntryControllerNode.swift
296 lines (232 loc) · 14 KB
/
AuthorizationSequenceEmailEntryControllerNode.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
288
289
290
291
292
293
294
295
296
import Foundation
import UIKit
import AsyncDisplayKit
import Display
import TelegramPresentationData
import AuthorizationUtils
import AuthenticationServices
import AnimatedStickerNode
import TelegramAnimatedStickerNode
import SolidRoundedButtonNode
final class AuthorizationDividerNode: ASDisplayNode {
private let titleNode: ImmediateTextNode
private let leftLineNode: ASDisplayNode
private let rightLineNode: ASDisplayNode
init(theme: PresentationTheme, strings: PresentationStrings) {
self.titleNode = ImmediateTextNode()
self.titleNode.maximumNumberOfLines = 1
self.titleNode.attributedText = NSAttributedString(string: strings.Login_Or, font: Font.regular(17.0), textColor: theme.list.itemSecondaryTextColor)
self.leftLineNode = ASDisplayNode()
self.leftLineNode.backgroundColor = theme.list.itemSecondaryTextColor
self.rightLineNode = ASDisplayNode()
self.rightLineNode.backgroundColor = theme.list.itemSecondaryTextColor
super.init()
self.addSubnode(self.titleNode)
self.addSubnode(self.leftLineNode)
self.addSubnode(self.rightLineNode)
}
func updateLayout(width: CGFloat) -> CGSize {
let lineSize = CGSize(width: 33.0, height: UIScreenPixel)
let spacing: CGFloat = 7.0
let titleSize = self.titleNode.updateLayout(CGSize(width: width - (lineSize.width + spacing) * 2.0, height: .greatestFiniteMagnitude))
let height: CGFloat = 40.0
let titleFrame = CGRect(origin: CGPoint(x: floorToScreenPixels((width - lineSize.width) / 2.0), y: floor((height - titleSize.height) / 2.0)), size: titleSize)
self.titleNode.frame = titleFrame
self.leftLineNode.frame = CGRect(origin: CGPoint(x: titleFrame.minX - spacing - lineSize.width, y: floorToScreenPixels(height / 2.0)), size: lineSize)
self.rightLineNode.frame = CGRect(origin: CGPoint(x: titleFrame.maxX + spacing, y: floorToScreenPixels(height / 2.0)), size: lineSize)
return CGSize(width: width, height: height)
}
}
final class AuthorizationSequenceEmailEntryControllerNode: ASDisplayNode, UITextFieldDelegate {
private let strings: PresentationStrings
private let theme: PresentationTheme
private let mode: AuthorizationSequenceEmailEntryController.Mode
private let animationNode: AnimatedStickerNode
private let titleNode: ASTextNode
private let noticeNode: ASTextNode
private let dividerNode: AuthorizationDividerNode
private var signInWithAppleButton: UIControl?
private let proceedNode: SolidRoundedButtonNode
private let codeField: TextFieldNode
private let codeSeparatorNode: ASDisplayNode
private var layoutArguments: (ContainerViewLayout, CGFloat)?
var currentEmail: String {
return self.codeField.textField.text ?? ""
}
var proceedWithEmail: ((String) -> Void)?
var signInWithApple: (() -> Void)?
private var appleSignInAllowed = false
var inProgress: Bool = false {
didSet {
self.codeField.alpha = self.inProgress ? 0.6 : 1.0
if self.inProgress != oldValue {
if self.inProgress {
self.proceedNode.transitionToProgress()
} else {
self.proceedNode.transitionFromProgress()
}
}
}
}
private let appearanceTimestamp = CACurrentMediaTime()
init(strings: PresentationStrings, theme: PresentationTheme, mode: AuthorizationSequenceEmailEntryController.Mode) {
self.strings = strings
self.theme = theme
self.mode = mode
self.animationNode = DefaultAnimatedStickerNodeImpl()
self.animationNode.setup(source: AnimatedStickerNodeLocalFileSource(name: "IntroMail"), width: 256, height: 256, playbackMode: .once, mode: .direct(cachePathPrefix: nil))
self.titleNode = ASTextNode()
self.titleNode.isUserInteractionEnabled = false
self.titleNode.displaysAsynchronously = false
self.noticeNode = ASTextNode()
self.noticeNode.isUserInteractionEnabled = false
self.noticeNode.displaysAsynchronously = false
self.noticeNode.lineSpacing = 0.1
self.noticeNode.attributedText = NSAttributedString(string: self.strings.Login_AddEmailText, font: Font.regular(16.0), textColor: self.theme.list.itemPrimaryTextColor, paragraphAlignment: .center)
if #available(iOS 13.0, *) {
self.signInWithAppleButton = ASAuthorizationAppleIDButton(authorizationButtonType: .signIn, authorizationButtonStyle: theme.overallDarkAppearance ? .white : .black)
(self.signInWithAppleButton as? ASAuthorizationAppleIDButton)?.cornerRadius = 11
}
self.proceedNode = SolidRoundedButtonNode(title: self.strings.Login_Continue, theme: SolidRoundedButtonTheme(theme: self.theme), height: 50.0, cornerRadius: 11.0, gloss: false)
self.proceedNode.progressType = .embedded
self.codeSeparatorNode = ASDisplayNode()
self.codeSeparatorNode.isLayerBacked = true
self.codeSeparatorNode.backgroundColor = self.theme.list.itemPlainSeparatorColor
self.codeField = TextFieldNode()
self.codeField.textField.font = Font.regular(20.0)
self.codeField.textField.textColor = self.theme.list.itemPrimaryTextColor
self.codeField.textField.textAlignment = .natural
self.codeField.textField.autocorrectionType = .no
self.codeField.textField.autocapitalizationType = .none
self.codeField.textField.keyboardType = .emailAddress
if #available(iOSApplicationExtension 10.0, iOS 10.0, *) {
self.codeField.textField.textContentType = UITextContentType(rawValue: "")
}
self.codeField.textField.returnKeyType = .done
self.codeField.textField.keyboardAppearance = self.theme.rootController.keyboardColor.keyboardAppearance
self.codeField.textField.disableAutomaticKeyboardHandling = [.forward, .backward]
self.codeField.textField.tintColor = self.theme.list.itemAccentColor
self.codeField.textField.placeholder = self.strings.Login_AddEmailPlaceholder
self.dividerNode = AuthorizationDividerNode(theme: self.theme, strings: self.strings)
super.init()
self.setViewBlock({
return UITracingLayerView()
})
self.backgroundColor = self.theme.list.plainBackgroundColor
self.codeField.textField.delegate = self
self.addSubnode(self.codeSeparatorNode)
self.addSubnode(self.codeField)
self.addSubnode(self.titleNode)
self.addSubnode(self.proceedNode)
self.addSubnode(self.noticeNode)
self.addSubnode(self.animationNode)
self.addSubnode(self.dividerNode)
self.codeField.textField.addTarget(self, action: #selector(self.textDidChange), for: .editingChanged)
self.proceedNode.pressed = { [weak self] in
self?.proceedPressed()
}
self.signInWithAppleButton?.addTarget(self, action: #selector(self.signInWithApplePressed), for: .touchUpInside)
}
override func didLoad() {
super.didLoad()
if let signInWithAppleButton = self.signInWithAppleButton {
self.view.addSubview(signInWithAppleButton)
}
}
@objc private func textDidChange() {
self.updateButtonsVisibility(transition: .animated(duration: 0.2, curve: .easeInOut))
}
private func updateButtonsVisibility(transition: ContainedViewLayoutTransition) {
if self.currentEmail.isEmpty && self.appleSignInAllowed {
transition.updateAlpha(node: self.proceedNode, alpha: 0.0)
transition.updateAlpha(node: self.dividerNode, alpha: 1.0)
if let signInWithAppleButton = self.signInWithAppleButton {
transition.updateAlpha(layer: signInWithAppleButton.layer, alpha: 1.0)
}
} else {
transition.updateAlpha(node: self.proceedNode, alpha: 1.0)
transition.updateAlpha(node: self.dividerNode, alpha: 0.0)
if let signInWithAppleButton = self.signInWithAppleButton {
transition.updateAlpha(layer: signInWithAppleButton.layer, alpha: 0.0)
}
}
}
func updateData(appleSignInAllowed: Bool) {
self.appleSignInAllowed = appleSignInAllowed
if let (layout, navigationHeight) = self.layoutArguments {
self.updateButtonsVisibility(transition: .immediate)
self.containerLayoutUpdated(layout, navigationBarHeight: navigationHeight, transition: .immediate)
}
}
func containerLayoutUpdated(_ layout: ContainerViewLayout, navigationBarHeight: CGFloat, transition: ContainedViewLayoutTransition) {
let previousInputHeight = self.layoutArguments?.0.inputHeight ?? 0.0
let newInputHeight = layout.inputHeight ?? 0.0
self.layoutArguments = (layout, navigationBarHeight)
var layout = layout
if CACurrentMediaTime() - self.appearanceTimestamp < 2.0, newInputHeight < previousInputHeight {
layout = layout.withUpdatedInputHeight(previousInputHeight)
}
var insets = layout.insets(options: [])
insets.top = layout.statusBarHeight ?? 20.0
if let inputHeight = layout.inputHeight {
insets.bottom = max(inputHeight, insets.bottom)
}
let titleInset: CGFloat = layout.size.width > 320.0 ? 18.0 : 0.0
self.titleNode.attributedText = NSAttributedString(string: self.mode == .setup ? self.strings.Login_AddEmailTitle : self.strings.Login_EnterNewEmailTitle, font: Font.bold(28.0), textColor: self.theme.list.itemPrimaryTextColor)
let inset: CGFloat = 24.0
let maximumWidth: CGFloat = min(430.0, layout.size.width)
let animationSize = CGSize(width: 100.0, height: 100.0)
let titleSize = self.titleNode.measure(CGSize(width: maximumWidth, height: CGFloat.greatestFiniteMagnitude))
let noticeSize = self.noticeNode.measure(CGSize(width: maximumWidth - 80.0, height: CGFloat.greatestFiniteMagnitude))
let proceedHeight = self.proceedNode.updateLayout(width: maximumWidth - 48.0, transition: transition)
let proceedSize = CGSize(width: maximumWidth - 48.0, height: proceedHeight)
var items: [AuthorizationLayoutItem] = []
items.append(AuthorizationLayoutItem(node: self.titleNode, size: titleSize, spacingBefore: AuthorizationLayoutItemSpacing(weight: titleInset, maxValue: titleInset), spacingAfter: AuthorizationLayoutItemSpacing(weight: 0.0, maxValue: 0.0)))
items.append(AuthorizationLayoutItem(node: self.noticeNode, size: noticeSize, spacingBefore: AuthorizationLayoutItemSpacing(weight: 18.0, maxValue: 18.0), spacingAfter: AuthorizationLayoutItemSpacing(weight: 0.0, maxValue: 0.0)))
items.append(AuthorizationLayoutItem(node: self.codeField, size: CGSize(width: maximumWidth - 88.0, height: 44.0), spacingBefore: AuthorizationLayoutItemSpacing(weight: 18.0, maxValue: 30.0), spacingAfter: AuthorizationLayoutItemSpacing(weight: 0.0, maxValue: 0.0)))
items.append(AuthorizationLayoutItem(node: self.codeSeparatorNode, size: CGSize(width: maximumWidth - 48.0, height: UIScreenPixel), spacingBefore: AuthorizationLayoutItemSpacing(weight: 0.0, maxValue: 0.0), spacingAfter: AuthorizationLayoutItemSpacing(weight: 0.0, maxValue: 0.0)))
if layout.size.width > 320.0 {
items.insert(AuthorizationLayoutItem(node: self.animationNode, size: animationSize, spacingBefore: AuthorizationLayoutItemSpacing(weight: 10.0, maxValue: 10.0), spacingAfter: AuthorizationLayoutItemSpacing(weight: 0.0, maxValue: 0.0)), at: 0)
self.animationNode.updateLayout(size: animationSize)
self.proceedNode.isHidden = false
self.animationNode.isHidden = false
self.animationNode.visibility = true
} else {
insets.top = navigationBarHeight
self.proceedNode.isHidden = true
self.animationNode.isHidden = true
}
let buttonFrame = CGRect(origin: CGPoint(x: floorToScreenPixels((layout.size.width - proceedSize.width) / 2.0), y: layout.size.height - insets.bottom - proceedSize.height - inset), size: proceedSize)
transition.updateFrame(node: self.proceedNode, frame: buttonFrame)
let dividerSize = self.dividerNode.updateLayout(width: maximumWidth)
transition.updateFrame(node: self.dividerNode, frame: CGRect(origin: CGPoint(x: floorToScreenPixels((layout.size.width - dividerSize.width) / 2.0), y: buttonFrame.minY - dividerSize.height), size: dividerSize))
if let _ = self.signInWithAppleButton, self.appleSignInAllowed {
self.dividerNode.isHidden = false
} else {
self.dividerNode.isHidden = true
}
let _ = layoutAuthorizationItems(bounds: CGRect(origin: CGPoint(x: 0.0, y: insets.top), size: CGSize(width: layout.size.width, height: layout.size.height - insets.top - insets.bottom - 120.0)), items: items, transition: transition, failIfDoesNotFit: false)
if let signInWithAppleButton = self.signInWithAppleButton, self.appleSignInAllowed {
signInWithAppleButton.isHidden = false
transition.updateFrame(view: signInWithAppleButton, frame: self.proceedNode.frame)
} else {
self.signInWithAppleButton?.isHidden = true
}
}
func activateInput() {
self.codeField.textField.becomeFirstResponder()
}
func animateError() {
self.codeField.layer.addShakeAnimation()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.proceedWithEmail?(self.currentEmail)
return false
}
@objc func proceedPressed() {
self.proceedWithEmail?(self.currentEmail)
}
@objc func signInWithApplePressed() {
self.signInWithApple?()
}
}