Swiftgram/submodules/TelegramUI/TelegramUI/AuthorizationSequencePhoneEntryController.swift

176 lines
7.8 KiB
Swift

import Foundation
import UIKit
import Display
import AsyncDisplayKit
import SwiftSignalKit
import TelegramCore
import Postbox
import TelegramPresentationData
import ProgressNavigationButtonNode
import AccountContext
import CountrySelectionUI
final class AuthorizationSequencePhoneEntryController: ViewController {
private var controllerNode: AuthorizationSequencePhoneEntryControllerNode {
return self.displayNode as! AuthorizationSequencePhoneEntryControllerNode
}
private let sharedContext: SharedAccountContext
private let isTestingEnvironment: Bool
private let otherAccountPhoneNumbers: ((String, AccountRecordId, Bool)?, [(String, AccountRecordId, Bool)])
private let network: Network
private let strings: PresentationStrings
private let theme: PresentationTheme
private let openUrl: (String) -> Void
private let back: () -> Void
private var currentData: (Int32, String?, String)?
var inProgress: Bool = false {
didSet {
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))
}
self.controllerNode.inProgress = self.inProgress
}
}
var loginWithNumber: ((String, Bool) -> Void)?
private let termsDisposable = MetaDisposable()
private let hapticFeedback = HapticFeedback()
init(sharedContext: SharedAccountContext, isTestingEnvironment: Bool, otherAccountPhoneNumbers: ((String, AccountRecordId, Bool)?, [(String, AccountRecordId, Bool)]), network: Network, strings: PresentationStrings, theme: PresentationTheme, openUrl: @escaping (String) -> Void, back: @escaping () -> Void) {
self.sharedContext = sharedContext
self.isTestingEnvironment = isTestingEnvironment
self.otherAccountPhoneNumbers = otherAccountPhoneNumbers
self.network = network
self.strings = strings
self.theme = theme
self.openUrl = openUrl
self.back = back
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.rootController.statusBarStyle.style
self.attemptNavigation = { _ in
return false
}
self.navigationBar?.backPressed = {
back()
}
if !otherAccountPhoneNumbers.1.isEmpty {
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: strings.Common_Cancel, style: .plain, target: self, action: #selector(self.cancelPressed))
}
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: strings.Common_Next, style: .done, target: self, action: #selector(self.nextPressed))
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
self.termsDisposable.dispose()
}
@objc private func cancelPressed() {
self.back()
}
func updateData(countryCode: Int32, countryName: String?, number: String) {
self.currentData = (countryCode, countryName, number)
if self.isNodeLoaded {
self.controllerNode.codeAndNumber = (countryCode, countryName, number)
}
}
override public func loadDisplayNode() {
self.displayNode = AuthorizationSequencePhoneEntryControllerNode(strings: self.strings, theme: self.theme, debugAction: { [weak self] in
guard let strongSelf = self else {
return
}
self?.present(debugController(sharedContext: strongSelf.sharedContext, context: nil, modal: true), in: .window(.root), with: ViewControllerPresentationArguments(presentationAnimation: .modalSheet))
}, hasOtherAccounts: self.otherAccountPhoneNumbers.0 != nil)
if let (code, name, number) = self.currentData {
self.controllerNode.codeAndNumber = (code, name, number)
}
self.displayNodeDidLoad()
self.controllerNode.selectCountryCode = { [weak self] in
if let strongSelf = self {
let controller = AuthorizationSequenceCountrySelectionController(strings: strongSelf.strings, theme: strongSelf.theme)
controller.completeWithCountryCode = { code, name in
if let strongSelf = self, let currentData = strongSelf.currentData {
strongSelf.updateData(countryCode: Int32(code), countryName: name, number: currentData.2)
strongSelf.controllerNode.activateInput()
}
}
controller.dismissed = {
self?.controllerNode.activateInput()
}
strongSelf.controllerNode.view.endEditing(true)
strongSelf.present(controller, in: .window(.root))
}
}
self.controllerNode.checkPhone = { [weak self] in
self?.nextPressed()
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.controllerNode.activateInput()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.controllerNode.activateInput()
}
override func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
super.containerLayoutUpdated(layout, transition: transition)
self.controllerNode.containerLayoutUpdated(layout, navigationBarHeight: self.navigationHeight, transition: transition)
}
@objc func nextPressed() {
let (_, _, number) = self.controllerNode.codeAndNumber
if !number.isEmpty {
let logInNumber = formatPhoneNumber(self.controllerNode.currentNumber)
var existing: (String, AccountRecordId)?
for (number, id, isTestingEnvironment) in self.otherAccountPhoneNumbers.1 {
if isTestingEnvironment == self.isTestingEnvironment && formatPhoneNumber(number) == logInNumber {
existing = (number, id)
}
}
if let (_, id) = existing {
var actions: [TextAlertAction] = []
if let (current, _, _) = self.otherAccountPhoneNumbers.0, logInNumber != formatPhoneNumber(current) {
actions.append(TextAlertAction(type: .genericAction, title: self.strings.Login_PhoneNumberAlreadyAuthorizedSwitch, action: { [weak self] in
self?.sharedContext.switchToAccount(id: id, fromSettingsController: nil, withChatListController: nil)
self?.back()
}))
}
actions.append(TextAlertAction(type: .defaultAction, title: self.strings.Common_OK, action: {}))
self.present(standardTextAlertController(theme: AlertControllerTheme(presentationTheme: self.theme), title: nil, text: self.strings.Login_PhoneNumberAlreadyAuthorized, actions: actions), in: .window(.root))
} else {
self.loginWithNumber?(self.controllerNode.currentNumber, self.controllerNode.syncContacts)
}
} else {
hapticFeedback.error()
self.controllerNode.animateError()
}
}
}