// // StatusTableViewCell.swift // Tusker // // Created by Shadowfacts on 8/16/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import MastodonKit protocol StatusTableViewCellDelegate { func selected(account: Account) func selected(mention: Mention) func selected(tag: MastodonKit.Tag) func selected(url: URL) func selected(status: Status) func reply(to status: Status) func showLargeImage(_ image: UIImage, description: String?, animatingFrom originView: UIView) func updatedStatus(for cell: StatusTableViewCell, status: Status) } class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { var delegate: StatusTableViewCellDelegate? @IBOutlet weak var displayNameLabel: UILabel! @IBOutlet weak var usernameLabel: UILabel! @IBOutlet weak var contentLabel: StatusContentLabel! @IBOutlet weak var avatarImageView: UIImageView! @IBOutlet weak var reblogLabel: UILabel! @IBOutlet weak var timestampLabel: UILabel! @IBOutlet weak var attachmentsView: UIView! @IBOutlet weak var favoriteButton: UIButton! @IBOutlet weak var reblogButton: UIButton! var status: Status! var account: Account! var reblogger: Account? var favorited: Bool = false { didSet { favoriteButton.tintColor = favorited ? UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) : tintColor } } var reblogged: Bool = false { didSet { reblogButton.tintColor = reblogged ? UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) : tintColor } } var avatarURL: URL? var updateTimestampWorkItem: DispatchWorkItem? var attachmentDataTasks: [URLSessionDataTask] = [] override func awakeFromNib() { displayNameLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed))) displayNameLabel.isUserInteractionEnabled = true usernameLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed))) usernameLabel.isUserInteractionEnabled = true reblogLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(reblogLabelPressed))) reblogLabel.isUserInteractionEnabled = true avatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed))) avatarImageView.isUserInteractionEnabled = true avatarImageView.layer.masksToBounds = true attachmentsView.layer.cornerRadius = 5 attachmentsView.layer.masksToBounds = true contentLabel.delegate = self } func updateUIForPreferences() { avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) if let reblogger = reblogger { reblogLabel.text = "Reblogged by \(reblogger.realDisplayName)" } displayNameLabel.text = account.realDisplayName } func updateUI(for status: Status) { self.status = status let account: Account if let reblog = status.reblog { account = reblog.account reblogger = status.account reblogLabel.isHidden = false } else { account = status.account reblogLabel.isHidden = true reblogger = nil } self.account = account updateUIForPreferences() usernameLabel.text = "@\(account.acct)" avatarImageView.image = nil if let url = URL(string: account.avatar) { avatarURL = url AvatarCache.shared.get(url) { image in DispatchQueue.main.async { self.avatarImageView.image = image self.avatarURL = nil } } } updateTimestamp() let attachments = status.mediaAttachments.filter({ $0.type == .image }) if attachments.count > 0 { attachmentsView.isHidden = false let width = attachmentsView.bounds.width let height: CGFloat = 200 switch attachments.count { case 1: addAttachmentView(frame: CGRect(x: 0, y: 0, width: width, height: height), attachment: attachments[0]) case 2: addAttachmentView(frame: CGRect(x: 0, y: 0, width: width / 2 - 4, height: height), attachment: attachments[0]) addAttachmentView(frame: CGRect(x: width / 2 + 4, y: 0, width: width / 2 - 4, height: height), attachment: attachments[1]) case 3: addAttachmentView(frame: CGRect(x: 0, y: 0, width: width / 2 - 4, height: height), attachment: attachments[0]) addAttachmentView(frame: CGRect(x: width / 2 + 4, y: 0, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[1]) addAttachmentView(frame: CGRect(x: width / 2 + 4, y: height / 2 + 4, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[2]) case 4: addAttachmentView(frame: CGRect(x: 0, y: 0, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[0]) addAttachmentView(frame: CGRect(x: 0, y: height / 2 + 4, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[1]) addAttachmentView(frame: CGRect(x: width / 2 + 4, y: 0, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[2]) addAttachmentView(frame: CGRect(x: width / 2 + 4, y: height / 2 + 4, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[3]) default: fatalError("Too many attachments") } } else { attachmentsView.isHidden = true } let realStatus = status.reblog ?? status favorited = realStatus.favourited ?? false reblogged = realStatus.reblogged ?? false contentLabel.status = status } func updateTimestamp() { timestampLabel.text = status.createdAt.timeAgoString() let delay: DispatchTimeInterval? switch status.createdAt.timeAgo().1 { case .second: delay = .seconds(10) case .minute: delay = .seconds(60) default: delay = nil } if let delay = delay { updateTimestampWorkItem = DispatchWorkItem { self.updateTimestamp() } DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!) } else { updateTimestampWorkItem = nil } } func addAttachmentView(frame: CGRect, attachment: Attachment) { let attachmentView = AttachmentView(frame: frame, attachment: attachment) attachmentView.delegate = self attachmentsView.addSubview(attachmentView) } override func prepareForReuse() { if let url = avatarURL { AvatarCache.shared.cancel(url) } updateTimestampWorkItem?.cancel() updateTimestampWorkItem = nil attachmentsView.subviews.forEach { view in (view as? AttachmentView)?.task?.cancel() view.removeFromSuperview() } } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) if selected { delegate?.selected(status: status) } } @IBAction func replyPressed(_ sender: Any) { delegate?.reply(to: status) } @objc func accountPressed() { delegate?.selected(account: account) } @objc func reblogLabelPressed() { guard let reblogger = reblogger else { return } delegate?.selected(account: reblogger) } @IBAction func favoritePressed(_ sender: Any) { let oldValue = favorited favorited = !favorited let realStatus: Status = status.reblog ?? status let req = favorited ? Statuses.favourite(id: realStatus.id) : Statuses.unfavourite(id: realStatus.id) MastodonController.shared.client.run(req) { result in guard case .success = result else { print("Couldn't favorite status \(realStatus.id)") // todo: display error message DispatchQueue.main.async { self.favorited = oldValue let generator = UINotificationFeedbackGenerator() generator.notificationOccurred(.error) } return } DispatchQueue.main.async { let generator = UIImpactFeedbackGenerator(style: .light) generator.impactOccurred() } } } @IBAction func reblogPressed(_ sender: Any) { let oldValue = reblogged reblogged = !reblogged let realStatus: Status = status.reblog ?? status let req = reblogged ? Statuses.reblog(id: realStatus.id) : Statuses.unreblog(id: realStatus.id) MastodonController.shared.client.run(req) { result in guard case .success = result else { print("Couldn't reblog status \(realStatus.id)") // todo: display error message DispatchQueue.main.async { self.reblogged = oldValue let generator = UINotificationFeedbackGenerator() generator.notificationOccurred(.error) } return } DispatchQueue.main.async { let generator = UIImpactFeedbackGenerator(style: .light) generator.impactOccurred() } } } } extension StatusTableViewCell: HTMLContentLabelDelegate { func selected(mention: Mention) { delegate?.selected(mention: mention) } func selected(tag: MastodonKit.Tag) { delegate?.selected(tag: tag) } func selected(url: URL) { delegate?.selected(url: url) } } extension StatusTableViewCell: AttachmentViewDelegate { func showLargeAttachment(for attachmentView: AttachmentView) { delegate?.showLargeImage(attachmentView.image!, description: attachmentView.attachment.description, animatingFrom: attachmentView) } }