Tusker/Tusker/Views/Draft Cell/DraftTableViewCell.swift

65 lines
2.3 KiB
Swift

//
// DraftsTableViewCell.swift
// Tusker
//
// Created by Shadowfacts on 10/22/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import Photos
class DraftTableViewCell: UITableViewCell {
@IBOutlet weak var contentWarningLabel: UILabel!
@IBOutlet weak var contentLabel: UILabel!
@IBOutlet weak var lastModifiedLabel: UILabel!
@IBOutlet weak var attachmentsStackViewContainer: UIView!
@IBOutlet weak var attachmentsStackView: UIStackView!
func updateUI(for draft: Draft) {
contentWarningLabel.text = draft.contentWarning
contentWarningLabel.isHidden = !draft.contentWarningEnabled
contentLabel.text = draft.text
lastModifiedLabel.text = draft.lastModified.timeAgoString()
attachmentsStackViewContainer.isHidden = draft.attachments.count == 0
for attachment in draft.attachments {
let size = CGSize(width: 50, height: 50)
let imageView = UIImageView(frame: CGRect(origin: .zero, size: size))
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = 5
attachmentsStackView.addArrangedSubview(imageView)
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
imageView.backgroundColor = .secondarySystemBackground
imageView.contentMode = .scaleAspectFill
switch attachment.data {
case let .asset(asset):
PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: nil) { (image, _) in
imageView.image = image
}
case let .image(image):
imageView.image = image
case .video(_):
// videos aren't saved to drafts, so this is unreachable
return
case let .drawing(drawing):
imageView.image = drawing.imageInLightMode(from: drawing.bounds)
imageView.backgroundColor = .white
imageView.contentMode = .scaleAspectFit
}
}
}
override func prepareForReuse() {
super.prepareForReuse()
attachmentsStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
}
}