From 7117ce63200c1d488b1aeaf7e160d73690741a37 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 14 Mar 2020 16:21:36 -0400 Subject: [PATCH] Support pasting images to create attachments Closes #91 --- .../ComposeAttachmentsViewController.swift | 38 ++++++++++++++++++- .../Compose/ComposeViewController.swift | 10 +++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/Tusker/Screens/Compose/Attachments/ComposeAttachmentsViewController.swift b/Tusker/Screens/Compose/Attachments/ComposeAttachmentsViewController.swift index a3e8b083..b3ecab0b 100644 --- a/Tusker/Screens/Compose/Attachments/ComposeAttachmentsViewController.swift +++ b/Tusker/Screens/Compose/Attachments/ComposeAttachmentsViewController.swift @@ -57,6 +57,8 @@ class ComposeAttachmentsViewController: UITableViewController { heightConstraint = tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height) heightConstraint.isActive = true + + pasteConfiguration = UIPasteConfiguration(forAccepting: UIImage.self) } override func viewWillAppear(_ animated: Bool) { @@ -65,8 +67,11 @@ class ComposeAttachmentsViewController: UITableViewController { } func setAttachments(_ attachments: [CompositionAttachment]) { - self.attachments = attachments - tableView.reloadData() + tableView.performBatchUpdates({ + tableView.deleteRows(at: self.attachments.indices.map { IndexPath(row: $0, section: 0) }, with: .automatic) + self.attachments = attachments + tableView.insertRows(at: self.attachments.indices.map { IndexPath(row: $0, section: 0) }, with: .automatic) + }) updateHeightConstraint() delegate?.composeRequiresAttachmentDescriptionsDidChange() } @@ -88,6 +93,35 @@ class ComposeAttachmentsViewController: UITableViewController { let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 1)) as! AddAttachmentTableViewCell cell.setEnabled(isAddAttachmentsButtonEnabled()) } + + override func canPaste(_ itemProviders: [NSItemProvider]) -> Bool { + switch mastodonController.instance.instanceType { + case .pleroma: + return true + case .mastodon: + return itemProviders.count + attachments.count <= 4 + } + } + + override func paste(itemProviders: [NSItemProvider]) { + for provider in itemProviders { + provider.loadObject(ofClass: UIImage.self) { (object, error) in + if let error = error { + fatalError("Couldn't load image from NSItemProvider: \(error)") + } + guard let image = object as? UIImage else { + fatalError("Couldn't convert object from NSItemProvider to UIImage") + } + + DispatchQueue.main.async { + let attachment = CompositionAttachment(data: .image(image)) + self.attachments.append(attachment) + self.tableView.insertRows(at: [IndexPath(row: self.attachments.count - 1, section: 0)], with: .automatic) + self.updateHeightConstraint() + } + } + } + } func uploadAll(stepProgress: @escaping () -> Void, completion: @escaping (_ success: Bool, _ uploadedAttachments: [Attachment]) -> Void) { let group = DispatchGroup() diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index 4cad8a08..9aa40dae 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -146,6 +146,8 @@ class ComposeViewController: UIViewController { composeAttachmentsViewController.tableView.isScrollEnabled = false composeAttachmentsViewController.tableView.translatesAutoresizingMaskIntoConstraints = false embedChild(composeAttachmentsViewController, in: composeAttachmentsContainerView) + + pasteConfiguration = composeAttachmentsViewController.pasteConfiguration NotificationCenter.default.addObserver(self, selector: #selector(contentWarningTextFieldDidChange), name: UITextField.textDidChangeNotification, object: contentWarningTextField) } @@ -329,6 +331,14 @@ class ComposeViewController: UIViewController { xcbSession?.complete(with: .cancel) } + override func canPaste(_ itemProviders: [NSItemProvider]) -> Bool { + return composeAttachmentsViewController.canPaste(itemProviders) + } + + override func paste(itemProviders: [NSItemProvider]) { + composeAttachmentsViewController.paste(itemProviders: itemProviders) + } + // MARK: - Interaction @objc func showSaveAndClosePrompt() {