202 lines
7.6 KiB
Swift
202 lines
7.6 KiB
Swift
|
//
|
||
|
// ActionNotificationTableViewCell.swift
|
||
|
// Tusker
|
||
|
//
|
||
|
// Created by Shadowfacts on 9/2/18.
|
||
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import UIKit
|
||
|
import MastodonKit
|
||
|
|
||
|
class ActionNotificationTableViewCell: UITableViewCell, PreferencesAdaptive {
|
||
|
|
||
|
var delegate: StatusTableViewCellDelegate?
|
||
|
|
||
|
@IBOutlet weak var displayNameLabel: UILabel!
|
||
|
@IBOutlet weak var usernameLabel: UILabel!
|
||
|
@IBOutlet weak var contentLabel: StatusContentLabel!
|
||
|
@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!
|
||
|
|
||
|
var notification: MastodonKit.Notification!
|
||
|
var status: Status!
|
||
|
|
||
|
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
|
||
|
contentLabel.delegate = self
|
||
|
}
|
||
|
|
||
|
func updateUIForPreferences() {
|
||
|
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
|
||
|
switch notification.type {
|
||
|
case .favourite:
|
||
|
verb = "Liked"
|
||
|
case .reblog:
|
||
|
verb = "Reblogged"
|
||
|
default:
|
||
|
fatalError("Invalid notification type \(notification.type) for ActionNotificationTableViewCell")
|
||
|
}
|
||
|
actionLabel.text = "\(verb) by \(notification.account.realDisplayName)"
|
||
|
}
|
||
|
|
||
|
func updateUI(for notification: MastodonKit.Notification) {
|
||
|
guard notification.type == .favourite || notification.type == .reblog else {
|
||
|
fatalError("Invalid notification type \(notification.type) for ActionNotificationTableViewCell")
|
||
|
}
|
||
|
self.notification = notification
|
||
|
self.status = notification.status!
|
||
|
|
||
|
updateUIForPreferences()
|
||
|
|
||
|
usernameLabel.text = "@\(status.account.acct)"
|
||
|
opAvatarImageView.image = nil
|
||
|
if let url = URL(string: status.account.avatar) {
|
||
|
opAvatarURL = url
|
||
|
AvatarCache.shared.get(url) { image in
|
||
|
DispatchQueue.main.async {
|
||
|
self.opAvatarImageView.image = image
|
||
|
self.opAvatarURL = nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
actionAvatarImageView.image = nil
|
||
|
if let url = URL(string: notification.account.avatar) {
|
||
|
actionAvatarURL = url
|
||
|
AvatarCache.shared.get(url) { image in
|
||
|
DispatchQueue.main.async {
|
||
|
self.actionAvatarImageView.image = image
|
||
|
self.actionAvatarURL = nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
updateTimestamp()
|
||
|
let attachments = status.mediaAttachments.filter({ $0.type == .image })
|
||
|
if attachments.count > 0 {
|
||
|
attachmentsView.isHidden = false
|
||
|
for attachment in attachments {
|
||
|
guard let url = URL(string: attachment.textURL ?? attachment.url) else { continue }
|
||
|
let label = UILabel()
|
||
|
label.textColor = .darkGray
|
||
|
|
||
|
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.addAttribute(.font, value: UIFont.systemFont(ofSize: 0), range: NSRange(location: 0, length: text.length))
|
||
|
|
||
|
text.append(NSAttributedString(string: "\(url.lastPathComponent)"))
|
||
|
text.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange(location: 0, length: 2))
|
||
|
|
||
|
|
||
|
|
||
|
// let text = NSMutableAttributedString(string: " \(url.lastPathComponent)")
|
||
|
// let imageAttachment = InlineTextAttachment()
|
||
|
// imageAttachment.image = UIImage(named: "Link")!
|
||
|
// imageAttachment.bounds = CGRect(x: 0, y: 0, width: label.font.pointSize, height: label.font.pointSize)
|
||
|
// imageAttachment.fontDescender = label.font.descender
|
||
|
// let imageStr = NSMutableAttributedString(attachment: imageAttachment)
|
||
|
// imageStr.setAttributes([.foregroundColor: UIColor.darkGray], range: )
|
||
|
// text.insert(imageStr, at: 0)
|
||
|
|
||
|
label.attributedText = text
|
||
|
attachmentsView.addArrangedSubview(label)
|
||
|
}
|
||
|
} else {
|
||
|
attachmentsView.isHidden = true
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
|
||
|
override func prepareForReuse() {
|
||
|
if let url = opAvatarURL {
|
||
|
AvatarCache.shared.cancel(url)
|
||
|
}
|
||
|
if let url = actionAvatarURL {
|
||
|
AvatarCache.shared.cancel(url)
|
||
|
}
|
||
|
updateTimestampWorkItem?.cancel()
|
||
|
updateTimestampWorkItem = nil
|
||
|
attachmentsView.subviews.forEach { $0.removeFromSuperview() }
|
||
|
}
|
||
|
|
||
|
override func setSelected(_ selected: Bool, animated: Bool) {
|
||
|
super.setSelected(selected, animated: animated)
|
||
|
|
||
|
if selected {
|
||
|
delegate?.selected(status: status)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@objc func accountPressed() {
|
||
|
delegate?.selected(account: status.account)
|
||
|
}
|
||
|
|
||
|
@objc func actionPressed() {
|
||
|
delegate?.selected(account: notification.account)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
extension ActionNotificationTableViewCell: HTMLContentLabelDelegate {
|
||
|
|
||
|
func selected(mention: Mention) {
|
||
|
delegate?.selected(mention: mention)
|
||
|
}
|
||
|
|
||
|
func selected(tag: Tag) {
|
||
|
delegate?.selected(tag: tag)
|
||
|
}
|
||
|
|
||
|
func selected(url: URL) {
|
||
|
delegate?.selected(url: url)
|
||
|
}
|
||
|
|
||
|
}
|