Tusker/Tusker/Views/Instance Cell/InstanceTableViewCell.swift

81 lines
2.4 KiB
Swift

//
// InstanceTableViewCell.swift
// Tusker
//
// Created by Shadowfacts on 9/15/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
class InstanceTableViewCell: UITableViewCell {
@IBOutlet weak var thumbnailImageView: UIImageView!
@IBOutlet weak var domainLabel: UILabel!
@IBOutlet weak var adultLabel: UILabel!
@IBOutlet weak var descriptionTextView: ContentTextView!
var instance: Instance?
var selectorInstance: InstanceSelector.Instance?
var thumbnailURL: URL?
var thumbnailRequest: ImageCache.Request?
override func awakeFromNib() {
super.awakeFromNib()
thumbnailImageView.layer.masksToBounds = true
thumbnailImageView.layer.cornerRadius = 5
adultLabel.layer.masksToBounds = true
adultLabel.layer.cornerRadius = 0.5 * adultLabel.bounds.height
}
func updateUI(instance: InstanceSelector.Instance) {
self.selectorInstance = instance
self.instance = nil
domainLabel.text = instance.domain
adultLabel.isHidden = instance.category != "adult"
descriptionTextView.setTextFromHtml(instance.description)
updateThumbnail(url: instance.proxiedThumbnailURL)
}
func updateUI(instance: Instance) {
self.instance = instance
self.selectorInstance = nil
domainLabel.text = URLComponents(string: instance.uri)?.host ?? instance.uri
adultLabel.isHidden = true
descriptionTextView.setTextFromHtml(instance.description)
if let thumbnail = instance.thumbnail {
updateThumbnail(url: thumbnail)
} else {
thumbnailImageView.image = nil
}
}
private func updateThumbnail(url: URL) {
thumbnailImageView.image = nil
thumbnailURL = url
thumbnailRequest = ImageCache.attachments.get(url) { [weak self] (data) in
guard let self = self, self.thumbnailURL == url, let data = data, let image = UIImage(data: data) else { return }
self.thumbnailRequest = nil
DispatchQueue.main.async {
self.thumbnailImageView.image = image
}
}
}
override func prepareForReuse() {
super.prepareForReuse()
thumbnailRequest?.cancel()
instance = nil
selectorInstance = nil
}
}