// // 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: MultiSourceEmojiLabel! var group: NotificationGroup! private var avatarRequests = [String: ImageCache.Request]() private var updateTimestampWorkItem: DispatchWorkItem? private var isGrayscale = false deinit { updateTimestampWorkItem?.cancel() } override func awakeFromNib() { super.awakeFromNib() actionLabel.combiner = self.updateActionLabel NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } @objc func updateUIForPreferences() { for case let imageView as UIImageView in avatarStackView.arrangedSubviews { imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: imageView) } if isGrayscale != Preferences.shared.grayscaleImages { updateGrayscaleableUI() } } func updateUI(group: NotificationGroup) { self.group = group let people = group.notifications.compactMap { mastodonController.persistentContainer.account(for: $0.account.id) } actionLabel.setEmojis(pairs: people.map { ($0.displayOrUserName, $0.emojis) }, identifier: group.id) updateTimestamp() isGrayscale = Preferences.shared.grayscaleImages 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 let avatarURL = account.avatar avatarRequests[account.id] = ImageCache.avatars.get(avatarURL) { [weak self] (_, image) in guard let self = self, let image = image, self.group.id == group.id, let transformedImage = ImageGrayscalifier.convertIfNecessary(url: avatarURL, image: image) else { return } DispatchQueue.main.async { self.avatarRequests.removeValue(forKey: account.id) imageView.image = transformedImage } } avatarStackView.addArrangedSubview(imageView) NSLayoutConstraint.activate([ imageView.widthAnchor.constraint(equalToConstant: 30), imageView.heightAnchor.constraint(equalToConstant: 30), ]) } } private func updateGrayscaleableUI() { isGrayscale = Preferences.shared.grayscaleImages let people = group.notifications.compactMap { mastodonController.persistentContainer.account(for: $0.account.id) } let groupID = group.id for (index, account) in people.enumerated() { guard avatarStackView.arrangedSubviews.count > index, let imageView = avatarStackView.arrangedSubviews[index] as? UIImageView else { continue } let avatarURL = account.avatar avatarRequests[account.id] = ImageCache.avatars.get(avatarURL) { [weak self] (_, image) in guard let self = self else { return } guard let image = image, self.group.id == groupID, let transformedImage = ImageGrayscalifier.convertIfNecessary(url: avatarURL, image: image) else { DispatchQueue.main.async { self.avatarRequests.removeValue(forKey: account.id) } return } DispatchQueue.main.async { self.avatarRequests.removeValue(forKey: account.id) imageView.image = transformedImage } } } } func updateActionLabel(names: [NSAttributedString]) -> NSAttributedString { // todo: figure out how to localize this let str = NSMutableAttributedString(string: "Followed by ") switch names.count { case 1: str.append(names.first!) case 2: str.append(names.first!) str.append(NSAttributedString(string: " and ")) str.append(names.last!) default: for (index, name) in names.enumerated() { str.append(name) if index < names.count - 2 { str.append(NSAttributedString(string: ", ")) } else if index == names.count - 2 { str.append(NSAttributedString(string: ", and ")) } } } return str } 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 [] } }) } } extension FollowNotificationGroupTableViewCell: DraggableTableViewCell { func dragItemsForBeginning(session: UIDragSession) -> [UIDragItem] { guard group.notifications.count == 1 else { return [] } let notification = group.notifications[0] let provider = NSItemProvider(object: notification.account.url as NSURL) let activity = UserActivityManager.showProfileActivity(id: notification.account.id, accountID: mastodonController.accountInfo!.id) provider.registerObject(activity, visibility: .all) return [UIDragItem(itemProvider: provider)] } }