From 9e57f852e5abc656cff7415227b3db7ae91cbbfd Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Fri, 26 Feb 2021 02:18:07 +0400 Subject: [PATCH] Fix immediate external sharing --- .../Sources/ShareController.swift | 25 +++++++++++++------ .../Sources/ShareControllerNode.swift | 20 +++++++++++---- .../ShareItems/Sources/ShareItems.swift | 3 ++- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/submodules/ShareController/Sources/ShareController.swift b/submodules/ShareController/Sources/ShareController.swift index c26f12aa7d..2cf7ced645 100644 --- a/submodules/ShareController/Sources/ShareController.swift +++ b/submodules/ShareController/Sources/ShareController.swift @@ -469,7 +469,7 @@ public final class ShareController: ViewController { } strongSelf.present(standardTextAlertController(theme: AlertControllerTheme(presentationData: strongSelf.presentationData), title: title, text: text, actions: [TextAlertAction(type: .defaultAction, title: strongSelf.presentationData.strings.Common_OK, action: {})]), in: .window(.root)) }, externalShare: self.externalShare, immediateExternalShare: self.immediateExternalShare, immediatePeerId: self.immediatePeerId, shares: self.shares, fromForeignApp: self.fromForeignApp, forcedTheme: self.forcedTheme) - self.controllerNode.completed = completed + self.controllerNode.completed = self.completed self.controllerNode.dismiss = { [weak self] shared in self?.presentingViewController?.dismiss(animated: false, completion: nil) self?.dismissed?(shared) @@ -721,13 +721,18 @@ public final class ShareController: ViewController { }) activities = [shareToInstagram] } - let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: activities) - if let window = strongSelf.view.window, let rootViewController = window.rootViewController { - activityController.popoverPresentationController?.sourceView = window - activityController.popoverPresentationController?.sourceRect = CGRect(origin: CGPoint(x: window.bounds.width / 2.0, y: window.bounds.size.height - 1.0), size: CGSize(width: 1.0, height: 1.0)) - rootViewController.present(activityController, animated: true, completion: nil) - } + let _ = (strongSelf.didAppearPromise.get() + |> filter { $0 } + |> take(1) + |> deliverOnMainQueue).start(next: { [weak self] _ in + let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: activities) + if let strongSelf = self, let window = strongSelf.view.window, let rootViewController = window.rootViewController { + activityController.popoverPresentationController?.sourceView = window + activityController.popoverPresentationController?.sourceRect = CGRect(origin: CGPoint(x: window.bounds.width / 2.0, y: window.bounds.size.height - 1.0), size: CGSize(width: 1.0, height: 1.0)) + rootViewController.present(activityController, animated: true, completion: nil) + } + }) } return .done } @@ -785,12 +790,16 @@ public final class ShareController: ViewController { super.loadView() } + let didAppearPromise = ValuePromise(false, ignoreRepeated: true) override public func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if !self.animatedIn { self.animatedIn = true - self.controllerNode.animateIn() + self.didAppearPromise.set(true) + if !self.immediateExternalShare { + self.controllerNode.animateIn() + } } } diff --git a/submodules/ShareController/Sources/ShareControllerNode.swift b/submodules/ShareController/Sources/ShareControllerNode.swift index 973ce11948..e5846646d1 100644 --- a/submodules/ShareController/Sources/ShareControllerNode.swift +++ b/submodules/ShareController/Sources/ShareControllerNode.swift @@ -21,10 +21,6 @@ enum ShareExternalState { case done } -func openExternalShare(state: () -> Signal) { - -} - final class ShareControllerNode: ViewControllerTracingNode, UIScrollViewDelegate { private let sharedContext: SharedAccountContext private var context: AccountContext? @@ -175,6 +171,8 @@ final class ShareControllerNode: ViewControllerTracingNode, UIScrollViewDelegate } super.init() + + self.isHidden = true self.controllerInteraction = ShareControllerInteraction(togglePeer: { [weak self] peer, search in if let strongSelf = self { @@ -621,6 +619,11 @@ final class ShareControllerNode: ViewControllerTracingNode, UIScrollViewDelegate } func animateIn() { + if let completion = self.outCompletion { + self.outCompletion = nil + completion() + return + } if self.contentNode != nil { self.isHidden = false @@ -635,6 +638,7 @@ final class ShareControllerNode: ViewControllerTracingNode, UIScrollViewDelegate } } + var outCompletion: (() -> Void)? func animateOut(shared: Bool, completion: @escaping () -> Void) { if self.contentNode != nil { var dimCompleted = false @@ -664,7 +668,13 @@ final class ShareControllerNode: ViewControllerTracingNode, UIScrollViewDelegate internalCompletion() }) } else { - completion() + self.outCompletion = completion + Queue.mainQueue().after(0.2) { + if let completion = self.outCompletion { + self.outCompletion = nil + completion() + } + } } } diff --git a/submodules/ShareItems/Sources/ShareItems.swift b/submodules/ShareItems/Sources/ShareItems.swift index 7ddea6d59b..48d7040e80 100644 --- a/submodules/ShareItems/Sources/ShareItems.swift +++ b/submodules/ShareItems/Sources/ShareItems.swift @@ -241,13 +241,14 @@ private func preparedShareItem(account: Account, to peerId: PeerId, value: [Stri let isVoice = ((value["isVoice"] as? NSNumber)?.boolValue ?? false) let title = value["title"] as? String let artist = value["artist"] as? String + let mimeType = value["mimeType"] as? String ?? "audio/ogg" var waveform: MemoryBuffer? if let waveformData = TGItemProviderSignals.audioWaveform(url) { waveform = MemoryBuffer(data: waveformData) } - return standaloneUploadedFile(account: account, peerId: peerId, text: "", source: .data(audioData), mimeType: "audio/ogg", attributes: [.Audio(isVoice: isVoice, duration: Int(duration), title: title, performer: artist, waveform: waveform), .FileName(fileName: fileName)], hintFileIsLarge: audioData.count > 10 * 1024 * 1024) + return standaloneUploadedFile(account: account, peerId: peerId, text: "", source: .data(audioData), mimeType: mimeType, attributes: [.Audio(isVoice: isVoice, duration: Int(duration), title: title, performer: artist, waveform: waveform), .FileName(fileName: fileName)], hintFileIsLarge: audioData.count > 10 * 1024 * 1024) |> mapError { _ -> Void in return Void() } |> mapToSignal { event -> Signal in switch event {