Tusker/Tusker/Views/Notifications/ActionNotificationTableView...

204 lines
8.2 KiB
Swift
Raw Normal View History

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
class ActionNotificationTableViewCell: UITableViewCell, PreferencesAdaptive {
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
}
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
ImageCache.avatars.get(status.account.avatar) { (image) in
2018-09-11 14:52:21 +00:00
DispatchQueue.main.async {
self.opAvatarImageView.image = image
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
ImageCache.avatars.get(notification.account.avatar) { (image) in
2018-09-11 14:52:21 +00:00
DispatchQueue.main.async {
self.actionAvatarImageView.image = image
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()
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.statusID = status.id
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 {
ImageCache.avatars.cancel(url)
2018-09-03 20:54:03 +00:00
}
if let url = actionAvatarURL {
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
}
}
2018-10-12 01:20:58 +00:00
extension ActionNotificationTableViewCell: PreviewViewControllerProvider {
func getPreviewViewController(forLocation location: CGPoint, sourceViewController: UIViewController) -> UIViewController? {
if avatarContainerView.frame.contains(location) {
return delegate?.router.profile(for: notification.account.id)
2018-10-12 01:20:58 +00:00
} else if contentLabel.frame.contains(location),
let vc = contentLabel.getViewController(forLinkAt: contentLabel.convert(location, from: self)) {
return vc
}
return delegate?.router.conversation(for: statusID)
2018-09-03 20:54:03 +00:00
}
}