// // FollowNotificationTableViewCell.swift // Tusker // // Created by Shadowfacts on 9/2/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import Pachyderm class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { var router: AppRouter! var delegate: StatusTableViewCellDelegate? @IBOutlet weak var followLabel: UILabel! @IBOutlet weak var timestampLabel: UILabel! @IBOutlet weak var avatarImageView: UIImageView! @IBOutlet weak var displayNameLabel: UILabel! @IBOutlet weak var usernameLabel: UILabel! var notification: Pachyderm.Notification! var accountID: String! var avatarURL: URL? var updateTimestampWorkItem: DispatchWorkItem? override func awakeFromNib() { super.awakeFromNib() avatarImageView.layer.masksToBounds = true } func updateUIForPreferences() { let account = MastodonCache.account(for: accountID)! avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) followLabel.text = "Followed by \(account.realDisplayName)" displayNameLabel.text = account.realDisplayName } func updateUI(for notification: Pachyderm.Notification) { self.notification = notification let account = notification.account self.accountID = account.id updateUIForPreferences() usernameLabel.text = "@\(account.acct)" avatarImageView.image = nil avatarURL = account.avatar ImageCache.avatars.get(account.avatar) { (image) in DispatchQueue.main.async { self.avatarImageView.image = image self.avatarURL = nil } } updateTimestamp() } func updateTimestamp() { timestampLabel.text = notification.createdAt.timeAgoString() let delay: DispatchTimeInterval? switch notification.createdAt.timeAgo().1 { case .second: delay = .seconds(10) case .minute: delay = .seconds(60) default: delay = nil } if let delay = delay { updateTimestampWorkItem = DispatchWorkItem { self.updateTimestamp() } DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!) } else { updateTimestampWorkItem = nil } } override func prepareForReuse() { if let url = avatarURL { ImageCache.avatars.cancel(url) } updateTimestampWorkItem?.cancel() updateTimestampWorkItem = nil } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) if selected { delegate?.selected(account: accountID) } } } extension FollowNotificationTableViewCell: PreviewViewControllerProvider { func getPreviewViewController(forLocation location: CGPoint, sourceViewController: UIViewController) -> UIViewController? { return router.profile(for: accountID) } }