import Foundation import LottieSettings import UIKit import AsyncDisplayKit import Display import ComponentFlow import ButtonComponent import SwiftSignalKit import OverlayStatusController import AnimatedStickerNode import TelegramAnimatedStickerNode import AccountContext import TelegramPresentationData import PresentationDataUtils import TelegramCore public enum TwoFactorAuthSplashMode { public struct Intro { public var title: String public var text: String public var actionText: String public var doneText: String public var phoneNumber: String? public init( title: String, text: String, actionText: String, doneText: String, phoneNumber: String? ) { self.title = title self.text = text self.actionText = actionText self.doneText = doneText self.phoneNumber = phoneNumber } } case intro(Intro) case done(doneText: String) case recoveryDone(recoveredAccountData: RecoveredAccountData?, syncContacts: Bool, isPasswordSet: Bool) case remember } public final class TwoFactorAuthSplashScreen: ViewController { private let sharedContext: SharedAccountContext private let engine: SomeTelegramEngine private var presentationData: PresentationData private var mode: TwoFactorAuthSplashMode public var dismissConfirmation: ((@escaping () -> Void) -> Bool)? public init(sharedContext: SharedAccountContext, engine: SomeTelegramEngine, mode: TwoFactorAuthSplashMode, presentation: ViewControllerNavigationPresentation = .modalInLargeLayout) { self.sharedContext = sharedContext self.engine = engine self.mode = mode self.presentationData = self.sharedContext.currentPresentationData.with { $0 } let navigationBarTheme = NavigationBarTheme(rootControllerTheme: self.presentationData.theme, edgeEffectColor: .clear) super.init(navigationBarPresentationData: NavigationBarPresentationData(theme: navigationBarTheme, strings: NavigationBarStrings(back: self.presentationData.strings.Common_Back, close: self.presentationData.strings.Common_Close, more: self.presentationData.strings.Common_More))) self.navigationPresentation = presentation self.statusBar.statusBarStyle = self.presentationData.theme.rootController.statusBarStyle.style self.supportedOrientations = ViewControllerSupportedOrientations(regularSize: .all, compactSize: .portrait) self.navigationBar?.intrinsicCanTransitionInline = false let hasBackButton: Bool switch mode { case .done, .remember: hasBackButton = false default: hasBackButton = true } if hasBackButton { self.navigationItem.backBarButtonItem = UIBarButtonItem(title: self.presentationData.strings.Common_Back, style: .plain, target: nil, action: nil) } else { self.navigationItem.leftBarButtonItem = UIBarButtonItem(customDisplayNode: ASDisplayNode()) } self.attemptNavigation = { [weak self] f in guard let strongSelf = self, let dismissConfirmation = strongSelf.dismissConfirmation else { return true } return dismissConfirmation(f) } } required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } deinit { } override public func loadDisplayNode() { self.displayNode = TwoFactorAuthSplashScreenNode(sharedContext: self.sharedContext, presentationData: self.presentationData, mode: self.mode, action: { [weak self] in guard let strongSelf = self else { return } switch strongSelf.mode { case let .intro(intro): strongSelf.push(TwoFactorDataInputScreen(sharedContext: strongSelf.sharedContext, engine: strongSelf.engine, mode: .password(phoneNumber: intro.phoneNumber, doneText: intro.doneText), stateUpdated: { _ in }, presentation: strongSelf.navigationPresentation)) case .done, .remember: guard let navigationController = strongSelf.navigationController as? NavigationController else { return } navigationController.filterController(strongSelf, animated: true) case let .recoveryDone(recoveredAccountData, syncContacts, _): guard let navigationController = strongSelf.navigationController as? NavigationController else { return } switch strongSelf.engine { case let .unauthorized(engine): if let recoveredAccountData = recoveredAccountData { let _ = loginWithRecoveredAccountData(accountManager: strongSelf.sharedContext.accountManager, account: engine.account, recoveredAccountData: recoveredAccountData, syncContacts: syncContacts).start() } case .authorized: navigationController.filterController(strongSelf, animated: true) } } }) self.displayNodeDidLoad() } override public func containerLayoutUpdated(_ layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) { super.containerLayoutUpdated(layout, transition: transition) (self.displayNode as! TwoFactorAuthSplashScreenNode).containerLayoutUpdated(layout: layout, navigationHeight: self.navigationLayout(layout: layout).navigationFrame.maxY, transition: transition) } } private final class TwoFactorAuthSplashScreenNode: ViewControllerTracingNode { private var presentationData: PresentationData private let mode: TwoFactorAuthSplashMode private var animationSize: CGSize = CGSize() private var animationOffset: CGPoint = CGPoint() private let animationNode: AnimatedStickerNode private let titleNode: ImmediateTextNode private let textNodes: [ImmediateTextNode] private let textArrowNodes: [ASImageNode] private let button = ComponentView() private let buttonText: String private let action: () -> Void var inProgress: Bool = false { didSet { self.button.view?.isUserInteractionEnabled = !self.inProgress self.button.view?.alpha = self.inProgress ? 0.6 : 1.0 } } init(sharedContext: SharedAccountContext, presentationData: PresentationData, mode: TwoFactorAuthSplashMode, action: @escaping () -> Void) { self.presentationData = presentationData self.mode = mode self.animationNode = DefaultAnimatedStickerNodeImpl(lottieSettings: .noAccountFallback) let title: String let texts: [NSAttributedString] let buttonText: String let textFont = Font.regular(16.0) let textColor = self.presentationData.theme.list.itemPrimaryTextColor switch mode { case let .intro(intro): title = intro.title texts = [NSAttributedString(string: intro.text, font: textFont, textColor: textColor)] buttonText = intro.actionText self.animationNode.setup(source: AnimatedStickerNodeLocalFileSource(name: "TwoFactorSetupIntro"), width: 248, height: 248, playbackMode: .once, mode: .direct(cachePathPrefix: nil)) self.animationSize = CGSize(width: 124.0, height: 124.0) self.animationNode.visibility = true case let .done(doneText): title = self.presentationData.strings.TwoFactorSetup_Done_Title texts = [NSAttributedString(string: self.presentationData.strings.TwoFactorSetup_Done_Text, font: textFont, textColor: textColor)] buttonText = doneText self.animationNode.setup(source: AnimatedStickerNodeLocalFileSource(name: "TwoFactorSetupDone"), width: 248, height: 248, playbackMode: .loop, mode: .direct(cachePathPrefix: nil)) self.animationSize = CGSize(width: 124.0, height: 124.0) self.animationNode.visibility = true case let .recoveryDone(_, _, isPasswordSet): title = isPasswordSet ? self.presentationData.strings.TwoFactorSetup_ResetDone_Title : self.presentationData.strings.TwoFactorSetup_ResetDone_TitleNoPassword let rawText = isPasswordSet ? self.presentationData.strings.TwoFactorSetup_ResetDone_Text : self.presentationData.strings.TwoFactorSetup_ResetDone_TextNoPassword var splitTexts: [String] = [""] var index = rawText.startIndex while index != rawText.endIndex { let c = rawText[index] if c == ">" { splitTexts.append("") } else { splitTexts[splitTexts.count - 1].append(c) } index = rawText.index(after: index) } texts = splitTexts.map { NSAttributedString(string: $0, font: textFont, textColor: textColor) } buttonText = self.presentationData.strings.TwoFactorSetup_ResetDone_Action self.animationNode.setup(source: AnimatedStickerNodeLocalFileSource(name: isPasswordSet ? "TwoFactorSetupDone" : "TwoFactorRemovePasswordDone"), width: 248, height: 248, playbackMode: isPasswordSet ? .loop : .once, mode: .direct(cachePathPrefix: nil)) self.animationSize = CGSize(width: 124.0, height: 124.0) self.animationNode.visibility = true case .remember: title = self.presentationData.strings.TwoFactorRemember_Done_Title texts = [NSAttributedString(string: self.presentationData.strings.TwoFactorRemember_Done_Text, font: textFont, textColor: textColor)] buttonText = self.presentationData.strings.TwoFactorRemember_Done_Action self.animationNode.setup(source: AnimatedStickerNodeLocalFileSource(name: "TwoFactorSetupRememberSuccess"), width: 248, height: 248, playbackMode: .loop, mode: .direct(cachePathPrefix: nil)) self.animationSize = CGSize(width: 124.0, height: 124.0) self.animationNode.visibility = true } self.titleNode = ImmediateTextNode() self.titleNode.displaysAsynchronously = false self.titleNode.attributedText = NSAttributedString(string: title, font: Font.bold(32.0), textColor: self.presentationData.theme.list.itemPrimaryTextColor) self.titleNode.maximumNumberOfLines = 0 self.titleNode.textAlignment = .center self.textNodes = texts.map { text in let textNode = ImmediateTextNode() textNode.displaysAsynchronously = false textNode.attributedText = text textNode.maximumNumberOfLines = 0 textNode.lineSpacing = 0.1 textNode.textAlignment = .center return textNode } let arrowImage = generateTintedImage(image: UIImage(bundleImageName: "Chat/Input/Search/DownButton"), color: presentationData.theme.list.itemPrimaryTextColor) self.textArrowNodes = (0 ..< self.textNodes.count - 1).map { _ in let iconNode = ASImageNode() iconNode.image = arrowImage iconNode.alpha = 0.34 return iconNode } self.buttonText = buttonText self.action = action super.init() self.backgroundColor = self.presentationData.theme.list.plainBackgroundColor self.addSubnode(self.animationNode) self.addSubnode(self.titleNode) self.textNodes.forEach(self.addSubnode) self.textArrowNodes.forEach(self.addSubnode) } override func didLoad() { super.didLoad() } func containerLayoutUpdated(layout: ContainerViewLayout, navigationHeight: CGFloat, transition: ContainedViewLayoutTransition) { // The content above the button lays out in the band between the side insets when they differ (iPhone Duo's // side strip: the text ran under it); across the screen otherwise. let insetsDiffer = layout.safeInsets.left != layout.safeInsets.right let bandMinX: CGFloat = insetsDiffer ? layout.safeInsets.left : 0.0 let bandWidth: CGFloat = insetsDiffer ? layout.size.width - layout.safeInsets.left - layout.safeInsets.right : layout.size.width let sideInset: CGFloat = 32.0 let buttonSideInset: CGFloat = 48.0 let iconSpacing: CGFloat = 8.0 let titleSpacing: CGFloat = 19.0 let buttonHeight: CGFloat = 52.0 let iconSize: CGSize = self.animationSize var iconOffset = CGPoint() switch self.mode { case .done: iconOffset.x = 10.0 default: break } let titleSize = self.titleNode.updateLayout(CGSize(width: bandWidth - sideInset * 2.0, height: layout.size.height)) let textSizes = self.textNodes.map { $0.updateLayout(CGSize(width: bandWidth - sideInset * 2.0, height: layout.size.height)) } var combinedTextHeight: CGFloat = 0.0 let textSpacing: CGFloat = 32.0 for textSize in textSizes { combinedTextHeight += textSize.height } combinedTextHeight += CGFloat(max(0, textSizes.count - 1)) * textSpacing let contentHeight = iconSize.height + iconSpacing + titleSize.height + titleSpacing + combinedTextHeight var contentVerticalOrigin = floor((layout.size.height - contentHeight - iconSize.height / 2.0) / 2.0) let minimalBottomInset: CGFloat = 60.0 let bottomInset = layout.intrinsicInsets.bottom + minimalBottomInset // The bottom button spans iPhone Duo's side strip; it keeps clear only of a corner block at the bottom. let bottomSideInsets = layout.bottomEdgeSideInsets() let bottomBandMinX: CGFloat = insetsDiffer ? bottomSideInsets.left : 0.0 let bottomBandWidth: CGFloat = insetsDiffer ? layout.size.width - bottomSideInsets.left - bottomSideInsets.right : layout.size.width let buttonWidth = bottomBandWidth - buttonSideInset * 2.0 let buttonFrame = CGRect(origin: CGPoint(x: bottomBandMinX + floor((bottomBandWidth - buttonWidth) / 2.0), y: layout.size.height - bottomInset - buttonHeight), size: CGSize(width: buttonWidth, height: buttonHeight)) let _ = self.button.update( transition: ComponentTransition(transition), component: AnyComponent(ButtonComponent( background: ButtonComponent.Background( style: .glass, color: self.presentationData.theme.list.itemCheckColors.fillColor, foreground: self.presentationData.theme.list.itemCheckColors.foregroundColor, pressedColor: self.presentationData.theme.list.itemCheckColors.fillColor.withMultipliedAlpha(0.9), cornerRadius: 26.0 ), content: AnyComponentWithIdentity( id: AnyHashable("button"), component: AnyComponent(Text( text: self.buttonText, font: Font.semibold(17.0), color: self.presentationData.theme.list.itemCheckColors.foregroundColor )) ), action: self.action )), environment: {}, containerSize: buttonFrame.size ) if let buttonView = self.button.view { if buttonView.superview == nil { self.view.addSubview(buttonView) } transition.updateFrame(view: buttonView, frame: buttonFrame) buttonView.isHidden = self.buttonText.isEmpty buttonView.isUserInteractionEnabled = !self.inProgress buttonView.alpha = self.inProgress ? 0.6 : 1.0 } let maxContentVerticalOrigin = buttonFrame.minY - 12.0 - contentHeight contentVerticalOrigin = min(contentVerticalOrigin, maxContentVerticalOrigin) let iconFrame = CGRect(origin: CGPoint(x: bandMinX + floor((bandWidth - iconSize.width) / 2.0) + self.animationOffset.x, y: contentVerticalOrigin + self.animationOffset.y), size: iconSize).offsetBy(dx: iconOffset.x, dy: iconOffset.y) self.animationNode.updateLayout(size: iconFrame.size) transition.updateFrameAdditive(node: self.animationNode, frame: iconFrame) let titleFrame = CGRect(origin: CGPoint(x: bandMinX + floor((bandWidth - titleSize.width) / 2.0), y: iconFrame.maxY + iconSpacing), size: titleSize) transition.updateFrameAdditive(node: self.titleNode, frame: titleFrame) var nextTextOrigin: CGFloat = titleFrame.maxY + titleSpacing for i in 0 ..< self.textNodes.count { let textFrame = CGRect(origin: CGPoint(x: bandMinX + floor((bandWidth - textSizes[i].width) / 2.0), y: nextTextOrigin), size: textSizes[i]) transition.updateFrameAdditive(node: self.textNodes[i], frame: textFrame) if i != 0 { if let image = self.textArrowNodes[i - 1].image { let scaledImageSize = CGSize(width: floor(image.size.width * 0.7), height: floor(image.size.height * 0.7)) self.textArrowNodes[i - 1].frame = CGRect(origin: CGPoint(x: bandMinX + floor((bandWidth - scaledImageSize.width) / 2.0), y: nextTextOrigin - textSpacing + floor((textSpacing - scaledImageSize.height) / 2.0)), size: scaledImageSize) } } nextTextOrigin = textFrame.maxY + textSpacing } } }