-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathAuth_CodeEntry.swift
423 lines (351 loc) · 15 KB
/
Auth_CodeEntry.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//
// Auth_CodeEntry.swift
// Telegram
//
// Created by Mike Renoir on 15.02.2022.
// Copyright © 2022 Telegram. All rights reserved.
//
import Foundation
import TGUIKit
import TelegramCore
import AppKit
import KeyboardKey
import SwiftSignalKit
import TelegramMedia
private final class Auth_CodeEntryHeaderView : View {
enum IconType {
case phone
case desktop
case pirate
var header: String {
switch self {
case .phone:
return strings().loginNewCodeEnterSms
case .desktop:
return strings().loginNewCodeEnterCode
case .pirate:
return strings().loginNewCodeEnterSms
}
}
}
private let playerView:LottiePlayerView = LottiePlayerView()
private let header: TextView = TextView()
private let desc: TextView = TextView()
private(set) var type: IconType = .phone
private var descAttr: NSAttributedString?
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(playerView)
addSubview(header)
addSubview(desc)
header.userInteractionEnabled = false
header.isSelectable = false
desc.isSelectable = false
updateLocalizationAndTheme(theme: theme)
}
override func updateLocalizationAndTheme(theme: PresentationTheme) {
super.updateLocalizationAndTheme(theme: theme)
let theme = theme as! TelegramPresentationTheme
let layout = TextViewLayout(.initialize(string: self.type.header, color: theme.colors.text, font: Auth_Insets.headerFont))
layout.measure(width: frame.width)
self.header.update(layout)
if let descAttr = descAttr {
let layout = TextViewLayout(descAttr, alignment: .center)
layout.interactions = globalLinkExecutor
layout.measure(width: frame.width)
self.desc.update(layout)
}
switch self.type {
case .pirate:
if let data = LocalAnimatedSticker.pirate_flag.data {
self.playerView.set(LottieAnimation(compressed: data, key: .init(key: .bundle("pirate_flag"), size: Auth_Insets.logoSize, backingScale: Int(System.backingScale), fitzModifier: nil), playPolicy: .loop, colors: []))
}
default:
if let data = LocalAnimatedSticker.code_note.data {
var colors:[LottieColor] = []
colors.append(.init(keyPath: "Bubble.Group 1.Fill 1", color: theme.colors.accent))
colors.append(.init(keyPath: "Note.Path.Fill 1", color: theme.colors.grayText))
colors.append(.init(keyPath: "Note.Path-2.Stroke 1", color: theme.colors.grayText))
colors.append(.init(keyPath: "Phone.Combined-Shape.Fill 1", color: theme.colors.grayText))
self.playerView.set(LottieAnimation(compressed: data, key: .init(key: .bundle("code_note"), size: Auth_Insets.logoSize, backingScale: Int(System.backingScale), fitzModifier: nil), playPolicy: .onceEnd, colors: colors))
}
}
self.layout()
}
func update(desc: NSAttributedString, type: IconType) {
self.descAttr = desc
self.type = type
updateLocalizationAndTheme(theme: theme)
}
override func layout() {
super.layout()
self.playerView.setFrameSize(Auth_Insets.logoSize)
self.playerView.centerX(y: 0)
self.header.centerX(y: self.playerView.frame.maxY + 20)
self.desc.centerX(y: self.header.frame.maxY + 10)
}
func playAnimation() {
playerView.playAgain()
}
var height: CGFloat {
return self.desc.frame.maxY
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
final class Auth_CodeEntryView: View {
private let header: Auth_CodeEntryHeaderView
private let container = View()
private let control = Auth_CodeEntryContol(frame: .zero)
private let error: LoginErrorStateView = LoginErrorStateView()
private let nextView: Auth_NextView = Auth_NextView()
private var takeResend:(()->Void)?
private var nextTextView: TextView?
private let disposable = MetaDisposable()
private var fragmentUrl: String?
required init(frame frameRect: NSRect) {
header = Auth_CodeEntryHeaderView(frame: frameRect.size.bounds)
super.init(frame: frameRect)
container.addSubview(header)
container.addSubview(control)
container.addSubview(error)
container.addSubview(nextView)
nextView.set(handler: { [weak self] _ in
if self?.header.type == .pirate {
self?.openFragment()
} else {
self?.control.invoke()
}
}, for: .Click)
addSubview(container)
updateLocalizationAndTheme(theme: theme)
}
override func updateLocalizationAndTheme(theme: PresentationTheme) {
super.updateLocalizationAndTheme(theme: theme)
nextView.updateLocalizationAndTheme(theme: theme)
}
override func layout() {
super.layout()
container.setFrameSize(NSMakeSize(frame.width, header.height + Auth_Insets.betweenHeader + control.frame.height + Auth_Insets.betweenNextView + Auth_Insets.nextHeight))
header.setFrameSize(NSMakeSize(frame.width, header.height))
header.centerX(y: 0)
control.centerX(y: header.frame.maxY + Auth_Insets.betweenHeader)
error.centerX(y: control.frame.maxY + Auth_Insets.betweenError)
nextView.centerX(y: control.frame.maxY + Auth_Insets.betweenNextView)
container.center()
nextTextView?.centerX(y: container.frame.maxY + Auth_Insets.betweenHeader)
}
func applyExternalLoginCode(_ code: String) {
if !code.isEmpty {
let chars = Array(code).map { String($0) }
self.control.insertAll(chars.compactMap { Int($0) })
}
}
func update(locked: Bool, error: AuthorizationCodeVerificationError?, number: String, type: SentAuthorizationCodeType, timeout: Int32?, nextType: AuthorizationCodeNextType?, takeEdit:@escaping()->Void, takeNext:@escaping(String)->Void, takeResend: @escaping()->Void, takeError:@escaping()->Void) {
let info: String
let iconType: Auth_CodeEntryHeaderView.IconType
let length: Int32
var nextString: String = strings().loginNext
var detectBold = true
switch type {
case let .otherSession(_length):
length = _length
info = strings().loginNewCodeCodeInfo(formatPhoneNumber(number))
iconType = .desktop
case let .sms(_length):
length = _length
info = strings().loginNewCodeSmsInfo(formatPhoneNumber(number))
iconType = .phone
case let .call(_length):
length = _length
iconType = .phone
info = strings().loginNewCodeCallInfo(formatPhoneNumber(number))
case let .fragment(url, _length):
length = _length
iconType = .pirate
info = strings().loginNewCodeFragmentInfo(formatPhoneNumber(number))
nextString = strings().loginNextFragment
self.fragmentUrl = url
case let .email(pattern, _length, _, _, _, _):
length = _length
info = strings().loginNewCodeEmail(pattern)
iconType = .phone
detectBold = false
default:
fatalError()
}
let attr = parseMarkdownIntoAttributedString(info, attributes: MarkdownAttributes(body: MarkdownAttributeSet(font: Auth_Insets.infoFont, textColor: theme.colors.grayText), bold: MarkdownAttributeSet(font: Auth_Insets.infoFontBold, textColor: theme.colors.grayText), link: MarkdownAttributeSet(font: Auth_Insets.infoFont, textColor: theme.colors.link), linkAttribute: { contents in
return (NSAttributedString.Key.link.rawValue, inAppLink.callback(contents, { url in
if url.isEmpty {
takeEdit()
} else {
execute(inapp: .external(link: url, false))
}
}))
})).mutableCopy() as! NSMutableAttributedString
if detectBold {
attr.detectBoldColorInString(with: .medium(.header))
}
self.header.update(desc: attr, type: iconType)
let size = self.control.update(count: Int(length))
self.control.setFrameSize(size)
self.control.takeNext = takeNext
self.control.takeError = takeError
self.control.set(locked: locked, animated: true)
if let error = error {
let textError:String
switch error {
case .limitExceeded:
textError = strings().loginFloodWait
case .invalidCode:
textError = strings().phoneCodeInvalid
case .generic:
textError = strings().phoneCodeExpired
case .codeExpired:
textError = strings().phoneCodeExpired
case .invalidEmailToken:
fatalError()
case .invalidEmailAddress:
fatalError()
}
self.error.state.set(.error(textError))
self.control.shake()
self.control.moveToStart()
} else {
self.error.state.set(.normal)
}
nextView.updateLocked(locked, string: nextString)
self.takeResend = takeResend
needsLayout = true
if let timeout = timeout {
runNextTimer(type, nextType, timeout)
}
updateAfterTick(type, nextType, timeout)
}
private func openFragment() {
if let url = self.fragmentUrl {
execute(inapp: .external(link: url, false))
}
}
deinit {
disposable.dispose()
}
func runNextTimer(_ type:SentAuthorizationCodeType, _ nextType:AuthorizationCodeNextType?, _ timeout:Int32) {
disposable.set(countdown(Double(timeout), delay: 1).start(next: { [weak self] value in
self?.updateAfterTick(type, nextType, Int32(value))
}, completed: {
}))
}
func updateAfterTick(_ type:SentAuthorizationCodeType, _ nextType:AuthorizationCodeNextType?, _ timeout:Int32?) {
var nextText:String = ""
switch type {
case .otherSession:
nextText = strings().loginSendSmsIfNotReceivedAppCode
default:
break
}
if let nextType = nextType {
if let timeout = timeout {
let timeout = Int(timeout)
let minutes = timeout / 60;
let sec = timeout % 60;
let secValue = sec > 9 ? "\(sec)" : "0\(sec)"
if timeout > 0 {
switch nextType {
case .call:
nextText = strings().loginWillCall(minutes, secValue)
break
case .sms:
nextText = strings().loginWillSendSms(minutes, secValue)
break
default:
break
}
} else {
switch nextType {
case .call:
nextText = strings().loginPhoneDialed
break
default:
break
}
}
} else {
nextText = strings().loginSendSmsIfNotReceivedAppCode
}
}
var nextLayout: TextViewLayout? = nil
if !nextText.isEmpty {
let attr = NSMutableAttributedString()
if case .otherSession = type {
_ = attr.append(string: nextText, color: theme.colors.link , font: .normal(.title))
attr.add(link: inAppLink.callback("resend", { [weak self] _ in
self?.takeResend?()
}), for: attr.range)
if timeout == nil {
attr.addAttribute(NSAttributedString.Key.foregroundColor, value: theme.colors.link, range: attr.range)
} else if let timeout = timeout {
attr.addAttribute(NSAttributedString.Key.foregroundColor, value: timeout <= 0 ? theme.colors.link : theme.colors.grayText, range: attr.range)
}
} else {
_ = attr.append(string: nextText, color: theme.colors.grayText, font: .normal(.title))
}
let layout = TextViewLayout(attr)
layout.interactions = globalLinkExecutor
layout.measure(width: frame.width - 40)
nextLayout = layout
}
if let nextLayout = nextLayout {
let current: TextView
if let nextTextView = nextTextView {
current = nextTextView
} else {
current = TextView()
self.nextTextView = current
addSubview(current)
}
var point = focus(nextLayout.layoutSize).origin
point.y = container.frame.maxY + 30
current.update(nextLayout, origin: point)
} else if let nextTextView = nextTextView {
performSubviewRemoval(nextTextView, animated: true)
self.nextTextView = nil
}
needsLayout = true
}
func playAnimation() {
header.playAnimation()
control.moveToStart()
}
func firstResponder() -> NSResponder? {
return control.firstResponder()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
final class Auth_CodeEntryController : GenericViewController<Auth_CodeEntryView> {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
}
override func viewDidLoad() {
super.viewDidLoad()
readyOnce()
}
override func firstResponder() -> NSResponder? {
return genericView.firstResponder()
}
func applyExternalLoginCode(_ code: String) {
self.genericView.applyExternalLoginCode(code)
}
func update(locked: Bool, error: AuthorizationCodeVerificationError?, number: String, type: SentAuthorizationCodeType, timeout: Int32?, nextType: AuthorizationCodeNextType?, takeEdit:@escaping()->Void, takeNext:@escaping(String)->Void, takeResend:@escaping()->Void, takeError:@escaping()->Void) {
self.genericView.update(locked: locked, error: error, number: number, type: type, timeout: timeout, nextType: nextType, takeEdit: takeEdit, takeNext: takeNext, takeResend: takeResend, takeError: takeError)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if animated {
genericView.playAnimation()
}
}
}