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: 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.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()
}
@ -89,6 +94,35 @@ class ComposeAttachmentsViewController: UITableViewController {
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()

View File

@ -147,6 +147,8 @@ class ComposeViewController: UIViewController {
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() {