From 0cbcf0b1696ce512b12767535f777d83054a8c44 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 6 May 2021 11:09:54 +1000 Subject: [PATCH] Implement basic sending logic --- SessionShareExtension/ShareVC.swift | 27 +++---------------- SessionShareExtension/ThreadPickerVC.swift | 31 +++++++++++++++++++--- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/SessionShareExtension/ShareVC.swift b/SessionShareExtension/ShareVC.swift index 63e89d268..ce226e0af 100644 --- a/SessionShareExtension/ShareVC.swift +++ b/SessionShareExtension/ShareVC.swift @@ -4,6 +4,7 @@ import SessionUIKit final class ShareVC : UINavigationController, ShareViewDelegate, AppModeManagerDelegate { private var areVersionMigrationsComplete = false + public static var attachmentPrepPromise: Promise<[SignalAttachment]>? // MARK: Error enum ShareViewControllerError: Error { @@ -201,7 +202,7 @@ final class ShareVC : UINavigationController, ShareViewDelegate, AppModeManagerD private func showMainContent() { let threadPickerVC = ThreadPickerVC() setViewControllers([ threadPickerVC ], animated: false) - buildAttachments().retainUntilComplete() + ShareVC.attachmentPrepPromise = buildAttachments() } func shareViewWasUnlocked() { @@ -546,29 +547,7 @@ final class ShareVC : UINavigationController, ShareViewDelegate, AppModeManagerD guard !SignalAttachment.isInvalidVideo(dataSource: dataSource, dataUTI: specificUTIType) else { // This can happen, e.g. when sharing a quicktime-video from iCloud drive. - - let (promise, exportSession) = SignalAttachment.compressVideoAsMp4(dataSource: dataSource, dataUTI: specificUTIType) - - /* - // TODO: How can we move waiting for this export to the end of the share flow rather than having to do it up front? - // Ideally we'd be able to start it here, and not block the UI on conversion unless there's still work to be done - // when the user hits "send". - if let exportSession = exportSession { - let progressPoller = ProgressPoller(timeInterval: 0.1, ratioCompleteBlock: { return exportSession.progress }) - self.progressPoller = progressPoller - progressPoller.startPolling() - - guard let loadViewController = self.loadViewController else { - owsFailDebug("load view controller was unexpectedly nil") - return promise - } - - DispatchQueue.main.async { - loadViewController.progress = progressPoller.progress - } - } - */ - + let (promise, _) = SignalAttachment.compressVideoAsMp4(dataSource: dataSource, dataUTI: specificUTIType) return promise } diff --git a/SessionShareExtension/ThreadPickerVC.swift b/SessionShareExtension/ThreadPickerVC.swift index 73fdbc035..2f68d1736 100644 --- a/SessionShareExtension/ThreadPickerVC.swift +++ b/SessionShareExtension/ThreadPickerVC.swift @@ -1,8 +1,9 @@ import SessionUIKit -final class ThreadPickerVC : UIViewController, UITableViewDataSource { +final class ThreadPickerVC : UIViewController, UITableViewDataSource, UITableViewDelegate, AttachmentApprovalViewControllerDelegate { private var threads: YapDatabaseViewMappings! private var threadViewModelCache: [String:ThreadViewModel] = [:] // Thread ID to ThreadViewModel + private var selectedThread: TSThread? private var threadCount: UInt { threads.numberOfItems(inGroup: TSInboxGroup) @@ -53,6 +54,7 @@ final class ThreadPickerVC : UIViewController, UITableViewDataSource { navigationItem.titleView = titleLabel // Table view tableView.dataSource = self + tableView.delegate = self view.addSubview(tableView) tableView.pin(to: view) view.addSubview(fadeView) @@ -89,9 +91,32 @@ final class ThreadPickerVC : UIViewController, UITableViewDataSource { // MARK: Interaction func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - guard let thread = self.thread(at: indexPath.row) else { return } - // TODO: Send the attachment + guard let thread = self.thread(at: indexPath.row), let attachments = ShareVC.attachmentPrepPromise?.value else { return } + self.selectedThread = thread tableView.deselectRow(at: indexPath, animated: true) + let approvalVC = AttachmentApprovalViewController.wrappedInNavController(attachments: attachments, approvalDelegate: self) + navigationController!.present(approvalVC, animated: true, completion: nil) + } + + func attachmentApproval(_ attachmentApproval: AttachmentApprovalViewController, didApproveAttachments attachments: [SignalAttachment], messageText: String?) { + let message = VisibleMessage() + message.sentTimestamp = NSDate.millisecondTimestamp() + message.text = messageText + let tsMessage = TSOutgoingMessage.from(message, associatedWith: selectedThread!) + Storage.write { transaction in + tsMessage.save(with: transaction) + } + Storage.write { transaction in + MessageSender.sendNonDurably(message, with: attachments, in: self.selectedThread!, using: transaction).retainUntilComplete() + } + } + + func attachmentApprovalDidCancel(_ attachmentApproval: AttachmentApprovalViewController) { + + } + + func attachmentApproval(_ attachmentApproval: AttachmentApprovalViewController, didChangeMessageText newMessageText: String?) { + } // MARK: Convenience