2019-11-19 17:08:11 +00:00
|
|
|
//
|
|
|
|
// TimelineStatusTableViewCell.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 8/16/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import Pachyderm
|
|
|
|
|
|
|
|
class TimelineStatusTableViewCell: BaseStatusTableViewCell {
|
|
|
|
|
|
|
|
static let relativeDateFormatter: RelativeDateTimeFormatter = {
|
|
|
|
let formatter = RelativeDateTimeFormatter()
|
|
|
|
formatter.dateTimeStyle = .numeric
|
|
|
|
formatter.unitsStyle = .short
|
|
|
|
return formatter
|
|
|
|
}()
|
|
|
|
|
2020-03-02 00:40:32 +00:00
|
|
|
@IBOutlet weak var reblogLabel: EmojiLabel!
|
2019-11-19 17:08:11 +00:00
|
|
|
@IBOutlet weak var timestampLabel: UILabel!
|
|
|
|
@IBOutlet weak var pinImageView: UIImageView!
|
2020-06-17 03:00:48 +00:00
|
|
|
@IBOutlet weak var replyImageView: UIImageView!
|
2019-11-19 17:08:11 +00:00
|
|
|
|
|
|
|
var reblogStatusID: String?
|
|
|
|
var rebloggerID: String?
|
|
|
|
|
2020-06-17 03:00:48 +00:00
|
|
|
var showPinned = false
|
|
|
|
var showReplyIndicator = true
|
2019-11-19 17:08:11 +00:00
|
|
|
|
|
|
|
var updateTimestampWorkItem: DispatchWorkItem?
|
|
|
|
|
|
|
|
var rebloggerAccountUpdater: Cancellable?
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
rebloggerAccountUpdater?.cancel()
|
2020-01-20 04:10:52 +00:00
|
|
|
updateTimestampWorkItem?.cancel()
|
2019-11-19 17:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func awakeFromNib() {
|
|
|
|
super.awakeFromNib()
|
|
|
|
reblogLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(reblogLabelPressed)))
|
|
|
|
|
|
|
|
accessibilityElements!.insert(reblogLabel!, at: 0)
|
|
|
|
}
|
2020-01-06 00:54:28 +00:00
|
|
|
|
|
|
|
override func createObserversIfNecessary() {
|
|
|
|
super.createObserversIfNecessary()
|
|
|
|
|
2020-05-02 16:45:28 +00:00
|
|
|
if rebloggerAccountUpdater == nil {
|
|
|
|
rebloggerAccountUpdater = mastodonController.persistentContainer.accountSubject
|
|
|
|
.filter { [unowned self] in $0 == self.rebloggerID }
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.sink { [unowned self] in
|
|
|
|
if let reblogger = self.mastodonController.persistentContainer.account(for: $0) {
|
|
|
|
self.updateRebloggerLabel(reblogger: reblogger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 00:54:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 23:36:58 +00:00
|
|
|
override func updateUI(statusID: String, state: StatusState) {
|
2020-04-12 16:54:27 +00:00
|
|
|
guard var status = mastodonController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID)") }
|
2019-11-19 17:08:11 +00:00
|
|
|
|
|
|
|
let realStatusID: String
|
2020-04-12 16:54:27 +00:00
|
|
|
if let rebloggedStatus = status.reblog {
|
2019-11-19 17:08:11 +00:00
|
|
|
reblogStatusID = statusID
|
|
|
|
rebloggerID = status.account.id
|
|
|
|
reblogLabel.isHidden = false
|
2020-06-16 03:22:34 +00:00
|
|
|
updateRebloggerLabel(reblogger: status.account)
|
2020-06-17 02:01:01 +00:00
|
|
|
|
|
|
|
status = rebloggedStatus
|
|
|
|
realStatusID = rebloggedStatus.id
|
2019-11-19 17:08:11 +00:00
|
|
|
} else {
|
|
|
|
reblogStatusID = nil
|
|
|
|
rebloggerID = nil
|
|
|
|
reblogLabel.isHidden = true
|
|
|
|
realStatusID = statusID
|
|
|
|
}
|
|
|
|
|
2019-11-28 23:36:58 +00:00
|
|
|
super.updateUI(statusID: realStatusID, state: state)
|
2019-11-19 17:08:11 +00:00
|
|
|
|
|
|
|
updateTimestamp()
|
|
|
|
|
2020-06-17 03:00:48 +00:00
|
|
|
if showPinned {
|
|
|
|
let pinned = status.pinned ?? false
|
|
|
|
timestampLabel.isHidden = pinned
|
|
|
|
pinImageView.isHidden = !pinned
|
|
|
|
}
|
|
|
|
|
|
|
|
replyImageView.isHidden = !showReplyIndicator || status.inReplyToID == nil
|
2019-11-19 17:08:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 03:22:34 +00:00
|
|
|
@objc override func preferencesChanged() {
|
|
|
|
super.preferencesChanged()
|
2019-11-19 17:08:11 +00:00
|
|
|
if let rebloggerID = rebloggerID,
|
2020-04-12 16:54:27 +00:00
|
|
|
let reblogger = mastodonController.persistentContainer.account(for: rebloggerID) {
|
2019-11-19 17:08:11 +00:00
|
|
|
updateRebloggerLabel(reblogger: reblogger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 16:54:27 +00:00
|
|
|
private func updateRebloggerLabel(reblogger: AccountMO) {
|
2020-03-02 00:40:32 +00:00
|
|
|
if Preferences.shared.hideCustomEmojiInUsernames {
|
|
|
|
reblogLabel.text = "Reblogged by \(reblogger.displayNameWithoutCustomEmoji)"
|
|
|
|
reblogLabel.removeEmojis()
|
|
|
|
} else {
|
|
|
|
reblogLabel.text = "Reblogged by \(reblogger.displayOrUserName)"
|
|
|
|
reblogLabel.setEmojis(reblogger.emojis, identifier: reblogger.id)
|
|
|
|
}
|
2019-11-19 17:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateTimestamp() {
|
2020-03-15 16:17:19 +00:00
|
|
|
// if the mastodonController is nil (i.e. the delegate is nil), then the screen this cell was a part of has been deallocated
|
|
|
|
// so we bail out immediately, since there's nothing to update
|
|
|
|
guard let mastodonController = mastodonController else { return }
|
2020-04-12 16:54:27 +00:00
|
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
2019-11-19 17:08:11 +00:00
|
|
|
|
|
|
|
timestampLabel.text = status.createdAt.timeAgoString()
|
|
|
|
timestampLabel.accessibilityLabel = TimelineStatusTableViewCell.relativeDateFormatter.localizedString(for: status.createdAt, relativeTo: Date())
|
|
|
|
|
|
|
|
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 {
|
2020-03-01 23:33:44 +00:00
|
|
|
if updateTimestampWorkItem == nil {
|
|
|
|
updateTimestampWorkItem = DispatchWorkItem { [weak self] in
|
|
|
|
self?.updateTimestamp()
|
|
|
|
}
|
2019-11-19 17:08:11 +00:00
|
|
|
}
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
|
|
|
|
} else {
|
|
|
|
updateTimestampWorkItem = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 04:48:36 +00:00
|
|
|
func reply() {
|
|
|
|
if Preferences.shared.mentionReblogger,
|
|
|
|
let rebloggerID = rebloggerID,
|
2020-05-02 23:52:35 +00:00
|
|
|
let rebloggerAccount = mastodonController.persistentContainer.account(for: rebloggerID) {
|
2020-01-20 04:48:36 +00:00
|
|
|
delegate?.reply(to: statusID, mentioningAcct: rebloggerAccount.acct)
|
|
|
|
} else {
|
|
|
|
delegate?.reply(to: statusID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
updateTimestampWorkItem?.cancel()
|
|
|
|
updateTimestampWorkItem = nil
|
|
|
|
showPinned = false
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func reblogLabelPressed() {
|
|
|
|
guard let rebloggerID = rebloggerID else { return }
|
|
|
|
delegate?.selected(account: rebloggerID)
|
|
|
|
}
|
|
|
|
|
2020-01-20 04:48:36 +00:00
|
|
|
override func replyPressed() {
|
|
|
|
reply()
|
|
|
|
}
|
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
override func getStatusCellPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> BaseStatusTableViewCell.PreviewProviders? {
|
2020-01-05 20:25:07 +00:00
|
|
|
guard let mastodonController = mastodonController else { return nil }
|
2019-11-19 17:08:11 +00:00
|
|
|
return (
|
2020-01-05 20:25:07 +00:00
|
|
|
content: { ConversationTableViewController(for: self.statusID, state: self.statusState.copy(), mastodonController: mastodonController) },
|
2020-01-18 02:29:53 +00:00
|
|
|
actions: { self.actionsForStatus(statusID: self.statusID, sourceView: self) }
|
2019-11-19 17:08:11 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-14 16:59:31 +00:00
|
|
|
extension TimelineStatusTableViewCell: SelectableTableViewCell {
|
|
|
|
func didSelectCell() {
|
|
|
|
delegate?.selected(status: statusID, state: statusState.copy())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
extension TimelineStatusTableViewCell: TableViewSwipeActionProvider {
|
|
|
|
|
|
|
|
func leadingSwipeActionsConfiguration() -> UISwipeActionsConfiguration? {
|
2020-01-05 20:25:07 +00:00
|
|
|
guard let mastodonController = mastodonController else { return nil }
|
2020-04-27 23:32:16 +00:00
|
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
2019-11-19 17:08:11 +00:00
|
|
|
|
|
|
|
let favoriteTitle: String
|
|
|
|
let favoriteRequest: Request<Status>
|
|
|
|
let favoriteColor: UIColor
|
2020-04-27 23:32:16 +00:00
|
|
|
if status.favourited {
|
2019-11-19 17:08:11 +00:00
|
|
|
favoriteTitle = "Unfavorite"
|
2020-04-27 23:32:16 +00:00
|
|
|
favoriteRequest = Status.unfavourite(status.id)
|
2019-11-19 17:08:11 +00:00
|
|
|
favoriteColor = UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
favoriteTitle = "Favorite"
|
2020-04-27 23:32:16 +00:00
|
|
|
favoriteRequest = Status.favourite(status.id)
|
2019-11-19 17:08:11 +00:00
|
|
|
favoriteColor = UIColor(displayP3Red: 1, green: 204/255, blue: 0, alpha: 1)
|
|
|
|
}
|
|
|
|
let favorite = UIContextualAction(style: .normal, title: favoriteTitle) { (action, view, completion) in
|
2020-01-05 20:25:07 +00:00
|
|
|
mastodonController.run(favoriteRequest, completion: { response in
|
2019-11-19 17:08:11 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
guard case let .success(status, _) = response else {
|
|
|
|
completion(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
completion(true)
|
2020-04-27 23:32:16 +00:00
|
|
|
mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false)
|
2019-11-19 17:08:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
favorite.image = UIImage(systemName: "star.fill")
|
|
|
|
favorite.backgroundColor = favoriteColor
|
|
|
|
|
|
|
|
let reblogTitle: String
|
|
|
|
let reblogRequest: Request<Status>
|
|
|
|
let reblogColor: UIColor
|
2020-04-27 23:32:16 +00:00
|
|
|
if status.reblogged {
|
2019-11-19 17:08:11 +00:00
|
|
|
reblogTitle = "Unreblog"
|
2020-04-27 23:32:16 +00:00
|
|
|
reblogRequest = Status.unreblog(status.id)
|
2019-11-19 17:08:11 +00:00
|
|
|
reblogColor = UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1)
|
|
|
|
} else {
|
|
|
|
reblogTitle = "Reblog"
|
2020-04-27 23:32:16 +00:00
|
|
|
reblogRequest = Status.reblog(status.id)
|
2019-11-19 17:08:11 +00:00
|
|
|
reblogColor = tintColor
|
|
|
|
}
|
|
|
|
let reblog = UIContextualAction(style: .normal, title: reblogTitle) { (action, view, completion) in
|
2020-01-05 20:25:07 +00:00
|
|
|
mastodonController.run(reblogRequest, completion: { response in
|
2019-11-19 17:08:11 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
guard case let .success(status, _) = response else {
|
|
|
|
completion(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
completion(true)
|
2020-05-02 16:45:28 +00:00
|
|
|
mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: false)
|
2019-11-19 17:08:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
reblog.image = UIImage(systemName: "repeat")
|
|
|
|
reblog.backgroundColor = reblogColor
|
|
|
|
|
|
|
|
return UISwipeActionsConfiguration(actions: [favorite, reblog])
|
|
|
|
}
|
|
|
|
|
|
|
|
func trailingSwipeActionsConfiguration() -> UISwipeActionsConfiguration? {
|
|
|
|
let reply = UIContextualAction(style: .normal, title: "Reply") { (action, view, completion) in
|
|
|
|
completion(true)
|
2020-01-20 04:48:36 +00:00
|
|
|
self.reply()
|
2019-11-19 17:08:11 +00:00
|
|
|
}
|
|
|
|
reply.image = UIImage(systemName: "arrowshape.turn.up.left.fill")
|
|
|
|
reply.backgroundColor = tintColor
|
|
|
|
let more = UIContextualAction(style: .normal, title: "More") { (action, view, completion) in
|
|
|
|
completion(true)
|
2020-01-18 02:29:53 +00:00
|
|
|
self.delegate?.showMoreOptions(forStatus: self.statusID, sourceView: self)
|
2019-11-19 17:08:11 +00:00
|
|
|
}
|
2020-01-25 15:44:12 +00:00
|
|
|
more.image = UIImage(systemName: "ellipsis.circle.fill")
|
|
|
|
more.backgroundColor = .lightGray
|
2019-11-19 17:08:11 +00:00
|
|
|
return UISwipeActionsConfiguration(actions: [reply, more])
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|