Tusker/Tusker/Views/Notifications/FollowNotificationTableView...

108 lines
3.2 KiB
Swift

//
// 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) { (data) in
guard let data = data else { return }
DispatchQueue.main.async {
self.avatarImageView.image = UIImage(data: data)
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)
}
}