// // ComposeAttachmentTableViewCell.swift // Tusker // // Created by Shadowfacts on 3/13/20. // Copyright © 2020 Shadowfacts. All rights reserved. // import UIKit //import Combine import Photos import AVFoundation protocol ComposeAttachmentTableViewCellDelegate: class { func removeAttachment(_ cell: ComposeAttachmentTableViewCell) func attachmentDescriptionChanged(_ cell: ComposeAttachmentTableViewCell) } class ComposeAttachmentTableViewCell: UITableViewCell { weak var delegate: ComposeAttachmentTableViewCellDelegate? @IBOutlet weak var assetImageView: UIImageView! @IBOutlet weak var descriptionTextView: UITextView! @IBOutlet weak var descriptionPlaceholderLabel: UILabel! var attachment: CompositionAttachment! override func awakeFromNib() { super.awakeFromNib() assetImageView.layer.masksToBounds = true assetImageView.layer.cornerRadius = 8 descriptionTextView.delegate = self } func updateUI(for attachment: CompositionAttachment) { self.attachment = attachment descriptionTextView.text = attachment.description updateDescriptionPlaceholderLabel() switch attachment.data { case let .image(image): assetImageView.image = image case let .asset(asset): let size = CGSize(width: 80, height: 80) PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: nil) { (image, _) in guard self.attachment == attachment else { return } self.assetImageView.image = image } case let .video(url): let asset = AVURLAsset(url: url) let imageGenerator = AVAssetImageGenerator(asset: asset) if let cgImage = try? imageGenerator.copyCGImage(at: .zero, actualTime: nil) { assetImageView.image = UIImage(cgImage: cgImage) } } } func updateDescriptionPlaceholderLabel() { descriptionPlaceholderLabel.isHidden = !descriptionTextView.text.isEmpty } @IBAction func removeButtonPressed(_ sender: Any) { delegate?.removeAttachment(self) } } extension ComposeAttachmentTableViewCell: UITextViewDelegate { func textViewDidChange(_ textView: UITextView) { delegate?.attachmentDescriptionChanged(self) attachment.description = textView.text updateDescriptionPlaceholderLabel() } }