Tusker/Tusker/Views/Attachments/AttachmentView.swift

144 lines
4.4 KiB
Swift

//
// AttachmentView.swift
// Tusker
//
// Created by Shadowfacts on 8/31/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
import Gifu
import AVFoundation
protocol AttachmentViewDelegate: class {
func showAttachmentsGallery(startingAt index: Int)
}
class AttachmentView: UIImageView, GIFAnimatable {
weak var delegate: AttachmentViewDelegate?
var playImageView: UIImageView?
var attachment: Attachment!
var index: Int!
private var attachmentRequest: ImageCache.Request?
var gifData: Data?
public lazy var animator: Animator? = Animator(withDelegate: self)
init(attachment: Attachment, index: Int) {
super.init(image: nil)
commonInit()
self.attachment = attachment
self.index = index
loadAttachment()
}
deinit {
attachmentRequest?.cancel()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
contentMode = .scaleAspectFill
layer.masksToBounds = true
isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imagePressed)))
}
func loadAttachment() {
guard AttachmentsContainerView.supportedAttachmentTypes.contains(attachment.kind) else {
preconditionFailure("invalid attachment type")
}
switch attachment.kind {
case .image:
loadImage()
case .video:
loadVideo()
case .audio:
loadAudio()
default:
preconditionFailure("invalid attachment type")
}
}
func loadImage() {
attachmentRequest = ImageCache.attachments.get(attachment.url) { [weak self] (data) in
guard let self = self, let data = data else { return }
self.attachmentRequest = nil
DispatchQueue.main.async {
if self.attachment.url.pathExtension == "gif" {
self.animate(withGIFData: data)
self.gifData = data
} else {
self.image = UIImage(data: data)
}
}
}
}
func loadVideo() {
let attachmentURL = self.attachment.url
DispatchQueue.global(qos: .userInitiated).async {
let asset = AVURLAsset(url: attachmentURL)
let generator = AVAssetImageGenerator(asset: asset)
generator.appliesPreferredTrackTransform = true
guard let image = try? generator.copyCGImage(at: CMTime(seconds: 0, preferredTimescale: 1), actualTime: nil) else { return }
DispatchQueue.main.async {
guard self.attachment.url == attachmentURL else { return }
self.image = UIImage(cgImage: image)
}
}
let playImageView = UIImageView(image: UIImage(systemName: "play.circle.fill"))
playImageView.translatesAutoresizingMaskIntoConstraints = false
addSubview(playImageView)
NSLayoutConstraint.activate([
playImageView.widthAnchor.constraint(equalToConstant: 50),
playImageView.heightAnchor.constraint(equalToConstant: 50),
playImageView.centerXAnchor.constraint(equalTo: centerXAnchor),
playImageView.centerYAnchor.constraint(equalTo: centerYAnchor),
])
}
func loadAudio() {
let label = UILabel()
label.text = "Audio Only"
let playImageView = UIImageView(image: UIImage(systemName: "play.circle.fill"))
let stack = UIStackView(arrangedSubviews: [
label,
playImageView
])
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .vertical
stack.spacing = 8
stack.alignment = .center
addSubview(stack)
NSLayoutConstraint.activate([
stack.centerXAnchor.constraint(equalTo: centerXAnchor),
stack.centerYAnchor.constraint(equalTo: centerYAnchor),
playImageView.widthAnchor.constraint(equalToConstant: 50),
playImageView.heightAnchor.constraint(equalToConstant: 50),
])
}
override func display(_ layer: CALayer) {
updateImageIfNeeded()
}
@objc func imagePressed() {
delegate?.showAttachmentsGallery(startingAt: index)
}
}