Tusker/Tusker/Views/AttachmentView.swift

74 lines
1.8 KiB
Swift
Raw Normal View History

2018-09-02 20:59:20 +00:00
//
// AttachmentView.swift
// Tusker
//
// Created by Shadowfacts on 8/31/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
2018-09-11 14:52:21 +00:00
import Pachyderm
2018-11-09 20:48:08 +00:00
import Gifu
2018-09-02 20:59:20 +00:00
protocol AttachmentViewDelegate {
func showLargeAttachment(for attachmentView: AttachmentView)
}
2018-11-09 20:48:08 +00:00
class AttachmentView: UIImageView, GIFAnimatable {
2018-09-02 20:59:20 +00:00
var delegate: AttachmentViewDelegate?
var attachment: Attachment!
2018-11-09 20:48:08 +00:00
var gifData: Data?
public lazy var animator: Animator? = Animator(withDelegate: self)
2018-09-02 20:59:20 +00:00
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
convenience init(frame: CGRect, attachment: Attachment) {
self.init(frame: frame)
self.attachment = attachment
loadImage()
}
func commonInit() {
contentMode = .scaleAspectFill
layer.masksToBounds = true
isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imagePressed)))
}
func loadImage() {
2018-11-09 20:48:08 +00:00
ImageCache.attachments.get(attachment.url) { (data) in
guard let data = data else { return }
2018-09-02 20:59:20 +00:00
DispatchQueue.main.async {
2018-11-09 20:48:08 +00:00
if self.attachment.url.pathExtension == "gif" {
self.animate(withGIFData: data)
self.gifData = data
} else {
self.image = UIImage(data: data)
}
2018-09-02 20:59:20 +00:00
}
}
}
2018-11-09 20:48:08 +00:00
override func display(_ layer: CALayer) {
updateImageIfNeeded()
}
2018-09-02 20:59:20 +00:00
@objc func imagePressed() {
if image != nil {
delegate?.showLargeAttachment(for: self)
}
}
}