163 lines
5.8 KiB
Swift
163 lines
5.8 KiB
Swift
//
|
|
// FollowNotificationGroupTableViewCell.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 9/5/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Pachyderm
|
|
|
|
class FollowNotificationGroupTableViewCell: UITableViewCell {
|
|
|
|
weak var delegate: TuskerNavigationDelegate?
|
|
var mastodonController: MastodonController! { delegate?.apiController }
|
|
|
|
@IBOutlet weak var avatarStackView: UIStackView!
|
|
@IBOutlet weak var timestampLabel: UILabel!
|
|
@IBOutlet weak var actionLabel: UILabel!
|
|
|
|
var group: NotificationGroup!
|
|
|
|
var avatarRequests = [String: ImageCache.Request]()
|
|
var updateTimestampWorkItem: DispatchWorkItem?
|
|
|
|
deinit {
|
|
updateTimestampWorkItem?.cancel()
|
|
}
|
|
|
|
override func awakeFromNib() {
|
|
super.awakeFromNib()
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
|
|
}
|
|
|
|
@objc func updateUIForPreferences() {
|
|
let people = group.notifications.compactMap { mastodonController.persistentContainer.account(for: $0.account.id) }
|
|
updateActionLabel(people: people)
|
|
|
|
for case let imageView as UIImageView in avatarStackView.arrangedSubviews {
|
|
imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: imageView)
|
|
}
|
|
}
|
|
|
|
func updateUI(group: NotificationGroup) {
|
|
self.group = group
|
|
|
|
let people = group.notifications.compactMap { mastodonController.persistentContainer.account(for: $0.account.id) }
|
|
|
|
updateActionLabel(people: people)
|
|
updateTimestamp()
|
|
|
|
avatarStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
|
|
for account in people {
|
|
let imageView = UIImageView()
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
imageView.layer.masksToBounds = true
|
|
imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadiusFraction * 30
|
|
avatarRequests[account.id] = ImageCache.avatars.get(account.avatar) { [weak self] (data) in
|
|
guard let self = self, let data = data, self.group.id == group.id else { return }
|
|
DispatchQueue.main.async {
|
|
self.avatarRequests.removeValue(forKey: account.id)
|
|
imageView.image = UIImage(data: data)
|
|
}
|
|
}
|
|
avatarStackView.addArrangedSubview(imageView)
|
|
NSLayoutConstraint.activate([
|
|
imageView.widthAnchor.constraint(equalToConstant: 30),
|
|
imageView.heightAnchor.constraint(equalToConstant: 30),
|
|
])
|
|
}
|
|
}
|
|
|
|
func updateActionLabel(people: [AccountMO]) {
|
|
// todo: custom emoji in people display names
|
|
// todo: figure out how to localize this
|
|
let peopleStr: String
|
|
switch people.count {
|
|
case 1:
|
|
peopleStr = people.first!.displayOrUserName
|
|
case 2:
|
|
peopleStr = people.first!.displayOrUserName + " and " + people.last!.displayOrUserName
|
|
default:
|
|
peopleStr = people.dropLast().map { $0.displayOrUserName }.joined(separator: ", ") + ", and " + people.last!.displayOrUserName
|
|
|
|
}
|
|
actionLabel.text = "Followed by \(peopleStr)"
|
|
}
|
|
|
|
func updateTimestamp() {
|
|
guard let notification = group.notifications.first else {
|
|
fatalError("Missing cached notification")
|
|
}
|
|
|
|
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 {
|
|
if updateTimestampWorkItem == nil {
|
|
updateTimestampWorkItem = DispatchWorkItem { [weak self] in
|
|
self?.updateTimestamp()
|
|
}
|
|
}
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
|
|
} else {
|
|
updateTimestampWorkItem = nil
|
|
}
|
|
}
|
|
|
|
override func prepareForReuse() {
|
|
super.prepareForReuse()
|
|
|
|
avatarRequests.values.forEach { $0.cancel() }
|
|
updateTimestampWorkItem?.cancel()
|
|
updateTimestampWorkItem = nil
|
|
}
|
|
|
|
}
|
|
|
|
extension FollowNotificationGroupTableViewCell: SelectableTableViewCell {
|
|
func didSelectCell() {
|
|
let accountIDs = group.notifications.map { $0.account.id }
|
|
switch accountIDs.count {
|
|
case 0:
|
|
return
|
|
case 1:
|
|
delegate?.selected(account: accountIDs.first!)
|
|
default:
|
|
delegate?.showFollowedByList(accountIDs: accountIDs)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension FollowNotificationGroupTableViewCell: MenuPreviewProvider {
|
|
var navigationDelegate: TuskerNavigationDelegate? { return delegate }
|
|
|
|
func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? {
|
|
guard let mastodonController = mastodonController else { return nil }
|
|
let accountIDs = self.group.notifications.map { $0.account.id }
|
|
return (content: {
|
|
if accountIDs.count == 1 {
|
|
return ProfileViewController(accountID: accountIDs.first!, mastodonController: mastodonController)
|
|
} else {
|
|
return AccountListTableViewController(accountIDs: accountIDs, mastodonController: mastodonController)
|
|
}
|
|
}, actions: {
|
|
if accountIDs.count == 1 {
|
|
return self.actionsForProfile(accountID: accountIDs.first!, sourceView: self)
|
|
} else {
|
|
return []
|
|
}
|
|
})
|
|
}
|
|
}
|