Tusker/Tusker/Views/StatusTableViewCell.swift

116 lines
3.0 KiB
Swift
Raw Normal View History

2018-08-17 02:39:16 +00:00
//
// StatusTableViewCell.swift
// Tusker
//
// Created by Shadowfacts on 8/16/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import MastodonKit
protocol StatusTableViewCellDelegate {
2018-08-28 01:27:34 +00:00
func selected(account: Account)
func selected(mention: Mention)
func selected(tag: MastodonKit.Tag)
func selected(url: URL)
2018-08-28 18:29:06 +00:00
func selected(status: Status)
}
2018-08-28 23:49:31 +00:00
class StatusTableViewCell: UITableViewCell, PreferencesAdaptive {
2018-08-17 02:39:16 +00:00
var delegate: StatusTableViewCellDelegate?
2018-08-17 02:39:16 +00:00
@IBOutlet weak var displayNameLabel: UILabel!
@IBOutlet weak var usernameLabel: UILabel!
2018-08-26 18:49:22 +00:00
@IBOutlet weak var contentLabel: StatusContentLabel!
2018-08-21 23:23:27 +00:00
@IBOutlet weak var avatarImageView: UIImageView!
2018-08-17 02:39:16 +00:00
var status: Status!
2018-08-28 01:27:34 +00:00
var account: Account!
2018-08-21 23:23:27 +00:00
var avatarURL: URL?
2018-08-28 01:27:34 +00:00
override func awakeFromNib() {
displayNameLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
displayNameLabel.isUserInteractionEnabled = true
usernameLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
usernameLabel.isUserInteractionEnabled = true
avatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
avatarImageView.isUserInteractionEnabled = true
avatarImageView.layer.masksToBounds = true
}
2018-08-28 23:49:31 +00:00
func updateUIForPreferences() {
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
}
2018-08-17 02:39:16 +00:00
func updateUI(for status: Status) {
self.status = status
let account: Account
if let reblog = status.reblog {
account = reblog.account
} else {
account = status.account
}
2018-08-28 01:27:34 +00:00
self.account = account
2018-08-28 23:49:31 +00:00
updateUIForPreferences()
displayNameLabel.text = account.displayName
usernameLabel.text = "@\(account.acct)"
2018-08-21 23:23:27 +00:00
avatarImageView.image = nil
if let url = URL(string: account.avatar) {
2018-08-28 01:27:34 +00:00
avatarURL = url
2018-08-21 23:23:27 +00:00
AvatarCache.shared.get(url) { image in
DispatchQueue.main.async {
self.avatarImageView.image = image
2018-08-28 01:27:34 +00:00
self.avatarURL = nil
2018-08-21 23:23:27 +00:00
}
}
}
contentLabel.status = status
contentLabel.delegate = self
}
2018-08-21 23:23:27 +00:00
override func prepareForReuse() {
if let url = avatarURL {
AvatarCache.shared.cancel(url)
}
}
2018-08-17 02:39:16 +00:00
2018-08-28 01:27:34 +00:00
@objc func accountPressed() {
delegate?.selected(account: account)
}
2018-08-28 18:29:06 +00:00
func didSelect() {
delegate?.selected(status: status)
}
2018-08-17 02:39:16 +00:00
}
2018-08-28 01:27:34 +00:00
extension StatusTableViewCell: HTMLContentLabelDelegate {
func selected(mention: Mention) {
delegate?.selected(mention: mention)
}
func selected(tag: MastodonKit.Tag) {
delegate?.selected(tag: tag)
}
func selected(url: URL) {
delegate?.selected(url: url)
}
}