Tusker/Tusker/Views/Account Cell/AccountTableViewCell.swift

74 lines
2.2 KiB
Swift

//
// AccountTableViewCell.swift
// Tusker
//
// Created by Shadowfacts on 9/5/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import UIKit
class AccountTableViewCell: UITableViewCell {
var delegate: TuskerNavigationDelegate?
@IBOutlet weak var avatarImageView: UIImageView!
@IBOutlet weak var displayNameLabel: UILabel!
@IBOutlet weak var usernameLabel: UILabel!
var accountID: String!
var avatarURL: URL?
override func awakeFromNib() {
super.awakeFromNib()
avatarImageView.layer.masksToBounds = true
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPrefrences), name: .preferencesChanged, object: nil)
}
@objc func updateUIForPrefrences() {
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
guard let account = MastodonCache.account(for: accountID) else {
fatalError("Missing cached account \(accountID!)")
}
displayNameLabel.text = account.realDisplayName
}
func updateUI(accountID: String) {
self.accountID = accountID
guard let account = MastodonCache.account(for: accountID) else {
fatalError("Missing cached account \(accountID)")
}
self.avatarURL = account.avatar
ImageCache.avatars.get(account.avatar) { (data) in
guard let data = data, self.avatarURL == account.avatar else { return }
DispatchQueue.main.async {
self.avatarImageView.image = UIImage(data: data)
}
}
usernameLabel.text = "@\(account.acct)"
updateUIForPrefrences()
}
}
extension AccountTableViewCell: SelectableTableViewCell {
func didSelectCell() {
delegate?.selected(account: accountID)
}
}
extension AccountTableViewCell: MenuPreviewProvider {
func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? {
return (content: { ProfileTableViewController(accountID: self.accountID) }, actions: { self.actionsForProfile(accountID: self.accountID) })
}
}