2020-03-14 19:47:15 +00:00
|
|
|
//
|
|
|
|
// 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!
|
2020-03-15 00:05:29 +00:00
|
|
|
@IBOutlet weak var removeButton: UIButton!
|
2020-03-14 19:47:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-03-15 00:05:29 +00:00
|
|
|
descriptionTextView.text = attachment.attachmentDescription
|
2020-03-14 19:47:15 +00:00
|
|
|
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
|
|
|
|
}
|
2020-03-15 00:05:29 +00:00
|
|
|
|
|
|
|
func setEnabled(_ enabled: Bool) {
|
|
|
|
descriptionTextView.isEditable = enabled
|
|
|
|
removeButton.isEnabled = enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
assetImageView.image = nil
|
|
|
|
}
|
2020-03-14 19:47:15 +00:00
|
|
|
|
|
|
|
@IBAction func removeButtonPressed(_ sender: Any) {
|
|
|
|
delegate?.removeAttachment(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ComposeAttachmentTableViewCell: UITextViewDelegate {
|
|
|
|
func textViewDidChange(_ textView: UITextView) {
|
2020-03-15 00:05:29 +00:00
|
|
|
attachment.attachmentDescription = textView.text
|
2020-03-14 19:47:15 +00:00
|
|
|
updateDescriptionPlaceholderLabel()
|
2020-03-15 15:39:35 +00:00
|
|
|
delegate?.attachmentDescriptionChanged(self)
|
2020-03-14 19:47:15 +00:00
|
|
|
}
|
|
|
|
}
|