From 47edc24ec625e12498c2c47b3908a3ad0ec775d1 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 31 Aug 2018 12:39:39 -0400 Subject: [PATCH] Add image descriptions --- MastodonKit | 2 +- .../ComposeViewController.swift | 22 ++++++++++++++++--- Tusker/Views/ComposeMediaView.swift | 13 +++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/MastodonKit b/MastodonKit index b3653523..cfece308 160000 --- a/MastodonKit +++ b/MastodonKit @@ -1 +1 @@ -Subproject commit b3653523af6b15fae5e3f8d425304c93d9bef9c7 +Subproject commit cfece3083acfeda2f124a84dc35f268682681d49 diff --git a/Tusker/View Controllers/ComposeViewController.swift b/Tusker/View Controllers/ComposeViewController.swift index ef4b1b7b..fe5da6d1 100644 --- a/Tusker/View Controllers/ComposeViewController.swift +++ b/Tusker/View Controllers/ComposeViewController.swift @@ -98,6 +98,7 @@ class ComposeViewController: UIViewController { func addMedia(for image: UIImage) { let mediaView = ComposeMediaView(image: image) + mediaView.delegate = self mediaStackView.addArrangedSubview(mediaView) } @@ -184,13 +185,13 @@ class ComposeViewController: UIViewController { var attachments: [Attachment?] = [] let group = DispatchGroup() for view in mediaStackView.arrangedSubviews { - guard let imageView = view as? UIImageView, - let image = imageView.image, + guard let mediaView = view as? ComposeMediaView, + let image = mediaView.image, let data = image.pngData() else { continue } let index = attachments.count attachments.append(nil) group.enter() - let req = Media.upload(media: .png(data)) + let req = Media.upload(media: .png(data), description: mediaView.mediaDescription) MastodonController.shared.client.run(req) { result in guard case let .success(attachment, _) = result else { fatalError() } attachments[index] = attachment @@ -243,3 +244,18 @@ extension ComposeViewController: UIImagePickerControllerDelegate, UINavigationCo } } } + +extension ComposeViewController: ComposeMediaViewDelegate { + func editDescription(for media: ComposeMediaView) { + let alertController = UIAlertController(title: "Media Description", message: nil, preferredStyle: .alert) + alertController.addTextField { textField in + textField.text = media.mediaDescription + } + alertController.addAction(UIAlertAction(title: "Confirm", style: .default, handler: { _ in + let description = alertController.textFields![0].text + media.mediaDescription = description + })) + alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) + present(alertController, animated: true) + } +} diff --git a/Tusker/Views/ComposeMediaView.swift b/Tusker/Views/ComposeMediaView.swift index 3f6e96a1..4c6653df 100644 --- a/Tusker/Views/ComposeMediaView.swift +++ b/Tusker/Views/ComposeMediaView.swift @@ -8,10 +8,18 @@ import UIKit +protocol ComposeMediaViewDelegate { + func editDescription(for media: ComposeMediaView) +} + class ComposeMediaView: UIImageView { + var delegate: ComposeMediaViewDelegate? + var remove: UIImageView + var mediaDescription: String? + required init?(coder aDecoder: NSCoder) { return nil } @@ -25,6 +33,7 @@ class ComposeMediaView: UIImageView { layer.cornerRadius = 5 layer.masksToBounds = true isUserInteractionEnabled = true + addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(longPressed))) remove.isUserInteractionEnabled = true remove.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(removePressed))) @@ -41,4 +50,8 @@ class ComposeMediaView: UIImageView { removeFromSuperview() } + @objc func longPressed() { + delegate?.editDescription(for: self) + } + }