Tusker/Tusker/Views/Asset Picker/AssetCollectionViewCell.swift

75 lines
2.2 KiB
Swift

//
// AssetCollectionViewCell.swift
// Tusker
//
// Created by Shadowfacts on 1/1/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import UIKit
import Photos
class AssetCollectionViewCell: UICollectionViewCell {
static let durationFormatter: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = [.minute, .second]
formatter.zeroFormattingBehavior = .pad
return formatter
}()
var assetIdentifier: String!
var thumbnailImage: UIImage? {
get {
imageView.image
}
set {
imageView.image = newValue
}
}
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var checkmarkVisualEffectView: UIVisualEffectView!
@IBOutlet weak var livePhotoVisualEffectView: UIVisualEffectView!
@IBOutlet weak var durationVisualEffectView: UIVisualEffectView!
@IBOutlet weak var durationLabel: UILabel!
override var isSelected: Bool {
didSet {
checkmarkVisualEffectView.isHidden = !isSelected
}
}
override func awakeFromNib() {
super.awakeFromNib()
checkmarkVisualEffectView.layer.masksToBounds = true
checkmarkVisualEffectView.layer.cornerRadius = 12
checkmarkVisualEffectView.isHidden = true
livePhotoVisualEffectView.layer.masksToBounds = true
livePhotoVisualEffectView.layer.cornerRadius = 12
durationVisualEffectView.layer.masksToBounds = true
durationVisualEffectView.layer.cornerRadius = durationVisualEffectView.bounds.width * 0.1
}
func updateUI(asset: PHAsset) {
assetIdentifier = asset.localIdentifier
durationVisualEffectView.isHidden = asset.mediaType != .video
if asset.mediaType == .video {
durationLabel.text = AssetCollectionViewCell.durationFormatter.string(from: asset.duration)
}
livePhotoVisualEffectView.isHidden = !(asset.mediaType == .image && asset.mediaSubtypes.contains(.photoLive))
}
override func prepareForReuse() {
thumbnailImage = nil
isSelected = false
}
}