From 2af99eb710b1ee35c14481b764b82493f2260d8d Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Fri, 20 Oct 2017 11:41:41 -0700 Subject: [PATCH] Allow canceling GIF download // FREEBIE --- .../GifPicker/GifPickerViewController.swift | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/Signal/src/ViewControllers/GifPicker/GifPickerViewController.swift b/Signal/src/ViewControllers/GifPicker/GifPickerViewController.swift index 0385276d0..07da5294d 100644 --- a/Signal/src/ViewControllers/GifPicker/GifPickerViewController.swift +++ b/Signal/src/ViewControllers/GifPicker/GifPickerViewController.swift @@ -11,6 +11,8 @@ protocol GifPickerViewControllerDelegate: class { } class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollectionViewDataSource, UICollectionViewDelegate, GifPickerLayoutDelegate { + + static let TAG = "[GifPickerViewController]" let TAG = "[GifPickerViewController]" // MARK: Properties @@ -344,33 +346,43 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect public func getFileForCell(_ cell: GifPickerCell) { GiphyDownloader.sharedInstance.cancelAllRequests() - cell.requestRenditionForSending().then { (asset: GiphyAsset) -> Void in + cell.requestRenditionForSending().then { [weak self] (asset: GiphyAsset) -> Void in + guard let strongSelf = self else { + Logger.info("\(GifPickerViewController.TAG) ignoring send, since VC was dismissed before fetching finished.") + return + } + let filePath = asset.filePath guard let dataSource = DataSourcePath.dataSource(withFilePath: filePath) else { - owsFail("\(self.TAG) couldn't load asset.") + owsFail("\(strongSelf.TAG) couldn't load asset.") return } let attachment = SignalAttachment(dataSource: dataSource, dataUTI: asset.rendition.utiType) - self.delegate?.gifPickerWillSend() + strongSelf.delegate?.gifPickerWillSend() - let outgoingMessage = ThreadUtil.sendMessage(with: attachment, in: self.thread, messageSender: self.messageSender) + let outgoingMessage = ThreadUtil.sendMessage(with: attachment, in: strongSelf.thread, messageSender: strongSelf.messageSender) - self.delegate?.gifPickerDidSend(outgoingMessage: outgoingMessage) + strongSelf.delegate?.gifPickerDidSend(outgoingMessage: outgoingMessage) + + strongSelf.dismiss(animated: true, completion: nil) + }.catch { [weak self] error in + guard let strongSelf = self else { + Logger.info("\(GifPickerViewController.TAG) ignoring failure, since VC was dismissed before fetching finished.") + return + } - self.dismiss(animated: true, completion: nil) - }.catch { error in let alert = UIAlertController(title: NSLocalizedString("GIF_PICKER_FAILURE_ALERT_TITLE", comment: "Shown when selected gif couldn't be fetched"), message: error.localizedDescription, preferredStyle: .alert) alert.addAction(UIAlertAction(title: CommonStrings.retryButton, style: .default) { _ in - self.getFileForCell(cell) + strongSelf.getFileForCell(cell) }) alert.addAction(UIAlertAction(title: CommonStrings.dismissButton, style: .cancel) { _ in - self.dismiss(animated: true, completion: nil) + strongSelf.dismiss(animated: true, completion: nil) }) - UIApplication.shared.frontmostViewController?.present(alert, animated: true, completion: nil) + strongSelf.present(alert, animated: true, completion: nil) }.retainUntilComplete() }