// // 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.notificationIDs.compactMap(mastodonController.cache.notification(for:)).map { $0.account } 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.notificationIDs.compactMap(mastodonController.cache.notification(for:)).map { $0.account } 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: [Account]) { // todo: update to use managed objects // todo: figure out how to localize this let peopleStr: String switch people.count { case 1: peopleStr = people.first!.displayName case 2: peopleStr = people.first!.displayName + " and " + people.last!.displayName default: peopleStr = people.dropLast().map { $0.displayName }.joined(separator: ", ") + ", and " + people.last!.displayName } actionLabel.text = "Followed by \(peopleStr)" } func updateTimestamp() { guard let id = group.notificationIDs.first, let notification = mastodonController.cache.notification(for: id) 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 people = group.notificationIDs.compactMap(mastodonController.cache.notification(for:)).map { $0.account.id } switch people.count { case 0: return case 1: delegate?.selected(account: people.first!) default: delegate?.showFollowedByList(accountIDs: people) } } } 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.notificationIDs.compactMap(mastodonController.cache.notification(for:)).map { $0.account.id } return (content: { if accountIDs.count == 1 { return ProfileTableViewController(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 [] } }) } }