// // ActionNotificationGroupTableViewCell.swift // Tusker // // Created by Shadowfacts on 9/5/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import UIKit import Pachyderm import SwiftSoup class ActionNotificationGroupTableViewCell: UITableViewCell { var delegate: TuskerNavigationDelegate? @IBOutlet weak var actionImageView: UIImageView! @IBOutlet weak var actionAvatarStackView: UIStackView! @IBOutlet weak var timestampLabel: UILabel! @IBOutlet weak var actionLabel: UILabel! @IBOutlet weak var statusContentLabel: UILabel! var group: NotificationGroup! var statusID: String! var authorAvatarURL: URL? var updateTimestampWorkItem: DispatchWorkItem? override func awakeFromNib() { super.awakeFromNib() NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } @objc func updateUIForPreferences() { let people = group.notificationIDs.compactMap(MastodonCache.notification(for:)).map { $0.account } updateActionLabel(people: people) for case let imageView as UIImageView in actionAvatarStackView.arrangedSubviews { imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: imageView) } } 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 = MastodonCache.notification(for: group.notificationIDs.first!) else { fatalError() } let status = firstNotification.status! 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() } let people = group.notificationIDs.compactMap(MastodonCache.notification(for:)).map { $0.account } actionAvatarStackView.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 ImageCache.avatars.get(account.avatar) { (data) in guard let data = data, self.group.id == group.id else { return } DispatchQueue.main.async { imageView.image = UIImage(data: data) } } actionAvatarStackView.addArrangedSubview(imageView) NSLayoutConstraint.activate([ imageView.widthAnchor.constraint(equalToConstant: 30), imageView.heightAnchor.constraint(equalToConstant: 30) ]) } updateTimestamp() updateActionLabel(people: people) let doc = try! SwiftSoup.parse(status.content) statusContentLabel.text = try! doc.text() } func updateTimestamp() { guard let id = group.notificationIDs.first, let notification = MastodonCache.notification(for: id) else { fatalError("Missing cached status") } 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(block: self.updateTimestamp) DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!) } else { updateTimestampWorkItem = nil } } func updateActionLabel(people: [Account]) { let verb: String switch group.kind { case .favourite: verb = "Favorited" case .reblog: verb = "Reblogged" default: fatalError() } let peopleStr: String // todo: figure out how to localize this switch people.count { case 1: peopleStr = people.first!.realDisplayName case 2: peopleStr = people.first!.realDisplayName + " and " + people.last!.realDisplayName default: peopleStr = people.dropLast().map { $0.realDisplayName }.joined(separator: ", ") + ", and " + people.last!.realDisplayName } actionLabel.text = "\(verb) by \(peopleStr)" } }