Support pasting images to create attachments

Closes #91
This commit is contained in:
Shadowfacts 2020-03-14 16:21:36 -04:00
parent 34dccf1f37
commit 7117ce6320
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 46 additions and 2 deletions

View File

@ -57,6 +57,8 @@ class ComposeAttachmentsViewController: UITableViewController {
heightConstraint = tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height) heightConstraint = tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height)
heightConstraint.isActive = true heightConstraint.isActive = true
pasteConfiguration = UIPasteConfiguration(forAccepting: UIImage.self)
} }
override func viewWillAppear(_ animated: Bool) { override func viewWillAppear(_ animated: Bool) {
@ -65,8 +67,11 @@ class ComposeAttachmentsViewController: UITableViewController {
} }
func setAttachments(_ attachments: [CompositionAttachment]) { func setAttachments(_ attachments: [CompositionAttachment]) {
self.attachments = attachments tableView.performBatchUpdates({
tableView.reloadData() 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() updateHeightConstraint()
delegate?.composeRequiresAttachmentDescriptionsDidChange() delegate?.composeRequiresAttachmentDescriptionsDidChange()
} }
@ -88,6 +93,35 @@ class ComposeAttachmentsViewController: UITableViewController {
let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 1)) as! AddAttachmentTableViewCell let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 1)) as! AddAttachmentTableViewCell
cell.setEnabled(isAddAttachmentsButtonEnabled()) 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) { func uploadAll(stepProgress: @escaping () -> Void, completion: @escaping (_ success: Bool, _ uploadedAttachments: [Attachment]) -> Void) {
let group = DispatchGroup() let group = DispatchGroup()

View File

@ -146,6 +146,8 @@ class ComposeViewController: UIViewController {
composeAttachmentsViewController.tableView.isScrollEnabled = false composeAttachmentsViewController.tableView.isScrollEnabled = false
composeAttachmentsViewController.tableView.translatesAutoresizingMaskIntoConstraints = false composeAttachmentsViewController.tableView.translatesAutoresizingMaskIntoConstraints = false
embedChild(composeAttachmentsViewController, in: composeAttachmentsContainerView) embedChild(composeAttachmentsViewController, in: composeAttachmentsContainerView)
pasteConfiguration = composeAttachmentsViewController.pasteConfiguration
NotificationCenter.default.addObserver(self, selector: #selector(contentWarningTextFieldDidChange), name: UITextField.textDidChangeNotification, object: contentWarningTextField) NotificationCenter.default.addObserver(self, selector: #selector(contentWarningTextFieldDidChange), name: UITextField.textDidChangeNotification, object: contentWarningTextField)
} }
@ -329,6 +331,14 @@ class ComposeViewController: UIViewController {
xcbSession?.complete(with: .cancel) 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 // MARK: - Interaction
@objc func showSaveAndClosePrompt() { @objc func showSaveAndClosePrompt() {