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

92 lines
2.7 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: InstanceV1?
var selectorInstance: InstanceSelector.Instance?
private var thumbnailTask: Task<Void, Never>?
deinit {
thumbnailTask?.cancel()
}
override func awakeFromNib() {
super.awakeFromNib()
thumbnailImageView.layer.masksToBounds = true
thumbnailImageView.layer.cornerRadius = 5
domainLabel.font = UIFontMetrics(forTextStyle: .title1).scaledFont(for: .systemFont(ofSize: 22, weight: .bold))
domainLabel.adjustsFontForContentSizeCategory = true
adultLabel.layer.masksToBounds = true
adultLabel.layer.cornerRadius = 0.5 * adultLabel.bounds.height
descriptionTextView.adjustsFontForContentSizeCategory = true
}
override func updateConfiguration(using state: UICellConfigurationState) {
backgroundConfiguration = .appListGroupedCell(for: state)
}
func updateUI(instance: InstanceSelector.Instance) {
self.selectorInstance = instance
self.instance = nil
domainLabel.text = instance.domain
adultLabel.isHidden = instance.category != "adult"
descriptionTextView.setBodyTextFromHTML(instance.description)
updateThumbnail(url: instance.proxiedThumbnailURL)
}
func updateUI(instance: InstanceV1) {
self.instance = instance
self.selectorInstance = nil
domainLabel.text = URLComponents(string: instance.uri)?.host ?? instance.uri
adultLabel.isHidden = true
descriptionTextView.setBodyTextFromHTML(instance.shortDescription ?? instance.description)
if let thumbnail = instance.thumbnail {
updateThumbnail(url: thumbnail)
} else {
thumbnailImageView.image = nil
}
}
private func updateThumbnail(url: URL) {
thumbnailImageView.image = nil
thumbnailTask = Task {
guard let image = await ImageCache.attachments.get(url).1,
!Task.isCancelled else {
return
}
thumbnailImageView.image = image
}
}
override func prepareForReuse() {
super.prepareForReuse()
thumbnailTask?.cancel()
instance = nil
selectorInstance = nil
}
}