From 874396fbbb82d8f744a8d45e4feecf022bd66642 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Tue, 9 Jun 2020 17:05:24 +0400 Subject: [PATCH] Fix call issue --- .../TelegramUI/Sources/AppDelegate.swift | 32 ++++++++++++++++--- .../Sources/ApplicationContext.swift | 23 +++++++++++-- .../TelegramUI/Sources/ChatController.swift | 19 +++++++---- .../Sources/ListMessageSnippetItemNode.swift | 6 ++-- .../TelegramUI/Sources/TextLinkHandling.swift | 4 +-- 5 files changed, 65 insertions(+), 19 deletions(-) diff --git a/submodules/TelegramUI/Sources/AppDelegate.swift b/submodules/TelegramUI/Sources/AppDelegate.swift index f5a62bee9a..2f84234a5a 100644 --- a/submodules/TelegramUI/Sources/AppDelegate.swift +++ b/submodules/TelegramUI/Sources/AppDelegate.swift @@ -1726,11 +1726,8 @@ final class SharedApplicationContext { func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { if #available(iOS 10.0, *) { if let startCallIntent = userActivity.interaction?.intent as? SupportedStartCallIntent { - guard let context = self.contextValue?.context else { - return true - } let startCall: (Int32) -> Void = { userId in - let _ = context.sharedContext.callManager?.requestCall(account: context.account, peerId: PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), endCurrentIfAny: false) + self.startCallWhenReady(accountId: nil, peerId: PeerId(namespace: Namespaces.Peer.CloudUser, id: userId)) } func cleanPhoneNumber(_ text: String) -> String { @@ -1776,6 +1773,9 @@ final class SharedApplicationContext { case .phoneNumber: let phoneNumber = cleanPhoneNumber(value) if !phoneNumber.isEmpty { + guard let context = self.contextValue?.context else { + return true + } let _ = (context.account.postbox.transaction { transaction -> PeerId? in var result: PeerId? for peerId in transaction.getContactPeerIds() { @@ -1907,13 +1907,35 @@ final class SharedApplicationContext { } private func openNotificationSettingsWhenReady() { - let signal = (self.authorizedContext() + let _ = (self.authorizedContext() |> take(1) |> deliverOnMainQueue).start(next: { context in context.openNotificationSettings() }) } + private func startCallWhenReady(accountId: AccountRecordId?, peerId: PeerId) { + let signal = self.sharedContextPromise.get() + |> take(1) + |> mapToSignal { sharedApplicationContext -> Signal in + if let accountId = accountId { + sharedApplicationContext.sharedContext.switchToAccount(id: accountId) + return self.authorizedContext() + |> filter { context in + context.context.account.id == accountId + } + |> take(1) + } else { + return self.authorizedContext() + |> take(1) + } + } + self.openChatWhenReadyDisposable.set((signal + |> deliverOnMainQueue).start(next: { context in + context.startCall(peerId: peerId) + })) + } + private func openChatWhenReady(accountId: AccountRecordId?, peerId: PeerId, messageId: MessageId? = nil, activateInput: Bool = false) { let signal = self.sharedContextPromise.get() |> take(1) diff --git a/submodules/TelegramUI/Sources/ApplicationContext.swift b/submodules/TelegramUI/Sources/ApplicationContext.swift index 6f9257714d..fb1b768054 100644 --- a/submodules/TelegramUI/Sources/ApplicationContext.swift +++ b/submodules/TelegramUI/Sources/ApplicationContext.swift @@ -84,7 +84,8 @@ final class AuthorizedApplicationContext { let notificationController: NotificationContainerController private var scheduledOpenNotificationSettings: Bool = false - private var scheduledOperChatWithPeerId: (PeerId, MessageId?, Bool)? + private var scheduledOpenChatWithPeerId: (PeerId, MessageId?, Bool)? + private let scheduledCallPeerDisposable = MetaDisposable() private var scheduledOpenExternalUrl: URL? private let passcodeStatusDisposable = MetaDisposable() @@ -738,6 +739,7 @@ final class AuthorizedApplicationContext { self.termsOfServiceProceedToBotDisposable.dispose() self.watchNavigateToMessageDisposable.dispose() self.permissionsDisposable.dispose() + self.scheduledCallPeerDisposable.dispose() } func openNotificationSettings() { @@ -748,6 +750,23 @@ final class AuthorizedApplicationContext { } } + func startCall(peerId: PeerId) { + guard let appLockContext = self.context.sharedContext.appLockContext as? AppLockContextImpl else { + return + } + self.scheduledCallPeerDisposable.set((appLockContext.isCurrentlyLocked + |> filter { + !$0 + } + |> take(1) + |> deliverOnMainQueue).start(next: { [weak self] _ in + guard let strongSelf = self else { + return + } + let _ = strongSelf.context.sharedContext.callManager?.requestCall(account: strongSelf.context.account, peerId: peerId, endCurrentIfAny: false) + })) + } + func openChatWithPeerId(peerId: PeerId, messageId: MessageId? = nil, activateInput: Bool = false) { var visiblePeerId: PeerId? if let controller = self.rootController.topViewController as? ChatControllerImpl, case let .peer(peerId) = controller.chatLocation { @@ -758,7 +777,7 @@ final class AuthorizedApplicationContext { if self.rootController.rootTabController != nil { self.context.sharedContext.navigateToChatController(NavigateToChatControllerParams(navigationController: self.rootController, context: self.context, chatLocation: .peer(peerId), subject: messageId.flatMap { .message($0) }, activateInput: activateInput)) } else { - self.scheduledOperChatWithPeerId = (peerId, messageId, activateInput) + self.scheduledOpenChatWithPeerId = (peerId, messageId, activateInput) } } } diff --git a/submodules/TelegramUI/Sources/ChatController.swift b/submodules/TelegramUI/Sources/ChatController.swift index fa000fb9f4..8511d3895f 100644 --- a/submodules/TelegramUI/Sources/ChatController.swift +++ b/submodules/TelegramUI/Sources/ChatController.swift @@ -1223,7 +1223,7 @@ public final class ChatControllerImpl: TelegramBaseController, ChatController, G if let strongSelf = self { switch action { case let .url(url): - var (cleanUrl, _) = parseUrl(url: url) + var (cleanUrl, _) = parseUrl(url: url, wasConcealed: false) var canAddToReadingList = true var canOpenIn = availableOpenInOptions(context: strongSelf.context, item: .url(url: url)).count > 1 let mailtoString = "mailto:" @@ -9272,7 +9272,7 @@ private final class ContextControllerContentSourceImpl: ContextControllerContent } } -func parseUrl(url: String) -> (string: String, concealed: Bool) { +func parseUrl(url: String, wasConcealed: Bool) -> (string: String, concealed: Bool) { var parsedUrlValue: URL? if let parsed = URL(string: url) { parsedUrlValue = parsed @@ -9300,7 +9300,7 @@ func parseUrl(url: String) -> (string: String, concealed: Bool) { hasNonLatin = true } } - var concealed = false + var concealed = wasConcealed if hasLatin && hasNonLatin { concealed = true } @@ -9316,6 +9316,13 @@ func parseUrl(url: String) -> (string: String, concealed: Bool) { concealed = false } + let whitelistedSchemes: [String] = [ + "tel", + ] + if let parsedUrlValue = parsedUrlValue, let scheme = parsedUrlValue.scheme, whitelistedSchemes.contains(scheme) { + concealed = false + } + return (rawDisplayUrl, concealed) } @@ -9356,10 +9363,8 @@ func openUserGeneratedUrl(context: AccountContext, url: String, concealed: Bool, })) } - let (parsedString, parsedConcealed) = parseUrl(url: url) - if parsedConcealed { - concealed = true - } + let (parsedString, parsedConcealed) = parseUrl(url: url, wasConcealed: concealed) + concealed = parsedConcealed if concealed { var rawDisplayUrl: String = parsedString diff --git a/submodules/TelegramUI/Sources/ListMessageSnippetItemNode.swift b/submodules/TelegramUI/Sources/ListMessageSnippetItemNode.swift index 4152079a7d..773aadf8af 100644 --- a/submodules/TelegramUI/Sources/ListMessageSnippetItemNode.swift +++ b/submodules/TelegramUI/Sources/ListMessageSnippetItemNode.swift @@ -244,7 +244,7 @@ final class ListMessageSnippetItemNode: ListMessageNode { isInstantView = true } - let (parsedUrl, _) = parseUrl(url: content.url) + let (parsedUrl, _) = parseUrl(url: content.url, wasConcealed: false) primaryUrl = parsedUrl @@ -316,7 +316,7 @@ final class ListMessageSnippetItemNode: ListMessageNode { } let tempUrlString = nsString.substring(with: range) - var (urlString, concealed) = parseUrl(url: tempUrlString) + var (urlString, concealed) = parseUrl(url: tempUrlString, wasConcealed: false) let rawUrlString = urlString var parsedUrl = URL(string: urlString) @@ -338,7 +338,7 @@ final class ListMessageSnippetItemNode: ListMessageNode { } let mutableDescriptionText = NSMutableAttributedString() - let (messageTextUrl, _) = parseUrl(url: item.message.text) + let (messageTextUrl, _) = parseUrl(url: item.message.text, wasConcealed: false) if messageTextUrl != rawUrlString { mutableDescriptionText.append(NSAttributedString(string: item.message.text + "\n", font: descriptionFont, textColor: item.theme.list.itemSecondaryTextColor)) diff --git a/submodules/TelegramUI/Sources/TextLinkHandling.swift b/submodules/TelegramUI/Sources/TextLinkHandling.swift index 3a0ea94e09..f25dd539ed 100644 --- a/submodules/TelegramUI/Sources/TextLinkHandling.swift +++ b/submodules/TelegramUI/Sources/TextLinkHandling.swift @@ -91,7 +91,7 @@ func handleTextLinkActionImpl(context: AccountContext, peerId: PeerId?, navigate case .tap: switch itemLink { case .url(let url, var concealed): - let (parsedString, parsedConcealed) = parseUrl(url: url) + let (parsedString, parsedConcealed) = parseUrl(url: url, wasConcealed: false) if parsedConcealed { concealed = true } @@ -128,7 +128,7 @@ func handleTextLinkActionImpl(context: AccountContext, peerId: PeerId?, navigate let canOpenIn = availableOpenInOptions(context: context, item: .url(url: url)).count > 1 let openText = canOpenIn ? presentationData.strings.Conversation_FileOpenIn : presentationData.strings.Conversation_LinkDialogOpen let actionSheet = ActionSheetController(presentationData: presentationData) - let (displayUrl, _) = parseUrl(url: url) + let (displayUrl, _) = parseUrl(url: url, wasConcealed: false) actionSheet.setItemGroups([ActionSheetItemGroup(items: [ ActionSheetTextItem(title: displayUrl), ActionSheetButtonItem(title: openText, color: .accent, action: { [weak actionSheet] in