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

107 lines
3.1 KiB
Swift
Raw Normal View History

2018-09-03 20:54:03 +00:00
//
// FollowNotificationTableViewCell.swift
// Tusker
//
// Created by Shadowfacts on 9/2/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
2018-09-11 14:52:21 +00:00
import Pachyderm
2018-09-03 20:54:03 +00:00
class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive {
var router: AppRouter!
2018-09-03 20:54:03 +00:00
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!
2018-09-11 14:52:21 +00:00
var notification: Pachyderm.Notification!
2018-09-18 16:59:07 +00:00
var accountID: String!
2018-09-03 20:54:03 +00:00
var avatarURL: URL?
var updateTimestampWorkItem: DispatchWorkItem?
override func awakeFromNib() {
super.awakeFromNib()
avatarImageView.layer.masksToBounds = true
}
func updateUIForPreferences() {
2018-09-18 16:59:07 +00:00
let account = MastodonCache.account(for: accountID)!
2018-09-03 20:54:03 +00:00
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
followLabel.text = "Followed by \(account.realDisplayName)"
displayNameLabel.text = account.realDisplayName
}
2018-09-11 14:52:21 +00:00
func updateUI(for notification: Pachyderm.Notification) {
2018-09-03 20:54:03 +00:00
self.notification = notification
2018-09-18 16:59:07 +00:00
let account = notification.account
self.accountID = account.id
2018-09-03 20:54:03 +00:00
updateUIForPreferences()
usernameLabel.text = "@\(account.acct)"
avatarImageView.image = nil
2018-09-11 14:52:21 +00:00
avatarURL = account.avatar
ImageCache.avatars.get(account.avatar) { (image) in
2018-09-11 14:52:21 +00:00
DispatchQueue.main.async {
self.avatarImageView.image = image
self.avatarURL = nil
2018-09-03 20:54:03 +00:00
}
}
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)
2018-09-03 20:54:03 +00:00
}
updateTimestampWorkItem?.cancel()
updateTimestampWorkItem = nil
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
2018-09-18 16:59:07 +00:00
delegate?.selected(account: accountID)
2018-09-03 20:54:03 +00:00
}
}
}
2018-10-12 01:20:58 +00:00
extension FollowNotificationTableViewCell: PreviewViewControllerProvider {
func getPreviewViewController(forLocation location: CGPoint, sourceViewController: UIViewController) -> UIViewController? {
return router.profile(for: accountID)
2018-10-12 01:20:58 +00:00
}
}