From 4bf57d60fd9c44da5af3a36ec009de6b8c39b684 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Tue, 5 Nov 2019 22:20:55 +0400 Subject: [PATCH] Wait until authorized context is there --- .../TelegramUI/TelegramUI/AppDelegate.swift | 17 ++- .../TelegramUI/ApplicationContext.swift | 13 +- .../AuthorizationSequenceController.swift | 118 +++++++++--------- 3 files changed, 87 insertions(+), 61 deletions(-) diff --git a/submodules/TelegramUI/TelegramUI/AppDelegate.swift b/submodules/TelegramUI/TelegramUI/AppDelegate.swift index 9be8def29c..482ec25f4c 100644 --- a/submodules/TelegramUI/TelegramUI/AppDelegate.swift +++ b/submodules/TelegramUI/TelegramUI/AppDelegate.swift @@ -1147,8 +1147,21 @@ final class SharedApplicationContext { if let authContextValue = self.authContextValue { authContextValue.account.shouldBeServiceTaskMaster.set(.single(.never)) - authContextValue.rootController.view.endEditing(true) - authContextValue.rootController.dismiss() + if authContextValue.authorizationCompleted { + let accountId = authContextValue.account.id + let _ = (self.context.get() + |> filter { context in + return context?.context.account.id == accountId + } + |> take(1) + |> timeout(4.0, queue: .mainQueue(), alternate: .complete()) + |> deliverOnMainQueue).start(completed: { authContextValue.rootController.view.endEditing(true) + authContextValue.rootController.dismiss() + }) + } else { + authContextValue.rootController.view.endEditing(true) + authContextValue.rootController.dismiss() + } } self.authContextValue = context if let context = context { diff --git a/submodules/TelegramUI/TelegramUI/ApplicationContext.swift b/submodules/TelegramUI/TelegramUI/ApplicationContext.swift index 6e3705877b..95f59825b2 100644 --- a/submodules/TelegramUI/TelegramUI/ApplicationContext.swift +++ b/submodules/TelegramUI/TelegramUI/ApplicationContext.swift @@ -31,12 +31,23 @@ final class UnauthorizedApplicationContext { let isReady = Promise() + var authorizationCompleted: Bool = false + init(apiId: Int32, apiHash: String, sharedContext: SharedAccountContextImpl, account: UnauthorizedAccount, otherAccountPhoneNumbers: ((String, AccountRecordId, Bool)?, [(String, AccountRecordId, Bool)])) { self.sharedContext = sharedContext self.account = account let presentationData = sharedContext.currentPresentationData.with { $0 } - self.rootController = AuthorizationSequenceController(sharedContext: sharedContext, account: account, otherAccountPhoneNumbers: otherAccountPhoneNumbers, strings: presentationData.strings, theme: presentationData.theme, openUrl: sharedContext.applicationBindings.openUrl, apiId: apiId, apiHash: apiHash) + var authorizationCompleted: (() -> Void)? + + self.rootController = AuthorizationSequenceController(sharedContext: sharedContext, account: account, otherAccountPhoneNumbers: otherAccountPhoneNumbers, strings: presentationData.strings, theme: presentationData.theme, openUrl: sharedContext.applicationBindings.openUrl, apiId: apiId, apiHash: apiHash, authorizationCompleted: { + authorizationCompleted?() + }) + + authorizationCompleted = { [weak self] in + self?.authorizationCompleted = true + } + self.isReady.set(self.rootController.ready.get()) account.shouldBeServiceTaskMaster.set(sharedContext.applicationBindings.applicationInForeground |> map { value -> AccountServiceTaskMasterMode in diff --git a/submodules/TelegramUI/TelegramUI/AuthorizationSequenceController.swift b/submodules/TelegramUI/TelegramUI/AuthorizationSequenceController.swift index 04fdfb0121..6ff0635478 100644 --- a/submodules/TelegramUI/TelegramUI/AuthorizationSequenceController.swift +++ b/submodules/TelegramUI/TelegramUI/AuthorizationSequenceController.swift @@ -38,6 +38,7 @@ public final class AuthorizationSequenceController: NavigationController, MFMail private var strings: PresentationStrings public let theme: PresentationTheme private let openUrl: (String) -> Void + private let authorizationCompleted: () -> Void private var stateDisposable: Disposable? private let actionDisposable = MetaDisposable() @@ -50,7 +51,7 @@ public final class AuthorizationSequenceController: NavigationController, MFMail } private var didSetReady = false - public init(sharedContext: SharedAccountContext, account: UnauthorizedAccount, otherAccountPhoneNumbers: ((String, AccountRecordId, Bool)?, [(String, AccountRecordId, Bool)]), strings: PresentationStrings, theme: PresentationTheme, openUrl: @escaping (String) -> Void, apiId: Int32, apiHash: String) { + public init(sharedContext: SharedAccountContext, account: UnauthorizedAccount, otherAccountPhoneNumbers: ((String, AccountRecordId, Bool)?, [(String, AccountRecordId, Bool)]), strings: PresentationStrings, theme: PresentationTheme, openUrl: @escaping (String) -> Void, apiId: Int32, apiHash: String, authorizationCompleted: @escaping () -> Void) { self.sharedContext = sharedContext self.account = account self.otherAccountPhoneNumbers = otherAccountPhoneNumbers @@ -59,6 +60,7 @@ public final class AuthorizationSequenceController: NavigationController, MFMail self.strings = strings self.theme = theme self.openUrl = openUrl + self.authorizationCompleted = authorizationCompleted let navigationStatusBar: NavigationStatusBarStyle switch theme.rootController.statusBarStyle { @@ -719,68 +721,68 @@ public final class AuthorizationSequenceController: NavigationController, MFMail private func updateState(state: InnerState) { switch state { - case .authorized: - break - case let .state(state): - switch state { - case .empty: - if let _ = self.viewControllers.last as? AuthorizationSequenceSplashController { - } else { - var controllers: [ViewController] = [] - if self.otherAccountPhoneNumbers.1.isEmpty { - controllers.append(self.splashController()) - } else { - controllers.append(self.phoneEntryController(countryCode: defaultCountryCode(), number: "")) - } - self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) - } - case let .phoneEntry(countryCode, number): + case .authorized: + self.authorizationCompleted() + case let .state(state): + switch state { + case .empty: + if let _ = self.viewControllers.last as? AuthorizationSequenceSplashController { + } else { var controllers: [ViewController] = [] - if !self.otherAccountPhoneNumbers.1.isEmpty { - controllers.append(self.splashController()) - } - controllers.append(self.phoneEntryController(countryCode: countryCode, number: number)) - self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) - case let .confirmationCodeEntry(number, type, _, timeout, nextType, _): - var controllers: [ViewController] = [] - if !self.otherAccountPhoneNumbers.1.isEmpty { - controllers.append(self.splashController()) - } - controllers.append(self.phoneEntryController(countryCode: defaultCountryCode(), number: "")) - controllers.append(self.codeEntryController(number: number, type: type, nextType: nextType, timeout: timeout, termsOfService: nil)) - self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) - case let .passwordEntry(hint, _, _, suggestReset, syncContacts): - var controllers: [ViewController] = [] - if !self.otherAccountPhoneNumbers.1.isEmpty { - controllers.append(self.splashController()) - } - controllers.append(self.passwordEntryController(hint: hint, suggestReset: suggestReset, syncContacts: syncContacts)) - self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) - case let .passwordRecovery(_, _, _, emailPattern, syncContacts): - var controllers: [ViewController] = [] - if !self.otherAccountPhoneNumbers.1.isEmpty { - controllers.append(self.splashController()) - } - controllers.append(self.passwordRecoveryController(emailPattern: emailPattern, syncContacts: syncContacts)) - self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) - case let .awaitingAccountReset(protectedUntil, number, _): - var controllers: [ViewController] = [] - if !self.otherAccountPhoneNumbers.1.isEmpty { - controllers.append(self.splashController()) - } - controllers.append(self.awaitingAccountResetController(protectedUntil: protectedUntil, number: number)) - self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) - case let .signUp(_, _, firstName, lastName, termsOfService, _): - var controllers: [ViewController] = [] - var displayCancel = false - if !self.otherAccountPhoneNumbers.1.isEmpty { + if self.otherAccountPhoneNumbers.1.isEmpty { controllers.append(self.splashController()) } else { - displayCancel = true + controllers.append(self.phoneEntryController(countryCode: defaultCountryCode(), number: "")) } - controllers.append(self.signUpController(firstName: firstName, lastName: lastName, termsOfService: termsOfService, displayCancel: displayCancel)) self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) - } + } + case let .phoneEntry(countryCode, number): + var controllers: [ViewController] = [] + if !self.otherAccountPhoneNumbers.1.isEmpty { + controllers.append(self.splashController()) + } + controllers.append(self.phoneEntryController(countryCode: countryCode, number: number)) + self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) + case let .confirmationCodeEntry(number, type, _, timeout, nextType, _): + var controllers: [ViewController] = [] + if !self.otherAccountPhoneNumbers.1.isEmpty { + controllers.append(self.splashController()) + } + controllers.append(self.phoneEntryController(countryCode: defaultCountryCode(), number: "")) + controllers.append(self.codeEntryController(number: number, type: type, nextType: nextType, timeout: timeout, termsOfService: nil)) + self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) + case let .passwordEntry(hint, _, _, suggestReset, syncContacts): + var controllers: [ViewController] = [] + if !self.otherAccountPhoneNumbers.1.isEmpty { + controllers.append(self.splashController()) + } + controllers.append(self.passwordEntryController(hint: hint, suggestReset: suggestReset, syncContacts: syncContacts)) + self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) + case let .passwordRecovery(_, _, _, emailPattern, syncContacts): + var controllers: [ViewController] = [] + if !self.otherAccountPhoneNumbers.1.isEmpty { + controllers.append(self.splashController()) + } + controllers.append(self.passwordRecoveryController(emailPattern: emailPattern, syncContacts: syncContacts)) + self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) + case let .awaitingAccountReset(protectedUntil, number, _): + var controllers: [ViewController] = [] + if !self.otherAccountPhoneNumbers.1.isEmpty { + controllers.append(self.splashController()) + } + controllers.append(self.awaitingAccountResetController(protectedUntil: protectedUntil, number: number)) + self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) + case let .signUp(_, _, firstName, lastName, termsOfService, _): + var controllers: [ViewController] = [] + var displayCancel = false + if !self.otherAccountPhoneNumbers.1.isEmpty { + controllers.append(self.splashController()) + } else { + displayCancel = true + } + controllers.append(self.signUpController(firstName: firstName, lastName: lastName, termsOfService: termsOfService, displayCancel: displayCancel)) + self.setViewControllers(controllers, animated: !self.viewControllers.isEmpty) + } } }