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

71 lines
2.1 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 descriptionLabel: ContentLabel!
var instance: Instance?
var selectorInstance: InstanceSelector.Instance?
var thumbnailURL: URL?
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
descriptionLabel.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
descriptionLabel.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
ImageCache.attachments.get(url) { (data) in
guard self.thumbnailURL == url, let data = data, let image = UIImage(data: data) else { return }
DispatchQueue.main.async {
self.thumbnailImageView.image = image
}
}
}
}