2018-08-17 02:39:16 +00:00
|
|
|
//
|
2019-11-19 17:08:11 +00:00
|
|
|
// BaseStatusTableViewCell.swift
|
2018-08-17 02:39:16 +00:00
|
|
|
// Tusker
|
|
|
|
//
|
2019-11-19 17:08:11 +00:00
|
|
|
// Created by Shadowfacts on 11/19/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
2018-08-17 02:39:16 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2018-09-11 14:52:21 +00:00
|
|
|
import Pachyderm
|
2019-11-19 17:08:11 +00:00
|
|
|
import Combine
|
2020-10-17 16:56:13 +00:00
|
|
|
import AVKit
|
2018-08-17 02:39:16 +00:00
|
|
|
|
2022-05-02 03:04:56 +00:00
|
|
|
protocol StatusTableViewCellDelegate: TuskerNavigationDelegate, MenuActionProvider {
|
2022-06-27 21:25:22 +00:00
|
|
|
// @available(iOS, obsoleted: 16.0)
|
2019-11-28 23:36:58 +00:00
|
|
|
func statusCellCollapsedStateChanged(_ cell: BaseStatusTableViewCell)
|
2018-08-27 19:23:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 03:04:56 +00:00
|
|
|
class BaseStatusTableViewCell: UITableViewCell {
|
2020-01-05 20:25:07 +00:00
|
|
|
|
2020-01-20 04:02:07 +00:00
|
|
|
weak var delegate: StatusTableViewCellDelegate? {
|
2018-10-12 01:20:58 +00:00
|
|
|
didSet {
|
2020-01-18 21:00:38 +00:00
|
|
|
contentTextView.navigationDelegate = delegate
|
2018-10-12 01:20:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-06 00:39:37 +00:00
|
|
|
var overrideMastodonController: MastodonController?
|
|
|
|
var mastodonController: MastodonController! { overrideMastodonController ?? delegate?.apiController }
|
2019-12-14 18:36:05 +00:00
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
@IBOutlet weak var avatarImageView: UIImageView!
|
2020-03-02 00:40:32 +00:00
|
|
|
@IBOutlet weak var displayNameLabel: EmojiLabel!
|
2018-08-17 02:39:16 +00:00
|
|
|
@IBOutlet weak var usernameLabel: UILabel!
|
2022-01-23 15:58:36 +00:00
|
|
|
@IBOutlet weak var metaIndicatorsView: StatusMetaIndicatorsView!
|
2020-03-12 01:56:35 +00:00
|
|
|
@IBOutlet weak var contentWarningLabel: EmojiLabel!
|
2019-09-09 22:40:23 +00:00
|
|
|
@IBOutlet weak var collapseButton: UIButton!
|
2020-01-18 21:00:38 +00:00
|
|
|
@IBOutlet weak var contentTextView: StatusContentTextView!
|
2020-10-25 20:05:28 +00:00
|
|
|
@IBOutlet weak var cardView: StatusCardView!
|
2019-06-16 23:02:18 +00:00
|
|
|
@IBOutlet weak var attachmentsView: AttachmentsContainerView!
|
2021-04-28 23:00:17 +00:00
|
|
|
@IBOutlet weak var pollView: StatusPollView!
|
2019-09-27 00:53:22 +00:00
|
|
|
@IBOutlet weak var replyButton: UIButton!
|
2018-09-09 01:35:40 +00:00
|
|
|
@IBOutlet weak var favoriteButton: UIButton!
|
|
|
|
@IBOutlet weak var reblogButton: UIButton!
|
2019-09-27 00:53:22 +00:00
|
|
|
@IBOutlet weak var moreButton: UIButton!
|
2021-01-31 22:42:29 +00:00
|
|
|
private(set) var prevThreadLinkView: UIView?
|
|
|
|
private(set) var nextThreadLinkView: UIView?
|
2018-08-17 02:39:16 +00:00
|
|
|
|
2018-09-18 01:57:46 +00:00
|
|
|
var statusID: String!
|
2022-01-23 15:58:36 +00:00
|
|
|
private(set) var accountID: String!
|
2019-11-19 17:08:11 +00:00
|
|
|
|
2022-01-23 15:58:36 +00:00
|
|
|
private var favorited = false {
|
2018-09-09 01:35:40 +00:00
|
|
|
didSet {
|
|
|
|
favoriteButton.tintColor = favorited ? UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) : tintColor
|
|
|
|
}
|
|
|
|
}
|
2022-01-23 15:58:36 +00:00
|
|
|
private var reblogged = false {
|
2018-09-09 01:35:40 +00:00
|
|
|
didSet {
|
|
|
|
reblogButton.tintColor = reblogged ? UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) : tintColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 15:58:36 +00:00
|
|
|
private(set) var statusState: StatusState!
|
2019-09-09 22:40:23 +00:00
|
|
|
var collapsible = false {
|
|
|
|
didSet {
|
|
|
|
collapseButton.isHidden = !collapsible
|
2019-11-28 23:36:58 +00:00
|
|
|
statusState?.collapsible = collapsible
|
|
|
|
}
|
|
|
|
}
|
2022-01-23 15:58:36 +00:00
|
|
|
private var collapsed = false {
|
2019-11-28 23:36:58 +00:00
|
|
|
didSet {
|
|
|
|
statusState?.collapsed = collapsed
|
2019-09-09 22:40:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-19 03:23:15 +00:00
|
|
|
var showStatusAutomatically = false
|
|
|
|
|
2020-01-25 15:06:27 +00:00
|
|
|
private var avatarRequest: ImageCache.Request?
|
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
private var statusUpdater: Cancellable?
|
|
|
|
private var accountUpdater: Cancellable?
|
|
|
|
|
2020-10-17 16:56:13 +00:00
|
|
|
private var currentPictureInPictureVideoStatusID: String?
|
|
|
|
|
2020-11-01 18:59:58 +00:00
|
|
|
private var isGrayscale = false
|
|
|
|
|
2018-08-28 01:27:34 +00:00
|
|
|
override func awakeFromNib() {
|
2019-11-19 17:08:11 +00:00
|
|
|
super.awakeFromNib()
|
|
|
|
|
2018-08-28 01:27:34 +00:00
|
|
|
displayNameLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
|
|
|
|
usernameLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
|
2020-12-14 03:37:37 +00:00
|
|
|
|
2018-08-28 01:27:34 +00:00
|
|
|
avatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
|
|
|
|
avatarImageView.layer.masksToBounds = true
|
2020-12-14 03:37:37 +00:00
|
|
|
avatarImageView.addInteraction(UIDragInteraction(delegate: self))
|
2019-11-19 17:08:11 +00:00
|
|
|
|
2019-06-16 23:02:18 +00:00
|
|
|
attachmentsView.delegate = self
|
2019-11-19 17:08:11 +00:00
|
|
|
|
2019-09-09 22:40:23 +00:00
|
|
|
collapseButton.layer.masksToBounds = true
|
|
|
|
collapseButton.layer.cornerRadius = 5
|
2019-08-02 03:01:15 +00:00
|
|
|
|
2021-06-07 01:50:45 +00:00
|
|
|
accessibilityElements = [displayNameLabel!, contentWarningLabel!, collapseButton!, contentTextView!, attachmentsView!, pollView!]
|
2019-09-27 00:53:22 +00:00
|
|
|
|
2021-02-06 18:47:45 +00:00
|
|
|
moreButton.showsMenuAsPrimaryAction = true
|
2020-06-27 17:13:04 +00:00
|
|
|
|
2020-06-16 03:22:34 +00:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
|
2020-11-03 20:58:08 +00:00
|
|
|
|
|
|
|
contentWarningLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(collapseButtonPressed)))
|
2020-01-06 00:54:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
open func createObserversIfNecessary() {
|
2020-05-02 16:45:28 +00:00
|
|
|
if statusUpdater == nil {
|
|
|
|
statusUpdater = mastodonController.persistentContainer.statusSubject
|
|
|
|
.receive(on: DispatchQueue.main)
|
2021-06-26 20:51:54 +00:00
|
|
|
.filter { [unowned self] in $0 == self.statusID }
|
2020-05-02 16:45:28 +00:00
|
|
|
.sink { [unowned self] in
|
2020-08-15 21:37:56 +00:00
|
|
|
if let mastodonController = mastodonController,
|
|
|
|
let status = mastodonController.persistentContainer.status(for: $0) {
|
2020-05-02 16:45:28 +00:00
|
|
|
self.updateStatusState(status: status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if accountUpdater == nil {
|
|
|
|
accountUpdater = mastodonController.persistentContainer.accountSubject
|
|
|
|
.receive(on: DispatchQueue.main)
|
2021-06-26 20:51:54 +00:00
|
|
|
.filter { [unowned self] in $0 == self.accountID }
|
2020-05-02 16:45:28 +00:00
|
|
|
.sink { [unowned self] in
|
2020-08-15 21:37:56 +00:00
|
|
|
if let mastodonController = mastodonController,
|
|
|
|
let account = mastodonController.persistentContainer.account(for: $0) {
|
2020-05-02 16:45:28 +00:00
|
|
|
self.updateUI(account: account)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 01:27:34 +00:00
|
|
|
}
|
2019-11-19 17:08:11 +00:00
|
|
|
|
2020-09-17 23:38:49 +00:00
|
|
|
final func updateUI(statusID: String, state: StatusState) {
|
2020-01-06 00:54:28 +00:00
|
|
|
createObserversIfNecessary()
|
|
|
|
|
2020-04-12 16:54:27 +00:00
|
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else {
|
2019-11-19 17:08:11 +00:00
|
|
|
fatalError("Missing cached status")
|
|
|
|
}
|
2020-09-17 23:38:49 +00:00
|
|
|
|
2019-08-02 03:01:15 +00:00
|
|
|
self.statusID = statusID
|
2020-09-17 23:38:49 +00:00
|
|
|
|
|
|
|
doUpdateUI(status: status, state: state)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doUpdateUI(status: StatusMO, state: StatusState) {
|
2019-11-28 23:36:58 +00:00
|
|
|
self.statusState = state
|
2018-08-18 03:09:59 +00:00
|
|
|
|
2018-10-31 02:07:54 +00:00
|
|
|
let account = status.account
|
2018-09-18 16:59:07 +00:00
|
|
|
self.accountID = account.id
|
2019-08-02 03:01:15 +00:00
|
|
|
updateUI(account: account)
|
2020-12-29 16:56:40 +00:00
|
|
|
contentTextView.setTextFrom(status: status)
|
2020-11-01 18:59:58 +00:00
|
|
|
updateGrayscaleableUI(account: account, status: status)
|
2020-09-17 23:38:49 +00:00
|
|
|
updateUIForPreferences(account: account, status: status)
|
2019-11-19 17:08:11 +00:00
|
|
|
|
2022-10-08 19:24:12 +00:00
|
|
|
cardView.updateUI(status: status)
|
2020-10-25 20:05:28 +00:00
|
|
|
cardView.isHidden = status.card == nil
|
2022-05-02 03:04:56 +00:00
|
|
|
cardView.navigationDelegate = delegate
|
|
|
|
cardView.actionProvider = delegate
|
2020-10-25 20:05:28 +00:00
|
|
|
|
2019-08-02 03:01:15 +00:00
|
|
|
attachmentsView.updateUI(status: status)
|
2019-11-19 17:08:11 +00:00
|
|
|
|
|
|
|
updateStatusState(status: status)
|
|
|
|
|
2019-11-29 02:22:13 +00:00
|
|
|
contentWarningLabel.text = status.spoilerText
|
|
|
|
contentWarningLabel.isHidden = status.spoilerText.isEmpty
|
2020-03-12 01:56:35 +00:00
|
|
|
if !contentWarningLabel.isHidden {
|
|
|
|
contentWarningLabel.setEmojis(status.emojis, identifier: statusID)
|
|
|
|
}
|
2019-11-29 02:22:13 +00:00
|
|
|
|
2020-06-17 02:47:30 +00:00
|
|
|
let reblogDisabled: Bool
|
2022-01-25 03:49:18 +00:00
|
|
|
if mastodonController.instanceFeatures.boostToOriginalAudience {
|
2020-06-17 02:47:30 +00:00
|
|
|
// Pleroma allows 'Boost to original audience' for your own private posts
|
|
|
|
reblogDisabled = status.visibility == .direct || (status.visibility == .private && status.account.id != mastodonController.account.id)
|
2022-01-25 03:49:18 +00:00
|
|
|
} else {
|
|
|
|
reblogDisabled = status.visibility == .private || status.visibility == .direct
|
2020-06-17 02:47:30 +00:00
|
|
|
}
|
2020-09-13 19:51:06 +00:00
|
|
|
reblogButton.isEnabled = !reblogDisabled && mastodonController.loggedIn
|
|
|
|
|
|
|
|
favoriteButton.isEnabled = mastodonController.loggedIn
|
|
|
|
replyButton.isEnabled = mastodonController.loggedIn
|
2020-06-17 22:00:13 +00:00
|
|
|
|
|
|
|
updateStatusIconsForPreferences(status)
|
2020-06-17 02:47:30 +00:00
|
|
|
|
2019-11-28 23:36:58 +00:00
|
|
|
if state.unknown {
|
2022-11-05 17:11:36 +00:00
|
|
|
layoutIfNeeded()
|
|
|
|
state.resolveFor(status: status, height: contentTextView.bounds.height)
|
2020-09-16 01:37:08 +00:00
|
|
|
if state.collapsible! && showStatusAutomatically {
|
|
|
|
state.collapsed = false
|
2019-11-28 23:36:58 +00:00
|
|
|
}
|
2019-11-17 23:36:19 +00:00
|
|
|
}
|
2020-09-16 01:37:08 +00:00
|
|
|
collapsible = state.collapsible!
|
|
|
|
setCollapsed(state.collapsed!, animated: false)
|
2019-08-02 03:01:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 16:54:27 +00:00
|
|
|
func updateStatusState(status: StatusMO) {
|
|
|
|
favorited = status.favourited
|
|
|
|
reblogged = status.reblogged
|
2019-09-27 00:53:22 +00:00
|
|
|
|
|
|
|
if favorited {
|
|
|
|
favoriteButton.accessibilityLabel = NSLocalizedString("Undo Favorite", comment: "undo favorite button accessibility label")
|
|
|
|
} else {
|
|
|
|
favoriteButton.accessibilityLabel = NSLocalizedString("Favorite", comment: "favorite button accessibility label")
|
|
|
|
}
|
|
|
|
if reblogged {
|
|
|
|
reblogButton.accessibilityLabel = NSLocalizedString("Undo Reblog", comment: "undo reblog button accessibility label")
|
|
|
|
} else {
|
|
|
|
reblogButton.accessibilityLabel = NSLocalizedString("Reblog", comment: "reblog button accessibility label")
|
|
|
|
}
|
2020-06-27 17:13:04 +00:00
|
|
|
|
2021-02-06 18:47:45 +00:00
|
|
|
// keep menu in sync with changed states e.g. bookmarked, muted
|
2021-06-09 21:10:44 +00:00
|
|
|
// do not include reply action here, because the cell already contains a button for it
|
2022-05-02 03:04:56 +00:00
|
|
|
moreButton.menu = UIMenu(title: "", image: nil, identifier: nil, options: [], children: delegate?.actionsForStatus(status, sourceView: moreButton, includeReply: false) ?? [])
|
2021-04-28 23:00:17 +00:00
|
|
|
|
2021-05-22 15:30:40 +00:00
|
|
|
pollView.isHidden = status.poll == nil
|
|
|
|
pollView.mastodonController = mastodonController
|
2022-05-13 14:00:11 +00:00
|
|
|
pollView.toastableViewController = delegate?.toastableViewController
|
2021-05-22 15:30:40 +00:00
|
|
|
pollView.updateUI(status: status, poll: status.poll)
|
2019-08-02 03:01:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 16:54:27 +00:00
|
|
|
func updateUI(account: AccountMO) {
|
2018-08-18 03:09:59 +00:00
|
|
|
usernameLabel.text = "@\(account.acct)"
|
2018-08-21 23:23:27 +00:00
|
|
|
avatarImageView.image = nil
|
2018-08-18 03:09:59 +00:00
|
|
|
}
|
2019-11-19 17:08:11 +00:00
|
|
|
|
2020-09-17 23:38:49 +00:00
|
|
|
@objc private func preferencesChanged() {
|
2020-06-17 22:00:13 +00:00
|
|
|
guard let mastodonController = mastodonController,
|
|
|
|
let account = mastodonController.persistentContainer.account(for: accountID),
|
|
|
|
let status = mastodonController.persistentContainer.status(for: statusID) else { return }
|
2020-09-17 23:38:49 +00:00
|
|
|
updateUIForPreferences(account: account, status: status)
|
2020-06-16 03:22:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 23:38:49 +00:00
|
|
|
func updateUIForPreferences(account: AccountMO, status: StatusMO) {
|
2019-08-03 00:05:47 +00:00
|
|
|
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
|
2022-11-05 17:20:55 +00:00
|
|
|
if Preferences.shared.blurAllMedia {
|
|
|
|
attachmentsView.contentHidden = true
|
|
|
|
} else if status.sensitive {
|
|
|
|
if !Preferences.shared.blurMediaBehindContentWarning && !status.spoilerText.isEmpty {
|
|
|
|
attachmentsView.contentHidden = false
|
|
|
|
} else {
|
|
|
|
attachmentsView.contentHidden = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
attachmentsView.contentHidden = false
|
|
|
|
}
|
2020-09-17 23:38:49 +00:00
|
|
|
|
|
|
|
updateStatusIconsForPreferences(status)
|
2020-11-01 18:59:58 +00:00
|
|
|
|
|
|
|
if isGrayscale != Preferences.shared.grayscaleImages {
|
|
|
|
updateGrayscaleableUI(account: account, status: status)
|
|
|
|
}
|
2019-08-03 00:05:47 +00:00
|
|
|
}
|
2018-08-21 23:23:27 +00:00
|
|
|
|
2020-06-17 22:00:13 +00:00
|
|
|
func updateStatusIconsForPreferences(_ status: StatusMO) {
|
2022-01-23 15:58:36 +00:00
|
|
|
metaIndicatorsView.updateUI(status: status)
|
|
|
|
|
2020-06-17 22:00:13 +00:00
|
|
|
let reblogButtonImage: UIImage
|
|
|
|
if Preferences.shared.alwaysShowStatusVisibilityIcon || reblogButton.isEnabled {
|
|
|
|
reblogButtonImage = UIImage(systemName: "repeat")!
|
|
|
|
} else {
|
|
|
|
reblogButtonImage = UIImage(systemName: status.visibility.imageName)!
|
|
|
|
}
|
|
|
|
reblogButton.setImage(reblogButtonImage, for: .normal)
|
|
|
|
}
|
|
|
|
|
2020-11-01 18:59:58 +00:00
|
|
|
func updateGrayscaleableUI(account: AccountMO, status: StatusMO) {
|
|
|
|
isGrayscale = Preferences.shared.grayscaleImages
|
|
|
|
|
|
|
|
let accountID = account.id
|
2022-02-17 03:12:47 +00:00
|
|
|
if let avatarURL = account.avatar {
|
|
|
|
avatarRequest = ImageCache.avatars.get(avatarURL) { [weak self] (_, image) in
|
|
|
|
guard let self = self,
|
|
|
|
let image = image,
|
|
|
|
self.accountID == accountID,
|
|
|
|
let transformedImage = ImageGrayscalifier.convertIfNecessary(url: avatarURL, image: image) else { return }
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.avatarImageView.image = transformedImage
|
|
|
|
}
|
2020-11-01 18:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 16:56:40 +00:00
|
|
|
if contentTextView.hasEmojis {
|
|
|
|
contentTextView.setTextFrom(status: status)
|
|
|
|
}
|
2020-11-01 18:59:58 +00:00
|
|
|
|
|
|
|
displayNameLabel.updateForAccountDisplayName(account: account)
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:42:29 +00:00
|
|
|
func setShowThreadLinks(prev: Bool, next: Bool) {
|
|
|
|
if prev {
|
|
|
|
if let prevThreadLinkView = prevThreadLinkView {
|
|
|
|
prevThreadLinkView.isHidden = false
|
|
|
|
} else {
|
|
|
|
let view = UIView()
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.backgroundColor = tintColor.withAlphaComponent(0.5)
|
|
|
|
view.layer.cornerRadius = 2.5
|
|
|
|
view.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
|
|
|
|
prevThreadLinkView = view
|
|
|
|
addSubview(view)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
view.widthAnchor.constraint(equalToConstant: 5),
|
|
|
|
view.centerXAnchor.constraint(equalTo: avatarImageView.centerXAnchor),
|
|
|
|
view.topAnchor.constraint(equalTo: topAnchor),
|
|
|
|
view.bottomAnchor.constraint(equalTo: avatarImageView.topAnchor, constant: -2),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prevThreadLinkView?.isHidden = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if next {
|
|
|
|
if let nextThreadLinkView = nextThreadLinkView {
|
|
|
|
nextThreadLinkView.isHidden = false
|
|
|
|
} else {
|
|
|
|
let view = UIView()
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.backgroundColor = tintColor.withAlphaComponent(0.5)
|
|
|
|
view.layer.cornerRadius = 2.5
|
|
|
|
view.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
|
|
|
|
nextThreadLinkView = view
|
|
|
|
addSubview(view)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
view.widthAnchor.constraint(equalToConstant: 5),
|
|
|
|
view.centerXAnchor.constraint(equalTo: avatarImageView.centerXAnchor),
|
|
|
|
view.topAnchor.constraint(equalTo: avatarImageView.bottomAnchor, constant: 2),
|
|
|
|
view.bottomAnchor.constraint(equalTo: bottomAnchor),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nextThreadLinkView?.isHidden = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 23:23:27 +00:00
|
|
|
override func prepareForReuse() {
|
2019-11-19 17:08:11 +00:00
|
|
|
super.prepareForReuse()
|
|
|
|
|
2020-01-25 15:06:27 +00:00
|
|
|
avatarRequest?.cancel()
|
2019-11-17 17:52:42 +00:00
|
|
|
attachmentsView.attachmentViews.allObjects.forEach { $0.removeFromSuperview() }
|
2019-11-19 03:23:15 +00:00
|
|
|
showStatusAutomatically = false
|
2018-08-21 23:23:27 +00:00
|
|
|
}
|
2021-04-28 23:11:41 +00:00
|
|
|
|
|
|
|
// MARK: - Interaction
|
2018-08-17 02:39:16 +00:00
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
@IBAction func collapseButtonPressed() {
|
2019-09-09 22:40:23 +00:00
|
|
|
setCollapsed(!collapsed, animated: true)
|
2022-06-27 21:25:22 +00:00
|
|
|
if #available(iOS 16.0, *) {
|
|
|
|
invalidateIntrinsicContentSize()
|
|
|
|
} else {
|
|
|
|
delegate?.statusCellCollapsedStateChanged(self)
|
|
|
|
}
|
2019-09-09 22:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setCollapsed(_ collapsed: Bool, animated: Bool) {
|
|
|
|
self.collapsed = collapsed
|
2019-11-19 17:08:11 +00:00
|
|
|
|
2020-01-18 21:00:38 +00:00
|
|
|
contentTextView.isHidden = collapsed
|
2020-10-25 20:05:28 +00:00
|
|
|
cardView.isHidden = cardView.card == nil || collapsed
|
2019-09-09 22:40:23 +00:00
|
|
|
attachmentsView.isHidden = attachmentsView.attachments.count == 0 || collapsed
|
2021-05-22 15:30:40 +00:00
|
|
|
pollView.isHidden = pollView.poll == nil || collapsed
|
2019-11-19 17:08:11 +00:00
|
|
|
|
|
|
|
let buttonImage = UIImage(systemName: collapsed ? "chevron.down" : "chevron.up")!
|
2019-09-09 22:40:23 +00:00
|
|
|
|
|
|
|
if animated, let buttonImageView = collapseButton.imageView {
|
|
|
|
// we need to use a keyframe animation for this, because we want to control the direction the chevron rotates
|
|
|
|
// when rotating ±π, UIKit will always rotate in the same direction
|
|
|
|
// using a keyframe to set an intermediate point in the animation allows us to force a specific direction
|
|
|
|
UIView.animateKeyframes(withDuration: 0.2, delay: 0, options: .calculationModeLinear, animations: {
|
|
|
|
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) {
|
|
|
|
buttonImageView.transform = CGAffineTransform(rotationAngle: collapsed ? .pi / 2 : -.pi / 2)
|
|
|
|
}
|
|
|
|
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
|
|
|
|
buttonImageView.transform = CGAffineTransform(rotationAngle: .pi)
|
|
|
|
}
|
|
|
|
}, completion: { (finished) in
|
|
|
|
buttonImageView.transform = .identity
|
|
|
|
self.collapseButton.setImage(buttonImage, for: .normal)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
collapseButton.setImage(buttonImage, for: .normal)
|
|
|
|
}
|
2019-09-27 00:53:22 +00:00
|
|
|
|
|
|
|
if collapsed {
|
|
|
|
collapseButton.accessibilityLabel = NSLocalizedString("Expand Status", comment: "expand status button accessibility label")
|
|
|
|
} else {
|
|
|
|
collapseButton.accessibilityLabel = NSLocalizedString("Collapse Status", comment: "collapse status button accessibility label")
|
|
|
|
}
|
2019-11-19 17:08:11 +00:00
|
|
|
|
2019-09-09 22:40:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
@IBAction func replyPressed() {
|
2020-08-31 23:28:50 +00:00
|
|
|
delegate?.compose(inReplyToID: statusID)
|
2018-08-31 02:30:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
@IBAction func favoritePressed() {
|
2020-05-02 23:52:35 +00:00
|
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
2018-09-18 01:57:46 +00:00
|
|
|
|
2022-10-08 16:15:12 +00:00
|
|
|
Task {
|
|
|
|
await FavoriteService(status: status, mastodonController: mastodonController, presenter: delegate!).toggleFavorite()
|
2018-09-09 01:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
@IBAction func reblogPressed() {
|
2020-05-02 23:52:35 +00:00
|
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
2018-09-18 01:57:46 +00:00
|
|
|
|
2022-10-08 16:15:12 +00:00
|
|
|
Task {
|
|
|
|
await ReblogService(status: status, mastodonController: mastodonController, presenter: delegate!).toggleReblog()
|
2018-09-09 01:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
@IBAction func morePressed() {
|
2020-01-18 02:29:53 +00:00
|
|
|
delegate?.showMoreOptions(forStatus: statusID, sourceView: moreButton)
|
2018-09-11 22:17:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
@objc func accountPressed() {
|
|
|
|
delegate?.selected(account: accountID)
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 17:08:11 +00:00
|
|
|
extension BaseStatusTableViewCell: AttachmentViewDelegate {
|
2020-10-17 16:56:13 +00:00
|
|
|
func attachmentViewGallery(startingAt index: Int) -> GalleryViewController? {
|
2020-09-13 17:55:33 +00:00
|
|
|
guard let delegate = delegate,
|
|
|
|
let status = mastodonController.persistentContainer.status(for: statusID) else { return nil }
|
2019-06-17 02:39:46 +00:00
|
|
|
let sourceViews = status.attachments.map(attachmentsView.getAttachmentView(for:))
|
2020-10-17 16:56:13 +00:00
|
|
|
let gallery = delegate.gallery(attachments: status.attachments, sourceViews: sourceViews, startIndex: index)
|
|
|
|
gallery.avPlayerViewControllerDelegate = self
|
|
|
|
return gallery
|
2020-03-21 02:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func attachmentViewPresent(_ vc: UIViewController, animated: Bool) {
|
2020-10-17 16:56:13 +00:00
|
|
|
delegate?.present(vc, animated: animated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: This is not ideal. It works when the original cell remains visible and when the cell is reused, but if the cell is dealloc'd
|
|
|
|
// resuming from PiP won't work because AVPlayerViewController.delegate is a weak reference.
|
|
|
|
extension BaseStatusTableViewCell: AVPlayerViewControllerDelegate {
|
|
|
|
func playerViewControllerWillStartPictureInPicture(_ playerViewController: AVPlayerViewController) {
|
|
|
|
// We need to save the current statusID when PiP is initiated, because if the user restores from PiP after this cell has
|
|
|
|
// been reused, the current value of statusID will not be correct.
|
|
|
|
currentPictureInPictureVideoStatusID = statusID
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewControllerDidStopPictureInPicture(_ playerViewController: AVPlayerViewController) {
|
|
|
|
currentPictureInPictureVideoStatusID = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool {
|
|
|
|
// Ideally, when PiP is automatically initiated by app closing the gallery should not be dismissed
|
|
|
|
// and when PiP is started because the user has tapped the button in the player controls the gallery
|
|
|
|
// gallery should be dismissed. Unfortunately, this doesn't seem to be possible. Instead, the gallery is
|
|
|
|
// always dismissed and is recreated when restoring the interface from PiP.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func playerViewController(_ playerViewController: AVPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
|
|
|
|
guard let delegate = delegate,
|
|
|
|
let playerViewController = playerViewController as? GalleryPlayerViewController,
|
|
|
|
let id = currentPictureInPictureVideoStatusID,
|
|
|
|
let status = mastodonController.persistentContainer.status(for: id),
|
|
|
|
let index = status.attachments.firstIndex(where: { $0.id == playerViewController.attachment?.id }) else {
|
|
|
|
// returning without invoking completionHandler will dismiss the PiP window
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// We create a new gallery view controller starting at the appropriate index and swap the
|
|
|
|
// already-playing VC into the appropriate index so it smoothly continues playing.
|
|
|
|
|
|
|
|
let sourceViews: [UIImageView?]
|
|
|
|
if self.statusID == id {
|
|
|
|
sourceViews = status.attachments.map(attachmentsView.getAttachmentView(for:))
|
|
|
|
} else {
|
|
|
|
sourceViews = status.attachments.map { (_) in nil }
|
|
|
|
}
|
|
|
|
let gallery = delegate.gallery(attachments: status.attachments, sourceViews: sourceViews, startIndex: index)
|
|
|
|
gallery.avPlayerViewControllerDelegate = self
|
|
|
|
|
|
|
|
// ensure that all other page VCs are created
|
|
|
|
gallery.loadViewIfNeeded()
|
|
|
|
// replace the newly created player for the same attachment with the already-playing one
|
|
|
|
gallery.pages[index] = playerViewController
|
|
|
|
gallery.setViewControllers([playerViewController], direction: .forward, animated: false, completion: nil)
|
|
|
|
|
|
|
|
// this isn't animated, otherwise the animation plays first and then the PiP window expands
|
|
|
|
// which looks even weirder than the black background appearing instantly and then the PiP window animating
|
|
|
|
delegate.present(gallery, animated: false) {
|
|
|
|
completionHandler(false)
|
|
|
|
}
|
2018-09-02 20:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 01:20:58 +00:00
|
|
|
|
2020-12-14 03:37:37 +00:00
|
|
|
extension BaseStatusTableViewCell: UIDragInteractionDelegate {
|
|
|
|
func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
|
2020-12-14 23:44:41 +00:00
|
|
|
guard let currentAccountID = mastodonController.accountInfo?.id,
|
|
|
|
let account = mastodonController.persistentContainer.account(for: accountID) else {
|
2020-12-14 03:37:37 +00:00
|
|
|
return []
|
|
|
|
}
|
2020-12-14 23:44:41 +00:00
|
|
|
let provider = NSItemProvider(object: account.url as NSURL)
|
|
|
|
let activity = UserActivityManager.showProfileActivity(id: accountID, accountID: currentAccountID)
|
2022-05-13 21:10:18 +00:00
|
|
|
activity.displaysAuxiliaryScene = true
|
2020-12-14 23:44:41 +00:00
|
|
|
provider.registerObject(activity, visibility: .all)
|
2020-12-14 03:37:37 +00:00
|
|
|
return [UIDragItem(itemProvider: provider)]
|
|
|
|
}
|
|
|
|
}
|