52 lines
1.7 KiB
Swift
52 lines
1.7 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 contentLabel: UILabel!
|
|
@IBOutlet weak var lastModifiedLabel: UILabel!
|
|
@IBOutlet weak var attachmentsStackViewContainer: UIView!
|
|
@IBOutlet weak var attachmentsStackView: UIStackView!
|
|
|
|
func updateUI(for draft: DraftsManager.Draft) {
|
|
contentLabel.text = draft.text
|
|
lastModifiedLabel.text = draft.lastModified.timeAgoString()
|
|
|
|
attachmentsStackViewContainer.isHidden = draft.attachments.count == 0
|
|
|
|
let result = PHAsset.fetchAssets(withLocalIdentifiers: draft.attachments.map { $0.assetIdentifier }, options: nil)
|
|
var i = 0
|
|
while i < result.count {
|
|
let asset = result[i]
|
|
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
|
|
|
|
PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: nil) { (image, _) in
|
|
imageView.image = image
|
|
}
|
|
i += 1
|
|
}
|
|
}
|
|
|
|
override func prepareForReuse() {
|
|
super.prepareForReuse()
|
|
|
|
attachmentsStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
|
|
}
|
|
|
|
}
|