forked from shadowfacts/Tusker
262 lines
10 KiB
Swift
262 lines
10 KiB
Swift
//
|
|
// ConversationMainStatusTableViewCell.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 8/28/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Combine
|
|
import Pachyderm
|
|
|
|
class ConversationMainStatusTableViewCell: UITableViewCell {
|
|
|
|
var delegate: StatusTableViewCellDelegate? {
|
|
didSet {
|
|
contentLabel.navigationDelegate = delegate
|
|
}
|
|
}
|
|
|
|
@IBOutlet weak var displayNameLabel: UILabel!
|
|
@IBOutlet weak var usernameLabel: UILabel!
|
|
@IBOutlet weak var contentLabel: StatusContentLabel!
|
|
@IBOutlet weak var avatarImageView: UIImageView!
|
|
@IBOutlet weak var timestampLabel: UILabel!
|
|
@IBOutlet weak var attachmentsView: AttachmentsContainerView!
|
|
@IBOutlet weak var favoriteButton: UIButton!
|
|
@IBOutlet weak var reblogButton: UIButton!
|
|
|
|
var statusID: String!
|
|
var accountID: String!
|
|
|
|
var favorited: Bool = false {
|
|
didSet {
|
|
DispatchQueue.main.async {
|
|
self.favoriteButton.tintColor = self.favorited ? UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) : self.tintColor
|
|
}
|
|
}
|
|
}
|
|
var reblogged: Bool = false {
|
|
didSet {
|
|
DispatchQueue.main.async {
|
|
self.reblogButton.tintColor = self.reblogged ? UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) : self.tintColor
|
|
}
|
|
}
|
|
}
|
|
|
|
var avatarURL: URL?
|
|
var updateTimestampWorkItem: DispatchWorkItem?
|
|
|
|
var statusUpdater: Cancellable?
|
|
var accountUpdater: Cancellable?
|
|
|
|
deinit {
|
|
statusUpdater?.cancel()
|
|
accountUpdater?.cancel()
|
|
}
|
|
|
|
override func awakeFromNib() {
|
|
displayNameLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
|
|
usernameLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
|
|
avatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
|
|
avatarImageView.layer.masksToBounds = true
|
|
attachmentsView.delegate = self
|
|
attachmentsView.layer.cornerRadius = 5
|
|
attachmentsView.layer.masksToBounds = true
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
|
|
|
|
statusUpdater = MastodonCache.statusSubject
|
|
.filter { $0.id == self.statusID }
|
|
.receive(on: DispatchQueue.main)
|
|
.sink(receiveValue: updateStatusState(status:))
|
|
|
|
accountUpdater = MastodonCache.accountSubject
|
|
.filter { $0.id == self.accountID }
|
|
.receive(on: DispatchQueue.main)
|
|
.sink(receiveValue: updateUI(account:))
|
|
}
|
|
|
|
func updateUI(statusID: String) {
|
|
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") }
|
|
|
|
self.statusID = status.id
|
|
|
|
let account: Account
|
|
if let reblog = status.reblog {
|
|
account = reblog.account
|
|
} else {
|
|
account = status.account
|
|
}
|
|
self.accountID = account.id
|
|
updateUI(account: account)
|
|
updateUIForPreferences()
|
|
|
|
updateTimestamp()
|
|
|
|
attachmentsView.updateUI(status: status)
|
|
|
|
let realStatus = status.reblog ?? status
|
|
updateStatusState(status: realStatus)
|
|
|
|
contentLabel.statusID = statusID
|
|
}
|
|
|
|
private func updateStatusState(status: Status) {
|
|
favorited = status.favourited ?? false
|
|
reblogged = status.reblogged ?? false
|
|
}
|
|
|
|
private func updateUI(account: Account) {
|
|
usernameLabel.text = "@\(account.acct)"
|
|
avatarImageView.image = nil
|
|
avatarURL = account.avatar
|
|
ImageCache.avatars.get(account.avatar) { (data) in
|
|
guard let data = data else { return }
|
|
DispatchQueue.main.async {
|
|
self.avatarImageView.image = UIImage(data: data)
|
|
self.avatarURL = nil
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc func updateUIForPreferences() {
|
|
guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }
|
|
|
|
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
|
|
displayNameLabel.text = account.realDisplayName
|
|
}
|
|
|
|
func updateTimestamp() {
|
|
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
|
|
|
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 = avatarURL {
|
|
ImageCache.avatars.cancel(url)
|
|
}
|
|
updateTimestampWorkItem?.cancel()
|
|
updateTimestampWorkItem = nil
|
|
attachmentsView.subviews.forEach { $0.removeFromSuperview() }
|
|
}
|
|
|
|
@objc func accountPressed() {
|
|
delegate?.selected(account: accountID)
|
|
}
|
|
|
|
@IBAction func replyPressed(_ sender: Any) {
|
|
delegate?.reply(to: statusID)
|
|
}
|
|
|
|
@IBAction func favoritePressed(_ sender: Any) {
|
|
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
|
|
|
favorited = !favorited
|
|
|
|
let realStatus: Status = status.reblog ?? status
|
|
|
|
let request = (favorited ? Status.favourite : Status.unfavourite)(realStatus)
|
|
MastodonController.client.run(request) { response in
|
|
DispatchQueue.main.async {
|
|
if case let .success(newStatus, _) = response {
|
|
self.favorited = newStatus.favourited ?? false
|
|
MastodonCache.add(status: newStatus)
|
|
UIImpactFeedbackGenerator(style: .light).impactOccurred()
|
|
} else {
|
|
print("Couldn't favorite status \(realStatus.id)")
|
|
// todo: display error message
|
|
UINotificationFeedbackGenerator().notificationOccurred(.error)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction func reblogPressed(_ sender: Any) {
|
|
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
|
|
|
reblogged = !reblogged
|
|
|
|
let realStatus: Status = status.reblog ?? status
|
|
|
|
let request = (reblogged ? Status.reblog : Status.unreblog)(realStatus)
|
|
MastodonController.client.run(request) { response in
|
|
DispatchQueue.main.async {
|
|
if case let .success(newStatus, _) = response {
|
|
self.reblogged = newStatus.reblogged ?? false
|
|
MastodonCache.add(status: newStatus)
|
|
UIImpactFeedbackGenerator(style: .light).impactOccurred()
|
|
} else {
|
|
print("Couldn't reblog status \(realStatus.id)")
|
|
// todo: display error message
|
|
UINotificationFeedbackGenerator().notificationOccurred(.error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction func morePressed(_ sender: Any) {
|
|
delegate?.showMoreOptions(forStatus: statusID)
|
|
}
|
|
|
|
}
|
|
|
|
extension ConversationMainStatusTableViewCell: AttachmentViewDelegate {
|
|
func showAttachmentsGallery(startingAt index: Int) {
|
|
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
|
let sourceViews = status.attachments.map(attachmentsView.getAttachmentView(for:))
|
|
delegate?.showGallery(attachments: status.attachments, sourceViews: sourceViews, startIndex: index)
|
|
}
|
|
}
|
|
|
|
extension ConversationMainStatusTableViewCell: MenuPreviewProvider {
|
|
func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? {
|
|
if avatarImageView.frame.contains(location) {
|
|
return (content: { ProfileTableViewController(accountID: self.accountID) }, actions: { self.actionsForProfile(accountID: self.accountID) })
|
|
} else if attachmentsView.frame.contains(location) {
|
|
let attachmentsViewLocation = attachmentsView.convert(location, from: self)
|
|
if let attachmentView = attachmentsView.subviews.first(where: { $0.frame.contains(attachmentsViewLocation) }) as? AttachmentView {
|
|
let image = attachmentView.image!
|
|
let description = attachmentView.attachment.description
|
|
return (content: { self.delegate?.largeImage(image, description: description, sourceView: attachmentView) }, actions: { [] })
|
|
}
|
|
} else if contentLabel.frame.contains(location),
|
|
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)
|
|
}
|
|
}
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
}
|