// // ProfileHeaderTableViewCell.swift // Tusker // // Created by Shadowfacts on 8/27/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import Pachyderm protocol ProfileHeaderTableViewCellDelegate: TuskerNavigationDelegate { func showMoreOptions() } class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { var delegate: ProfileHeaderTableViewCellDelegate? @IBOutlet weak var headerImageView: UIImageView! @IBOutlet weak var avatarContainerView: UIView! @IBOutlet weak var avatarImageView: UIImageView! @IBOutlet weak var displayNameLabel: UILabel! @IBOutlet weak var usernameLabel: UILabel! @IBOutlet weak var followsYouLabel: UILabel! @IBOutlet weak var noteLabel: HTMLContentLabel! var accountID: String! var avatarURL: URL? var headerImageDownloadTask: URLSessionDataTask? override func awakeFromNib() { avatarContainerView.layer.masksToBounds = true avatarImageView.layer.masksToBounds = true avatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(avatarPressed))) avatarImageView.isUserInteractionEnabled = true headerImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(headerPressed))) headerImageView.isUserInteractionEnabled = true } func updateUIForPreferences() { guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } avatarContainerView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarContainerView) avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) displayNameLabel.text = account.realDisplayName } func updateUI(for accountID: String) { self.accountID = accountID guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID)") } updateUIForPreferences() usernameLabel.text = "@\(account.acct)" avatarImageView.image = nil avatarURL = account.avatar AvatarCache.shared.get(account.avatar) { image in DispatchQueue.main.async { self.avatarImageView.image = image self.avatarURL = nil } } headerImageDownloadTask = URLSession.shared.dataTask(with: account.header) { data, response, error in guard error == nil, let data = data, let image = UIImage(data: data) else { return } DispatchQueue.main.async { self.headerImageView.image = image self.headerImageDownloadTask = nil } } headerImageDownloadTask!.resume() // todo: HTML parsing noteLabel.text = account.note noteLabel.delegate = self if let relationship = MastodonCache.relationship(for: accountID) { followsYouLabel.isHidden = !relationship.followedBy } else { MastodonCache.relationship(for: accountID) { relationship in DispatchQueue.main.async { self.followsYouLabel.isHidden = !(relationship?.followedBy ?? false) } } } } override func prepareForReuse() { if let url = avatarURL { AvatarCache.shared.cancel(url) } } @IBAction func morePressed(_ sender: Any) { delegate?.showMoreOptions() } @objc func avatarPressed() { delegate?.showLargeImage(avatarImageView.image!, description: nil, animatingFrom: avatarImageView) } @objc func headerPressed() { delegate?.showLargeImage(headerImageView.image!, description: nil, animatingFrom: headerImageView) } } extension ProfileHeaderTableViewCell: HTMLContentLabelDelegate { func selected(mention: Mention) { delegate?.selected(mention: mention) } func selected(tag: Hashtag) { delegate?.selected(tag: tag) } func selected(url: URL) { delegate?.selected(url: url) } }