2022-10-03 01:34:06 +00:00
|
|
|
//
|
|
|
|
// TimelineStatusCollectionViewCell.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 10/1/22.
|
|
|
|
// Copyright © 2022 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2022-10-04 04:02:41 +00:00
|
|
|
import Pachyderm
|
2022-10-07 02:36:55 +00:00
|
|
|
import Combine
|
2022-10-04 04:02:41 +00:00
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
class TimelineStatusCollectionViewCell: UICollectionViewListCell, StatusCollectionViewCell {
|
2022-10-03 01:34:06 +00:00
|
|
|
|
2022-10-05 21:40:00 +00:00
|
|
|
// MARK: Subviews
|
2022-10-03 01:34:06 +00:00
|
|
|
|
2022-10-06 02:28:10 +00:00
|
|
|
private lazy var reblogLabel = EmojiLabel().configure {
|
2022-10-03 01:34:06 +00:00
|
|
|
$0.textColor = .secondaryLabel
|
2022-10-04 04:02:41 +00:00
|
|
|
// this needs to have a higher priorty than the content container's zero height constraint
|
|
|
|
$0.setContentHuggingPriority(.defaultHigh, for: .vertical)
|
2022-10-28 22:48:30 +00:00
|
|
|
$0.isUserInteractionEnabled = true
|
2022-10-06 02:28:10 +00:00
|
|
|
$0.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(reblogLabelPressed)))
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private lazy var mainContainer = UIView().configure {
|
|
|
|
avatarImageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
$0.addSubview(avatarImageView)
|
|
|
|
contentVStack.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
$0.addSubview(contentVStack)
|
|
|
|
metaIndicatorsView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
$0.addSubview(metaIndicatorsView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
avatarImageView.leadingAnchor.constraint(equalTo: $0.leadingAnchor),
|
|
|
|
avatarImageView.topAnchor.constraint(equalTo: $0.topAnchor),
|
|
|
|
contentVStack.leadingAnchor.constraint(equalTo: avatarImageView.trailingAnchor, constant: 8),
|
|
|
|
contentVStack.trailingAnchor.constraint(equalTo: $0.trailingAnchor),
|
|
|
|
contentVStack.topAnchor.constraint(equalTo: $0.topAnchor),
|
|
|
|
contentVStack.bottomAnchor.constraint(equalTo: $0.bottomAnchor),
|
|
|
|
metaIndicatorsView.leadingAnchor.constraint(greaterThanOrEqualTo: $0.leadingAnchor),
|
|
|
|
metaIndicatorsView.trailingAnchor.constraint(equalTo: avatarImageView.trailingAnchor),
|
|
|
|
metaIndicatorsView.topAnchor.constraint(equalTo: avatarImageView.bottomAnchor, constant: 4),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2022-10-05 21:40:00 +00:00
|
|
|
private static let avatarImageViewSize: CGFloat = 50
|
2022-10-06 03:12:03 +00:00
|
|
|
private(set) lazy var avatarImageView = CachedImageView(cache: .avatars).configure {
|
2022-10-03 01:34:06 +00:00
|
|
|
$0.layer.masksToBounds = true
|
|
|
|
NSLayoutConstraint.activate([
|
2022-10-05 21:40:00 +00:00
|
|
|
$0.heightAnchor.constraint(equalToConstant: TimelineStatusCollectionViewCell.avatarImageViewSize),
|
|
|
|
$0.widthAnchor.constraint(equalToConstant: TimelineStatusCollectionViewCell.avatarImageViewSize),
|
2022-10-03 01:34:06 +00:00
|
|
|
])
|
2022-10-06 02:28:10 +00:00
|
|
|
$0.isUserInteractionEnabled = true
|
|
|
|
$0.addInteraction(UIContextMenuInteraction(delegate: self))
|
|
|
|
$0.addInteraction(UIDragInteraction(delegate: self))
|
|
|
|
$0.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private let metaIndicatorsView = StatusMetaIndicatorsView()
|
|
|
|
|
|
|
|
private lazy var contentVStack = UIStackView(arrangedSubviews: [
|
|
|
|
nameHStack,
|
|
|
|
contentWarningLabel,
|
|
|
|
collapseButton,
|
2022-10-04 04:02:41 +00:00
|
|
|
contentContainer,
|
2022-10-03 01:34:06 +00:00
|
|
|
]).configure {
|
|
|
|
$0.axis = .vertical
|
|
|
|
$0.spacing = 4
|
|
|
|
$0.alignment = .fill
|
|
|
|
}
|
|
|
|
|
|
|
|
private lazy var nameHStack = UIStackView(arrangedSubviews: [
|
|
|
|
displayNameLabel,
|
|
|
|
usernameLabel,
|
|
|
|
pinImageView,
|
|
|
|
timestampLabel,
|
|
|
|
]).configure {
|
|
|
|
$0.axis = .horizontal
|
|
|
|
$0.spacing = 4
|
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
let displayNameLabel = EmojiLabel().configure {
|
2022-10-03 01:34:06 +00:00
|
|
|
$0.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).addingAttributes([
|
|
|
|
.traits: [
|
|
|
|
UIFontDescriptor.TraitKey.weight: UIFont.Weight.semibold.rawValue,
|
|
|
|
]
|
|
|
|
]), size: 0)
|
|
|
|
$0.setContentHuggingPriority(.init(251), for: .horizontal)
|
|
|
|
$0.setContentCompressionResistancePriority(.init(749), for: .horizontal)
|
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
let usernameLabel = UILabel().configure {
|
2022-10-03 01:34:06 +00:00
|
|
|
$0.textColor = .secondaryLabel
|
|
|
|
$0.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).addingAttributes([
|
|
|
|
.traits: [
|
|
|
|
UIFontDescriptor.TraitKey.weight: UIFont.Weight.light.rawValue,
|
|
|
|
]
|
|
|
|
]), size: 0)
|
|
|
|
$0.setContentHuggingPriority(.init(249), for: .horizontal)
|
|
|
|
$0.setContentCompressionResistancePriority(.init(748), for: .horizontal)
|
|
|
|
}
|
|
|
|
|
|
|
|
private let pinImageView = UIImageView(image: UIImage(systemName: "pin.fill")).configure {
|
|
|
|
$0.tintColor = .secondaryLabel
|
|
|
|
$0.setContentHuggingPriority(.init(251), for: .horizontal)
|
|
|
|
}
|
|
|
|
|
|
|
|
private let timestampLabel = UILabel().configure {
|
|
|
|
$0.textColor = .secondaryLabel
|
|
|
|
$0.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).addingAttributes([
|
|
|
|
.traits: [
|
|
|
|
UIFontDescriptor.TraitKey.weight: UIFont.Weight.light.rawValue,
|
|
|
|
]
|
|
|
|
]), size: 0)
|
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
private(set) lazy var contentWarningLabel = EmojiLabel().configure {
|
2022-10-11 21:04:31 +00:00
|
|
|
$0.numberOfLines = 0
|
2022-10-03 01:34:06 +00:00
|
|
|
$0.textColor = .secondaryLabel
|
|
|
|
$0.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).addingAttributes([
|
|
|
|
.traits: [
|
|
|
|
UIFontDescriptor.TraitKey.weight: UIFont.Weight.bold.rawValue,
|
|
|
|
]
|
|
|
|
]), size: 0)
|
2022-10-04 04:02:41 +00:00
|
|
|
// this needs to have a higher priorty than the content container's zero height constraint
|
|
|
|
$0.setContentHuggingPriority(.defaultHigh, for: .vertical)
|
2022-10-08 19:03:50 +00:00
|
|
|
$0.isUserInteractionEnabled = true
|
2022-10-04 04:02:41 +00:00
|
|
|
$0.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(collapseButtonPressed)))
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 19:03:50 +00:00
|
|
|
private(set) lazy var collapseButton = StatusCollapseButton(configuration: {
|
2022-10-03 01:34:06 +00:00
|
|
|
var config = UIButton.Configuration.filled()
|
|
|
|
config.image = UIImage(systemName: "chevron.down")
|
|
|
|
return config
|
2022-10-04 04:02:41 +00:00
|
|
|
}()).configure {
|
2022-10-06 03:12:03 +00:00
|
|
|
// this button is so big that dimming its background color is visually distracting
|
|
|
|
$0.tintAdjustmentMode = .normal
|
2022-10-04 04:02:41 +00:00
|
|
|
$0.setContentHuggingPriority(.defaultHigh, for: .vertical)
|
|
|
|
$0.addTarget(self, action: #selector(collapseButtonPressed), for: .touchUpInside)
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
let contentContainer = StatusContentContainer().configure {
|
2022-10-04 04:02:41 +00:00
|
|
|
$0.setContentHuggingPriority(.defaultLow, for: .vertical)
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
2022-10-04 04:02:41 +00:00
|
|
|
private var contentTextView: StatusContentTextView {
|
|
|
|
contentContainer.contentTextView
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
2022-10-04 04:02:41 +00:00
|
|
|
private var cardView: StatusCardView {
|
|
|
|
contentContainer.cardView
|
|
|
|
}
|
|
|
|
private var attachmentsView: AttachmentsContainerView {
|
|
|
|
contentContainer.attachmentsView
|
|
|
|
}
|
|
|
|
private var pollView: StatusPollView {
|
|
|
|
contentContainer.pollView
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var placeholderReplyButtonLeadingConstraint: NSLayoutConstraint!
|
|
|
|
private lazy var actionsContainer = UIView().configure {
|
|
|
|
replyButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
$0.addSubview(replyButton)
|
|
|
|
favoriteButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
$0.addSubview(favoriteButton)
|
|
|
|
reblogButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
$0.addSubview(reblogButton)
|
|
|
|
moreButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
$0.addSubview(moreButton)
|
|
|
|
placeholderReplyButtonLeadingConstraint = replyButton.leadingAnchor.constraint(equalTo: $0.leadingAnchor)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
favoriteButton.widthAnchor.constraint(equalTo: replyButton.widthAnchor),
|
|
|
|
reblogButton.widthAnchor.constraint(equalTo: replyButton.widthAnchor),
|
|
|
|
moreButton.widthAnchor.constraint(equalTo: replyButton.widthAnchor),
|
|
|
|
|
|
|
|
placeholderReplyButtonLeadingConstraint,
|
|
|
|
replyButton.topAnchor.constraint(equalTo: $0.topAnchor),
|
|
|
|
replyButton.bottomAnchor.constraint(equalTo: $0.bottomAnchor),
|
|
|
|
|
|
|
|
favoriteButton.leadingAnchor.constraint(equalTo: replyButton.trailingAnchor),
|
|
|
|
favoriteButton.topAnchor.constraint(equalTo: $0.topAnchor),
|
|
|
|
favoriteButton.bottomAnchor.constraint(equalTo: $0.bottomAnchor),
|
|
|
|
|
|
|
|
reblogButton.leadingAnchor.constraint(equalTo: favoriteButton.trailingAnchor),
|
|
|
|
reblogButton.topAnchor.constraint(equalTo: $0.topAnchor),
|
|
|
|
reblogButton.bottomAnchor.constraint(equalTo: $0.bottomAnchor),
|
|
|
|
|
|
|
|
moreButton.leadingAnchor.constraint(equalTo: reblogButton.trailingAnchor),
|
|
|
|
moreButton.trailingAnchor.constraint(equalTo: $0.trailingAnchor),
|
|
|
|
moreButton.topAnchor.constraint(equalTo: $0.topAnchor),
|
|
|
|
moreButton.bottomAnchor.constraint(equalTo: $0.bottomAnchor),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
private(set) lazy var replyButton = UIButton().configure {
|
2022-10-03 01:34:06 +00:00
|
|
|
$0.setImage(UIImage(systemName: "arrowshape.turn.up.left.fill"), for: .normal)
|
2022-10-06 02:28:10 +00:00
|
|
|
$0.addTarget(self, action: #selector(replyPressed), for: .touchUpInside)
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
private(set) lazy var favoriteButton = UIButton().configure {
|
2022-10-03 01:34:06 +00:00
|
|
|
$0.setImage(UIImage(systemName: "star.fill"), for: .normal)
|
2022-10-06 02:28:10 +00:00
|
|
|
$0.addTarget(self, action: #selector(favoritePressed), for: .touchUpInside)
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
private(set) lazy var reblogButton = UIButton().configure {
|
2022-10-03 01:34:06 +00:00
|
|
|
$0.setImage(UIImage(systemName: "repeat"), for: .normal)
|
2022-10-06 02:28:10 +00:00
|
|
|
$0.addTarget(self, action: #selector(reblogPressed), for: .touchUpInside)
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
let moreButton = UIButton().configure {
|
2022-10-03 01:34:06 +00:00
|
|
|
$0.setImage(UIImage(systemName: "ellipsis"), for: .normal)
|
|
|
|
$0.showsMenuAsPrimaryAction = true
|
|
|
|
}
|
|
|
|
|
2022-10-05 21:40:00 +00:00
|
|
|
// MARK: Cell state
|
|
|
|
|
|
|
|
private var mainContainerTopToReblogLabelConstraint: NSLayoutConstraint!
|
|
|
|
private var mainContainerTopToSelfConstraint: NSLayoutConstraint!
|
|
|
|
private var mainContainerBottomToActionsConstraint: NSLayoutConstraint!
|
|
|
|
private var mainContainerBottomToSelfConstraint: NSLayoutConstraint!
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
weak var delegate: StatusCollectionViewCellDelegate?
|
2022-10-05 21:40:00 +00:00
|
|
|
var showStatusAutomatically: Bool {
|
|
|
|
// TODO: needed once conversation controller refactored
|
|
|
|
false
|
|
|
|
}
|
|
|
|
var showReplyIndicator: Bool {
|
|
|
|
// TODO: needed once conversation controller refactored
|
2022-10-08 19:17:52 +00:00
|
|
|
true
|
2022-10-05 21:40:00 +00:00
|
|
|
}
|
2022-10-11 01:01:03 +00:00
|
|
|
var showPinned: Bool = false
|
2022-10-04 04:02:41 +00:00
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
// alas these need to be internal so they're accessible from the protocol extensions
|
|
|
|
var statusID: String!
|
|
|
|
var statusState: StatusState!
|
|
|
|
var accountID: String!
|
2022-10-05 21:40:00 +00:00
|
|
|
private var reblogStatusID: String?
|
|
|
|
private var rebloggerID: String?
|
2022-10-04 04:02:41 +00:00
|
|
|
|
2022-10-03 01:34:06 +00:00
|
|
|
private var firstLayout = true
|
2022-10-06 03:12:03 +00:00
|
|
|
var isGrayscale = false
|
2022-10-06 02:28:10 +00:00
|
|
|
private var updateTimestampWorkItem: DispatchWorkItem?
|
2022-10-07 02:36:55 +00:00
|
|
|
private var hasCreatedObservers = false
|
|
|
|
var cancellables = Set<AnyCancellable>()
|
2022-10-03 01:34:06 +00:00
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
|
2022-10-05 21:40:00 +00:00
|
|
|
for subview in [reblogLabel, mainContainer, actionsContainer] {
|
|
|
|
subview.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
contentView.addSubview(subview)
|
|
|
|
}
|
|
|
|
|
|
|
|
mainContainerTopToReblogLabelConstraint = mainContainer.topAnchor.constraint(equalTo: reblogLabel.bottomAnchor, constant: 4)
|
|
|
|
mainContainerTopToSelfConstraint = mainContainer.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8)
|
|
|
|
mainContainerBottomToActionsConstraint = mainContainer.bottomAnchor.constraint(equalTo: actionsContainer.topAnchor)
|
|
|
|
mainContainerBottomToSelfConstraint = mainContainer.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -6)
|
|
|
|
|
|
|
|
let metaIndicatorsBottomConstraint = metaIndicatorsView.bottomAnchor.constraint(lessThanOrEqualTo: contentView.bottomAnchor, constant: -6)
|
|
|
|
// sometimes during intermediate layouts, there are conflicting constraints, so let this one get broken temporarily, to avoid a bunch of printing
|
|
|
|
metaIndicatorsBottomConstraint.priority = .init(999)
|
|
|
|
|
2022-10-03 01:34:06 +00:00
|
|
|
NSLayoutConstraint.activate([
|
2022-10-05 21:40:00 +00:00
|
|
|
// why is this 4 but the mainContainerTopSelfConstraint constant 8? because this looks more balanced
|
|
|
|
reblogLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 4),
|
|
|
|
reblogLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
|
|
|
|
reblogLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
|
|
|
|
|
|
|
|
mainContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
|
|
|
|
mainContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
|
|
|
|
|
|
|
|
actionsContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 16),
|
|
|
|
actionsContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -16),
|
|
|
|
// yes, this is deliberately 6. 4 looks to cramped, 8 looks uneven
|
|
|
|
actionsContainer.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -6),
|
|
|
|
|
|
|
|
metaIndicatorsBottomConstraint,
|
2022-10-03 01:34:06 +00:00
|
|
|
])
|
|
|
|
|
2022-10-05 21:40:00 +00:00
|
|
|
updateActionsVisibility()
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func layoutSubviews() {
|
|
|
|
super.layoutSubviews()
|
|
|
|
|
|
|
|
if firstLayout {
|
|
|
|
firstLayout = false
|
2022-10-05 21:40:00 +00:00
|
|
|
|
|
|
|
// the button's image view doesn't exist until after the first layout
|
|
|
|
// accessing it before that cause the button to layoutIfNeeded which generates a broken, intermediate layout and prints a bunch of unhelpful autolayout warnings
|
|
|
|
// so we wait until after the first layout pass to setup the reply button's real constraint
|
2022-10-03 01:34:06 +00:00
|
|
|
placeholderReplyButtonLeadingConstraint.isActive = false
|
|
|
|
replyButton.imageView!.leadingAnchor.constraint(equalTo: contentTextView.leadingAnchor).isActive = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 02:28:10 +00:00
|
|
|
// MARK: Accessibility
|
|
|
|
|
|
|
|
override var accessibilityLabel: String? {
|
|
|
|
get {
|
|
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var str = "\(status.account.displayOrUserName), \(contentTextView.text ?? "")"
|
|
|
|
|
|
|
|
if status.attachments.count > 0 {
|
|
|
|
// TODO: localize me
|
|
|
|
str += ", \(status.attachments.count) attachment\(status.attachments.count > 1 ? "s" : "")"
|
|
|
|
}
|
|
|
|
if status.poll != nil {
|
|
|
|
str += ", poll"
|
|
|
|
}
|
|
|
|
str += ", \(status.createdAt.formatted(.relative(presentation: .numeric)))"
|
|
|
|
if let rebloggerID,
|
|
|
|
let reblogger = mastodonController.persistentContainer.account(for: rebloggerID) {
|
|
|
|
str += ", reblogged by \(reblogger.displayOrUserName)"
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
set {}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func accessibilityActivate() -> Bool {
|
|
|
|
delegate?.selected(status: statusID, state: statusState.copy())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Configure UI
|
|
|
|
|
2022-10-04 04:02:41 +00:00
|
|
|
func updateUI(statusID: String, state: StatusState) {
|
2022-10-05 21:40:00 +00:00
|
|
|
guard var status = mastodonController.persistentContainer.status(for: statusID) else {
|
2022-10-04 04:02:41 +00:00
|
|
|
fatalError()
|
|
|
|
}
|
2022-10-05 21:40:00 +00:00
|
|
|
|
2022-10-07 02:36:55 +00:00
|
|
|
createObservers()
|
|
|
|
|
2022-10-04 04:02:41 +00:00
|
|
|
self.statusState = state
|
2022-10-05 21:40:00 +00:00
|
|
|
|
|
|
|
if let rebloggedStatus = status.reblog {
|
|
|
|
reblogStatusID = statusID
|
|
|
|
rebloggerID = status.account.id
|
|
|
|
reblogLabel.isHidden = false
|
|
|
|
mainContainerTopToReblogLabelConstraint.isActive = true
|
|
|
|
mainContainerTopToSelfConstraint.isActive = false
|
|
|
|
updateRebloggerLabel(reblogger: status.account)
|
|
|
|
|
|
|
|
status = rebloggedStatus
|
|
|
|
} else {
|
|
|
|
reblogStatusID = nil
|
|
|
|
rebloggerID = nil
|
|
|
|
reblogLabel.isHidden = true
|
|
|
|
mainContainerTopToReblogLabelConstraint.isActive = false
|
|
|
|
mainContainerTopToSelfConstraint.isActive = true
|
|
|
|
}
|
|
|
|
|
|
|
|
doUpdateUI(status: status)
|
|
|
|
|
2022-10-06 02:28:10 +00:00
|
|
|
doUpdateTimestamp(status: status)
|
|
|
|
timestampLabel.isHidden = showPinned
|
|
|
|
pinImageView.isHidden = !showPinned
|
2022-10-05 21:40:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 02:36:55 +00:00
|
|
|
func createObservers() {
|
|
|
|
guard !hasCreatedObservers else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hasCreatedObservers = true
|
|
|
|
baseCreateObservers()
|
|
|
|
|
|
|
|
mastodonController.persistentContainer.accountSubject
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.filter { [unowned self] in $0 == self.rebloggerID }
|
|
|
|
.sink { [unowned self] in
|
|
|
|
if let mastodonController = self.mastodonController,
|
|
|
|
let reblogger = mastodonController.persistentContainer.account(for: $0) {
|
|
|
|
self.updateRebloggerLabel(reblogger: reblogger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
func updateUIForPreferences(status: StatusMO) {
|
|
|
|
baseUpdateUIForPreferences(status: status)
|
2022-10-05 21:40:00 +00:00
|
|
|
|
|
|
|
if showReplyIndicator {
|
|
|
|
metaIndicatorsView.allowedIndicators = .all
|
|
|
|
} else {
|
|
|
|
metaIndicatorsView.allowedIndicators = .all.subtracting(.reply)
|
|
|
|
}
|
2022-10-08 19:17:52 +00:00
|
|
|
metaIndicatorsView.updateUI(status: status)
|
2022-10-05 21:40:00 +00:00
|
|
|
|
|
|
|
if let rebloggerID,
|
|
|
|
let reblogger = mastodonController.persistentContainer.account(for: rebloggerID) {
|
|
|
|
updateRebloggerLabel(reblogger: reblogger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 02:28:10 +00:00
|
|
|
private func updateTimestamp() {
|
|
|
|
guard let mastodonController,
|
|
|
|
let status = mastodonController.persistentContainer.status(for: statusID) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
doUpdateTimestamp(status: status)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func doUpdateTimestamp(status: StatusMO) {
|
2022-10-08 19:25:58 +00:00
|
|
|
// if there's a pending update timestamp work item, cancel it
|
|
|
|
updateTimestampWorkItem?.cancel()
|
|
|
|
|
2022-10-06 02:28:10 +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 {
|
|
|
|
if updateTimestampWorkItem == nil {
|
|
|
|
updateTimestampWorkItem = DispatchWorkItem { [weak self] in
|
|
|
|
self?.updateTimestamp()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
|
|
|
|
} else {
|
|
|
|
updateTimestampWorkItem = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 21:40:00 +00:00
|
|
|
private func updateRebloggerLabel(reblogger: AccountMO) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateActionsVisibility() {
|
|
|
|
if Preferences.shared.hideActionsInTimeline {
|
|
|
|
actionsContainer.isHidden = true
|
|
|
|
mainContainerBottomToSelfConstraint.isActive = true
|
|
|
|
mainContainerBottomToActionsConstraint.isActive = false
|
|
|
|
} else {
|
|
|
|
actionsContainer.isHidden = false
|
|
|
|
mainContainerBottomToSelfConstraint.isActive = false
|
|
|
|
mainContainerBottomToActionsConstraint.isActive = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func preferencesChanged() {
|
|
|
|
guard let mastodonController,
|
|
|
|
let status = mastodonController.persistentContainer.status(for: statusID) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-06 03:12:03 +00:00
|
|
|
updateUIForPreferences(status: status)
|
|
|
|
|
|
|
|
if isGrayscale != Preferences.shared.grayscaleImages {
|
|
|
|
updateGrayscaleableUI(status: status)
|
|
|
|
}
|
2022-10-05 21:40:00 +00:00
|
|
|
|
|
|
|
// only needs to happen when prefs change, rather than in updateUIForPrefs b/c this is setup correctly during init
|
|
|
|
let oldState = actionsContainer.isHidden
|
|
|
|
if oldState != Preferences.shared.hideActionsInTimeline {
|
|
|
|
updateActionsVisibility()
|
2022-10-08 19:31:08 +00:00
|
|
|
delegate?.statusCellNeedsReconfigure(self, animated: true, completion: nil)
|
2022-10-05 21:40:00 +00:00
|
|
|
}
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|
2022-10-04 04:02:41 +00:00
|
|
|
|
|
|
|
// MARK: Interaction
|
|
|
|
|
2022-10-06 02:28:10 +00:00
|
|
|
@objc private func reblogLabelPressed() {
|
|
|
|
guard let rebloggerID else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delegate?.selected(account: rebloggerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func accountPressed() {
|
|
|
|
delegate?.selected(account: accountID)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func collapseButtonPressed() {
|
2022-10-06 03:12:03 +00:00
|
|
|
toggleCollapse()
|
2022-10-04 04:02:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 02:28:10 +00:00
|
|
|
@objc private func replyPressed() {
|
2022-10-06 03:12:03 +00:00
|
|
|
if Preferences.shared.mentionReblogger,
|
|
|
|
let rebloggerID = rebloggerID,
|
|
|
|
let rebloggerAccount = mastodonController.persistentContainer.account(for: rebloggerID) {
|
|
|
|
delegate?.compose(inReplyToID: statusID, mentioningAcct: rebloggerAccount.acct)
|
|
|
|
} else {
|
|
|
|
delegate?.compose(inReplyToID: statusID)
|
|
|
|
}
|
2022-10-06 02:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func favoritePressed() {
|
2022-10-06 03:12:03 +00:00
|
|
|
toggleFavorite()
|
2022-10-06 02:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func reblogPressed() {
|
2022-10-06 03:12:03 +00:00
|
|
|
toggleReblog()
|
2022-10-06 02:28:10 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 18:33:07 +00:00
|
|
|
func leadingSwipeActions() -> UISwipeActionsConfiguration? {
|
|
|
|
guard mastodonController.loggedIn,
|
|
|
|
let status = mastodonController.persistentContainer.status(for: statusID) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let favoriteTitle = status.favourited ? "Unfavorite" : "Favorite"
|
|
|
|
let favorite = UIContextualAction(style: .normal, title: favoriteTitle) { [unowned self] _, _, completion in
|
|
|
|
Task {
|
|
|
|
await FavoriteService(status: status, mastodonController: self.mastodonController, presenter: self.delegate!).toggleFavorite()
|
|
|
|
completion(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
favorite.image = UIImage(systemName: "star.fill")
|
|
|
|
favorite.backgroundColor = status.favourited ? UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1) : UIColor(displayP3Red: 1, green: 204/255, blue: 0, alpha: 1)
|
|
|
|
|
|
|
|
let reblogTitle = status.reblogged ? "Unreblog" : "Reblog"
|
|
|
|
let reblog = UIContextualAction(style: .normal, title: reblogTitle) { _, _, completion in
|
|
|
|
Task {
|
|
|
|
await ReblogService(status: status, mastodonController: self.mastodonController, presenter: self.delegate!).toggleReblog()
|
|
|
|
completion(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reblog.image = UIImage(systemName: "repeat")
|
|
|
|
reblog.backgroundColor = status.reblogged ? UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1) : tintColor
|
|
|
|
|
|
|
|
return UISwipeActionsConfiguration(actions: [favorite, reblog])
|
|
|
|
}
|
|
|
|
|
|
|
|
func trailingSwipeActions() -> UISwipeActionsConfiguration? {
|
|
|
|
var actions = [UIContextualAction]()
|
|
|
|
|
|
|
|
let share = UIContextualAction(style: .normal, title: "Share") { [unowned self] _, _, completion in
|
|
|
|
self.delegate?.showMoreOptions(forStatus: statusID, sourceView: self)
|
|
|
|
completion(true)
|
|
|
|
}
|
|
|
|
// bold to more closesly match other action symbols
|
|
|
|
let config = UIImage.SymbolConfiguration(weight: .bold)
|
|
|
|
share.image = UIImage(systemName: "square.and.arrow.up")!.applyingSymbolConfiguration(config)!
|
|
|
|
share.backgroundColor = .lightGray
|
|
|
|
actions.append(share)
|
|
|
|
|
|
|
|
if mastodonController.loggedIn {
|
|
|
|
let reply = UIContextualAction(style: .normal, title: "Reply") { [unowned self] _, _, completion in
|
|
|
|
self.replyPressed()
|
|
|
|
completion(true)
|
|
|
|
}
|
|
|
|
reply.image = UIImage(systemName: "arrowshape.turn.up.left.fill")
|
|
|
|
reply.backgroundColor = tintColor
|
|
|
|
actions.insert(reply, at: 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return UISwipeActionsConfiguration(actions: actions)
|
|
|
|
}
|
|
|
|
|
2022-10-08 18:59:50 +00:00
|
|
|
func dragItemsForBeginning(session: UIDragSession) -> [UIDragItem] {
|
|
|
|
// the poll options view is tracking while the user is dragging between options
|
|
|
|
// while that's happening, don't initiate a drag
|
|
|
|
guard !pollView.isTracking,
|
|
|
|
let status = mastodonController.persistentContainer.status(for: statusID),
|
|
|
|
let accountID = mastodonController.accountInfo?.id else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
let provider = NSItemProvider(object: status.url! as NSURL)
|
|
|
|
let activity = UserActivityManager.showConversationActivity(mainStatusID: status.id, accountID: accountID)
|
|
|
|
activity.displaysAuxiliaryScene = true
|
|
|
|
provider.registerObject(activity, visibility: .all)
|
|
|
|
return [UIDragItem(itemProvider: provider)]
|
|
|
|
}
|
|
|
|
|
2022-10-08 23:16:10 +00:00
|
|
|
func contextMenuConfiguration() -> UIContextMenuConfiguration? {
|
|
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return UIContextMenuConfiguration {
|
|
|
|
ConversationTableViewController(for: self.statusID, state: self.statusState.copy(), mastodonController: self.mastodonController)
|
|
|
|
} actionProvider: { _ in
|
|
|
|
UIMenu(children: self.delegate!.actionsForStatus(status, sourceView: self))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 02:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension TimelineStatusCollectionViewCell: UIContextMenuInteractionDelegate {
|
|
|
|
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
|
2022-10-06 03:12:03 +00:00
|
|
|
return contextMenuConfigurationForAccount(sourceView: interaction.view!)
|
2022-10-06 02:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
2022-10-06 03:12:03 +00:00
|
|
|
if let delegate {
|
|
|
|
MenuPreviewHelper.willPerformPreviewAction(animator: animator, presenter: delegate)
|
2022-10-06 02:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension TimelineStatusCollectionViewCell: UIDragInteractionDelegate {
|
|
|
|
func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
|
2022-10-06 03:12:03 +00:00
|
|
|
return dragItemsForAccount()
|
2022-10-06 02:28:10 +00:00
|
|
|
}
|
2022-10-03 01:34:06 +00:00
|
|
|
}
|