// // ActionNotificationGroupTableViewCell.swift // Tusker // // Created by Shadowfacts on 9/5/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import UIKit import Pachyderm import SwiftSoup import Sentry class ActionNotificationGroupTableViewCell: UITableViewCell { weak var delegate: (TuskerNavigationDelegate & MenuActionProvider)? var mastodonController: MastodonController! { delegate?.apiController } @IBOutlet weak var actionImageView: UIImageView! @IBOutlet weak var verticalStackView: UIStackView! @IBOutlet weak var actionAvatarStackView: UIStackView! @IBOutlet weak var timestampLabel: UILabel! @IBOutlet weak var actionLabel: MultiSourceEmojiLabel! @IBOutlet weak var statusContentLabel: UILabel! var group: NotificationGroup! var statusID: String! private var updateTimestampWorkItem: DispatchWorkItem? private var isGrayscale = false deinit { updateTimestampWorkItem?.cancel() } override func awakeFromNib() { super.awakeFromNib() timestampLabel.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).addingAttributes([ .traits: [ UIFontDescriptor.TraitKey.weight: UIFont.Weight.light.rawValue, ] ]), size: 0) timestampLabel.adjustsFontForContentSizeCategory = true actionLabel.combiner = self.updateActionLabel NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } override func updateConfiguration(using state: UICellConfigurationState) { var config = UIBackgroundConfiguration.listPlainCell().updated(for: state) if state.isHighlighted || state.isSelected { config.backgroundColor = .appSelectedCellBackground } else { config.backgroundColor = .appBackground } backgroundConfiguration = config } @objc func updateUIForPreferences() { for case let imageView as UIImageView in actionAvatarStackView.arrangedSubviews { imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: imageView) } if isGrayscale != Preferences.shared.grayscaleImages { Task { await updateGrayscaleableUI() } } } func updateUI(group: NotificationGroup) { guard group.kind == .favourite || group.kind == .reblog else { fatalError("Invalid notification type \(group.kind) for ActionNotificationGroupTableViewCell") } self.group = group guard let firstNotification = group.notifications.first else { fatalError() } guard let status = firstNotification.status else { let crumb = Breadcrumb(level: .fatal, category: "notifications") crumb.data = [ "id": firstNotification.id, "type": firstNotification.kind.rawValue, "created_at": firstNotification.createdAt.formatted(.iso8601), "account": firstNotification.account.id, ] SentrySDK.addBreadcrumb(crumb) fatalError("missing status for favorite/reblog notification") } self.statusID = status.id updateUIForPreferences() switch group.kind { case .favourite: actionImageView.image = UIImage(systemName: "star.fill") case .reblog: actionImageView.image = UIImage(systemName: "repeat") default: fatalError() } isGrayscale = Preferences.shared.grayscaleImages updateTimestamp() let timestampLabelSize = timestampLabel.sizeThatFits(CGSize(width: .greatestFiniteMagnitude, height: timestampLabel.bounds.height)) let people = group.notifications.compactMap { mastodonController.persistentContainer.account(for: $0.account.id) } actionAvatarStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } var imageViews = [UIImageView]() for _ in people { let imageView = UIImageView() imageView.translatesAutoresizingMaskIntoConstraints = false imageView.layer.masksToBounds = true imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadiusFraction * 30 actionAvatarStackView.addArrangedSubview(imageView) imageViews.append(imageView) // don't add more avatars if they would overflow or squeeze the timestamp label let avatarViewsWidth = 30 * CGFloat(imageViews.count) let avatarMarginsWidth = 4 * CGFloat(max(0, imageViews.count - 1)) // todo: when the cell is first created, verticalStackView.bounds.width is not correct let maxAvatarStackWidth = verticalStackView.bounds.width - timestampLabelSize.width - 8 let remainingWidth = maxAvatarStackWidth - avatarViewsWidth - avatarMarginsWidth if remainingWidth < 34 { break } } NSLayoutConstraint.activate(imageViews.map { $0.widthAnchor.constraint(equalTo: $0.heightAnchor) }) Task { await updateGrayscaleableUI() } actionLabel.setEmojis(pairs: people.map { ($0.displayOrUserName, $0.emojis) }, identifier: group.id) let doc = try! SwiftSoup.parse(status.content) statusContentLabel.text = try! doc.text() } @MainActor private func updateGrayscaleableUI() async { let people = group.notifications.compactMap { mastodonController.persistentContainer.account(for: $0.account.id) } let groupID = group.id for (index, account) in people.enumerated() { guard actionAvatarStackView.arrangedSubviews.count > index, let imageView = actionAvatarStackView.arrangedSubviews[index] as? UIImageView, let avatarURL = account.avatar else { continue } Task { let (_, image) = await ImageCache.avatars.get(avatarURL) guard let image = image, self.group.id == groupID, let transformedImage = ImageGrayscalifier.convertIfNecessary(url: avatarURL, image: image) else { return } imageView.image = transformedImage } } } private 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 } } func updateActionLabel(names: [NSAttributedString]) -> NSAttributedString { let verb: String switch group.kind { case .favourite: verb = "Favorited" case .reblog: verb = "Reblogged" default: fatalError() } // todo: figure out how to localize this let str = NSMutableAttributedString(string: "\(verb) 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 } override func prepareForReuse() { super.prepareForReuse() updateTimestampWorkItem?.cancel() updateTimestampWorkItem = nil } // MARK: Accessibility override var accessibilityLabel: String? { get { let first = group.notifications.first! var str = "" switch group.kind { case .favourite: str += "Favorited by " case .reblog: str += "Reblogged by " default: return nil } str += first.account.displayNameWithoutCustomEmoji if group.notifications.count > 1 { str += " and \(group.notifications.count - 1) more" } str += ", \(first.createdAt.formatted(.relative(presentation: .numeric))), " str += statusContentLabel.text ?? "" return str } set {} } } extension ActionNotificationGroupTableViewCell: SelectableTableViewCell { func didSelectCell() { guard let delegate = delegate else { return } let notifications = group.notifications let accountIDs = notifications.map { $0.account.id } let action: StatusActionAccountListViewController.ActionType switch notifications.first!.kind { case .favourite: action = .favorite case .reblog: action = .reblog default: fatalError() } let vc = StatusActionAccountListViewController(actionType: action, statusID: statusID, statusState: .unknown, accountIDs: accountIDs, mastodonController: mastodonController) vc.showInaccurateCountWarning = false delegate.show(vc) } } extension ActionNotificationGroupTableViewCell: MenuPreviewProvider { func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? { return (content: { let notifications = self.group.notifications let accountIDs = notifications.map { $0.account.id } let action: StatusActionAccountListViewController.ActionType switch notifications.first!.kind { case .favourite: action = .favorite case .reblog: action = .reblog default: fatalError() } let vc = StatusActionAccountListViewController(actionType: action, statusID: self.statusID, statusState: .unknown, accountIDs: accountIDs, mastodonController: self.mastodonController) vc.showInaccurateCountWarning = false return vc }, actions: { return [] }) } }