2018-09-03 20:54:03 +00:00
|
|
|
//
|
|
|
|
// ActionNotificationTableViewCell.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/2/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2018-09-11 14:52:21 +00:00
|
|
|
import Pachyderm
|
2018-09-03 20:54:03 +00:00
|
|
|
|
2019-08-03 00:05:47 +00:00
|
|
|
class ActionNotificationTableViewCell: UITableViewCell {
|
2018-09-03 20:54:03 +00:00
|
|
|
|
2018-10-12 01:20:58 +00:00
|
|
|
var delegate: StatusTableViewCellDelegate? {
|
|
|
|
didSet {
|
|
|
|
contentLabel.navigationDelegate = delegate
|
|
|
|
}
|
|
|
|
}
|
2018-09-03 20:54:03 +00:00
|
|
|
|
|
|
|
@IBOutlet weak var displayNameLabel: UILabel!
|
|
|
|
@IBOutlet weak var usernameLabel: UILabel!
|
|
|
|
@IBOutlet weak var contentLabel: StatusContentLabel!
|
2018-10-12 01:20:58 +00:00
|
|
|
@IBOutlet weak var avatarContainerView: UIView!
|
2018-09-03 20:54:03 +00:00
|
|
|
@IBOutlet weak var opAvatarImageView: UIImageView!
|
|
|
|
@IBOutlet weak var actionAvatarImageView: UIImageView!
|
|
|
|
@IBOutlet weak var actionLabel: UILabel!
|
|
|
|
@IBOutlet weak var timestampLabel: UILabel!
|
|
|
|
@IBOutlet weak var attachmentsView: UIStackView!
|
|
|
|
|
2018-09-11 14:52:21 +00:00
|
|
|
var notification: Pachyderm.Notification!
|
2018-09-18 01:57:46 +00:00
|
|
|
var statusID: String!
|
2018-09-03 20:54:03 +00:00
|
|
|
|
|
|
|
var opAvatarURL: URL?
|
|
|
|
var actionAvatarURL: URL?
|
|
|
|
var updateTimestampWorkItem: DispatchWorkItem?
|
|
|
|
|
|
|
|
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
|
|
|
|
opAvatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
|
|
|
|
opAvatarImageView.isUserInteractionEnabled = true
|
|
|
|
opAvatarImageView.layer.masksToBounds = true
|
|
|
|
actionAvatarImageView.layer.masksToBounds = true
|
|
|
|
actionLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(actionPressed)))
|
|
|
|
actionLabel.isUserInteractionEnabled = true
|
2019-08-03 00:05:47 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-03 00:05:47 +00:00
|
|
|
@objc func updateUIForPreferences() {
|
2018-09-18 16:59:07 +00:00
|
|
|
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
2018-09-18 01:57:46 +00:00
|
|
|
|
2018-09-03 20:54:03 +00:00
|
|
|
opAvatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: opAvatarImageView)
|
|
|
|
actionAvatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: actionAvatarImageView)
|
|
|
|
displayNameLabel.text = status.account.realDisplayName
|
|
|
|
|
|
|
|
let verb: String
|
2018-09-11 14:52:21 +00:00
|
|
|
switch notification.kind {
|
2018-09-03 20:54:03 +00:00
|
|
|
case .favourite:
|
|
|
|
verb = "Liked"
|
|
|
|
case .reblog:
|
|
|
|
verb = "Reblogged"
|
|
|
|
default:
|
2018-09-11 14:52:21 +00:00
|
|
|
fatalError("Invalid notification type \(notification.kind) for ActionNotificationTableViewCell")
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
actionLabel.text = "\(verb) by \(notification.account.realDisplayName)"
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:52:21 +00:00
|
|
|
func updateUI(for notification: Pachyderm.Notification) {
|
|
|
|
guard notification.kind == .favourite || notification.kind == .reblog else {
|
|
|
|
fatalError("Invalid notification type \(notification.kind) for ActionNotificationTableViewCell")
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
self.notification = notification
|
2018-09-18 01:57:46 +00:00
|
|
|
let status = notification.status!
|
|
|
|
self.statusID = status.id
|
2018-09-03 20:54:03 +00:00
|
|
|
|
|
|
|
updateUIForPreferences()
|
|
|
|
|
|
|
|
usernameLabel.text = "@\(status.account.acct)"
|
|
|
|
opAvatarImageView.image = nil
|
2018-09-11 14:52:21 +00:00
|
|
|
opAvatarURL = status.account.avatar
|
2018-11-09 20:48:08 +00:00
|
|
|
ImageCache.avatars.get(status.account.avatar) { (data) in
|
|
|
|
guard let data = data else { return }
|
2018-09-11 14:52:21 +00:00
|
|
|
DispatchQueue.main.async {
|
2018-11-09 20:48:08 +00:00
|
|
|
self.opAvatarImageView.image = UIImage(data: data)
|
2018-09-11 14:52:21 +00:00
|
|
|
self.opAvatarURL = nil
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
actionAvatarImageView.image = nil
|
2018-09-11 14:52:21 +00:00
|
|
|
actionAvatarURL = notification.account.avatar
|
2018-11-09 20:48:08 +00:00
|
|
|
ImageCache.avatars.get(notification.account.avatar) { (data) in
|
|
|
|
guard let data = data else { return }
|
2018-09-11 14:52:21 +00:00
|
|
|
DispatchQueue.main.async {
|
2018-11-09 20:48:08 +00:00
|
|
|
self.actionAvatarImageView.image = UIImage(data: data)
|
2018-09-11 14:52:21 +00:00
|
|
|
self.actionAvatarURL = nil
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
updateTimestamp()
|
2018-09-11 14:52:21 +00:00
|
|
|
let attachments = status.attachments.filter({ $0.kind == .image })
|
2018-09-03 20:54:03 +00:00
|
|
|
if attachments.count > 0 {
|
|
|
|
attachmentsView.isHidden = false
|
|
|
|
for attachment in attachments {
|
2018-09-11 14:52:21 +00:00
|
|
|
let url = attachment.textURL ?? attachment.url
|
2018-09-03 20:54:03 +00:00
|
|
|
let label = UILabel()
|
2019-06-04 17:31:05 +00:00
|
|
|
label.textColor = .secondaryLabel
|
2018-09-03 20:54:03 +00:00
|
|
|
|
|
|
|
let textAttachment = InlineTextAttachment()
|
|
|
|
textAttachment.image = UIImage(named: "Link")!
|
|
|
|
textAttachment.bounds = CGRect(x: 0, y: 0, width: label.font.pointSize, height: label.font.pointSize)
|
|
|
|
textAttachment.fontDescender = label.font.descender
|
|
|
|
let attachmentStr = NSAttributedString(attachment: textAttachment)
|
|
|
|
let text = NSMutableAttributedString(string: " ")
|
|
|
|
text.append(attachmentStr)
|
|
|
|
text.append(NSAttributedString(string: " "))
|
|
|
|
|
|
|
|
text.append(NSAttributedString(string: "\(url.lastPathComponent)"))
|
2019-06-04 17:31:05 +00:00
|
|
|
text.addAttribute(.foregroundColor, value: UIColor.systemBlue, range: NSRange(location: 0, length: 2))
|
2018-09-03 20:54:03 +00:00
|
|
|
|
|
|
|
label.attributedText = text
|
|
|
|
attachmentsView.addArrangedSubview(label)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
attachmentsView.isHidden = true
|
|
|
|
}
|
|
|
|
|
2018-10-01 19:14:09 +00:00
|
|
|
contentLabel.setTextFromHtml(status.content)
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateTimestamp() {
|
2018-09-18 16:59:07 +00:00
|
|
|
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
2018-09-18 01:57:46 +00:00
|
|
|
|
2018-09-03 20:54:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
if let url = opAvatarURL {
|
2018-10-02 17:42:37 +00:00
|
|
|
ImageCache.avatars.cancel(url)
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
if let url = actionAvatarURL {
|
2018-10-02 17:42:37 +00:00
|
|
|
ImageCache.avatars.cancel(url)
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
updateTimestampWorkItem?.cancel()
|
|
|
|
updateTimestampWorkItem = nil
|
|
|
|
attachmentsView.subviews.forEach { $0.removeFromSuperview() }
|
|
|
|
}
|
|
|
|
|
|
|
|
override func setSelected(_ selected: Bool, animated: Bool) {
|
|
|
|
super.setSelected(selected, animated: animated)
|
|
|
|
|
|
|
|
if selected {
|
2018-09-18 01:57:46 +00:00
|
|
|
delegate?.selected(status: statusID)
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func accountPressed() {
|
2018-09-18 16:59:07 +00:00
|
|
|
delegate?.selected(account: notification.status!.account.id)
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func actionPressed() {
|
2018-09-18 16:59:07 +00:00
|
|
|
delegate?.selected(account: notification.account.id)
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-04 21:04:37 +00:00
|
|
|
extension ActionNotificationTableViewCell: MenuPreviewProvider {
|
|
|
|
func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? {
|
2018-10-12 01:20:58 +00:00
|
|
|
if avatarContainerView.frame.contains(location) {
|
2019-06-04 21:04:37 +00:00
|
|
|
let accountID = notification.account.id
|
|
|
|
return (content: { ProfileTableViewController(accountID: accountID) }, actions: { self.actionsForProfile(accountID: accountID) })
|
2018-10-12 01:20:58 +00:00
|
|
|
} else if contentLabel.frame.contains(location),
|
2019-06-04 21:04:37 +00:00
|
|
|
let link = contentLabel.getLink(atPoint: contentLabel.convert(location, from: self)) {
|
|
|
|
return (
|
|
|
|
content: { self.contentLabel.getViewController(forLink: link.url, inRange: link.range) },
|
|
|
|
actions: {
|
|
|
|
let text = (self.contentLabel.text! as NSString).substring(with: link.range)
|
|
|
|
if let mention = self.contentLabel.getMention(for: link.url, text: text) {
|
|
|
|
return self.actionsForProfile(accountID: mention.id)
|
|
|
|
} else if let hashtag = self.contentLabel.getHashtag(for: link.url, text: text) {
|
|
|
|
return self.actionsForHashtag(hashtag)
|
|
|
|
} else {
|
|
|
|
return self.actionsForURL(link.url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
2019-06-04 21:04:37 +00:00
|
|
|
return (content: { ConversationTableViewController(for: self.statusID) }, actions: { self.actionsForStatus(statusID: self.statusID) })
|
2018-09-03 20:54:03 +00:00
|
|
|
}
|
|
|
|
}
|