forked from shadowfacts/Tusker
360 lines
15 KiB
Swift
360 lines
15 KiB
Swift
//
|
|
// BaseStatusTableViewCell.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 11/19/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Pachyderm
|
|
import Combine
|
|
|
|
protocol StatusTableViewCellDelegate: TuskerNavigationDelegate {
|
|
func statusCellCollapsedStateChanged(_ cell: BaseStatusTableViewCell)
|
|
}
|
|
|
|
class BaseStatusTableViewCell: UITableViewCell {
|
|
|
|
weak var delegate: StatusTableViewCellDelegate? {
|
|
didSet {
|
|
contentTextView.navigationDelegate = delegate
|
|
}
|
|
}
|
|
var overrideMastodonController: MastodonController?
|
|
var mastodonController: MastodonController! { overrideMastodonController ?? delegate?.apiController }
|
|
|
|
@IBOutlet weak var avatarImageView: UIImageView!
|
|
@IBOutlet weak var displayNameLabel: EmojiLabel!
|
|
@IBOutlet weak var usernameLabel: UILabel!
|
|
@IBOutlet weak var visibilityImageView: UIImageView!
|
|
@IBOutlet weak var contentWarningLabel: EmojiLabel!
|
|
@IBOutlet weak var collapseButton: UIButton!
|
|
@IBOutlet weak var contentTextView: StatusContentTextView!
|
|
@IBOutlet weak var attachmentsView: AttachmentsContainerView!
|
|
@IBOutlet weak var replyButton: UIButton!
|
|
@IBOutlet weak var favoriteButton: UIButton!
|
|
@IBOutlet weak var reblogButton: UIButton!
|
|
@IBOutlet weak var moreButton: UIButton!
|
|
|
|
var statusID: String!
|
|
var accountID: String!
|
|
|
|
var favorited = false {
|
|
didSet {
|
|
favoriteButton.tintColor = favorited ? UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) : tintColor
|
|
}
|
|
}
|
|
var reblogged = false {
|
|
didSet {
|
|
reblogButton.tintColor = reblogged ? UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) : tintColor
|
|
}
|
|
}
|
|
|
|
var statusState: StatusState!
|
|
var collapsible = false {
|
|
didSet {
|
|
collapseButton.isHidden = !collapsible
|
|
statusState?.collapsible = collapsible
|
|
}
|
|
}
|
|
var collapsed = false {
|
|
didSet {
|
|
statusState?.collapsed = collapsed
|
|
}
|
|
}
|
|
var showStatusAutomatically = false
|
|
|
|
private var avatarRequest: ImageCache.Request?
|
|
|
|
private var statusUpdater: Cancellable?
|
|
private var accountUpdater: Cancellable?
|
|
|
|
override func awakeFromNib() {
|
|
super.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
|
|
|
|
collapseButton.layer.masksToBounds = true
|
|
collapseButton.layer.cornerRadius = 5
|
|
|
|
accessibilityElements = [displayNameLabel!, contentWarningLabel!, collapseButton!, contentTextView!, attachmentsView!]
|
|
attachmentsView.isAccessibilityElement = true
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
|
|
}
|
|
|
|
open func createObserversIfNecessary() {
|
|
if statusUpdater == nil {
|
|
statusUpdater = mastodonController.persistentContainer.statusSubject
|
|
.filter { [unowned self] in $0 == self.statusID }
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [unowned self] in
|
|
if let status = self.mastodonController.persistentContainer.status(for: $0) {
|
|
self.updateStatusState(status: status)
|
|
}
|
|
}
|
|
}
|
|
|
|
if accountUpdater == nil {
|
|
accountUpdater = mastodonController.persistentContainer.accountSubject
|
|
.filter { [unowned self] in $0 == self.accountID }
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [unowned self] in
|
|
if let account = self.mastodonController.persistentContainer.account(for: $0) {
|
|
self.updateUI(account: account)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func updateUI(statusID: String, state: StatusState) {
|
|
createObserversIfNecessary()
|
|
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else {
|
|
fatalError("Missing cached status")
|
|
}
|
|
self.statusID = statusID
|
|
self.statusState = state
|
|
|
|
let account = status.account
|
|
self.accountID = account.id
|
|
updateUI(account: account)
|
|
updateUIForPreferences(account: account)
|
|
|
|
visibilityImageView.image = UIImage(systemName: status.visibility.unfilledImageName)
|
|
visibilityImageView.accessibilityLabel = String(format: NSLocalizedString("Visibility: %@", comment: "status visibility indicator accessibility label"), status.visibility.displayName)
|
|
|
|
attachmentsView.updateUI(status: status)
|
|
attachmentsView.isAccessibilityElement = status.attachments.count > 0
|
|
attachmentsView.accessibilityLabel = String(format: NSLocalizedString("%d attachments", comment: "status attachments count accessibility label"), status.attachments.count)
|
|
|
|
updateStatusState(status: status)
|
|
|
|
contentTextView.setTextFrom(status: status)
|
|
|
|
contentWarningLabel.text = status.spoilerText
|
|
contentWarningLabel.isHidden = status.spoilerText.isEmpty
|
|
if !contentWarningLabel.isHidden {
|
|
contentWarningLabel.setEmojis(status.emojis, identifier: statusID)
|
|
}
|
|
|
|
let reblogDisabled: Bool
|
|
switch mastodonController.instance.instanceType {
|
|
case .mastodon:
|
|
reblogDisabled = status.visibility == .private || status.visibility == .direct
|
|
case .pleroma:
|
|
// Pleroma allows 'Boost to original audience' for your own private posts
|
|
reblogDisabled = status.visibility == .direct || (status.visibility == .private && status.account.id != mastodonController.account.id)
|
|
}
|
|
reblogButton.isEnabled = !reblogDisabled
|
|
let reblogButtonImage = reblogDisabled ? UIImage(systemName: status.visibility.imageName) : UIImage(systemName: "repeat")
|
|
reblogButton.setImage(reblogButtonImage, for: .normal)
|
|
|
|
if state.unknown {
|
|
collapsible = !status.spoilerText.isEmpty
|
|
var shouldCollapse = collapsible
|
|
if !shouldCollapse,
|
|
let text = contentTextView.text,
|
|
text.count > 500 {
|
|
collapsible = true
|
|
shouldCollapse = true
|
|
}
|
|
if collapsible && showStatusAutomatically {
|
|
shouldCollapse = false
|
|
}
|
|
setCollapsed(shouldCollapse, animated: false)
|
|
|
|
state.collapsible = collapsible
|
|
state.collapsed = shouldCollapse
|
|
} else {
|
|
collapsible = state.collapsible!
|
|
setCollapsed(state.collapsed!, animated: false)
|
|
}
|
|
}
|
|
|
|
func updateStatusState(status: StatusMO) {
|
|
favorited = status.favourited
|
|
reblogged = status.reblogged
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
func updateUI(account: AccountMO) {
|
|
usernameLabel.text = "@\(account.acct)"
|
|
avatarImageView.image = nil
|
|
avatarRequest = ImageCache.avatars.get(account.avatar) { [weak self] (data) in
|
|
DispatchQueue.main.async {
|
|
guard let self = self, let data = data, self.accountID == account.id else { return }
|
|
self.avatarImageView.image = UIImage(data: data)
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc func preferencesChanged() {
|
|
guard let mastodonController = mastodonController, let account = mastodonController.persistentContainer.account(for: accountID) else { return }
|
|
updateUIForPreferences(account: account)
|
|
}
|
|
|
|
func updateUIForPreferences(account: AccountMO) {
|
|
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
|
|
displayNameLabel.updateForAccountDisplayName(account: account)
|
|
attachmentsView.contentHidden = Preferences.shared.blurAllMedia || (mastodonController.persistentContainer.status(for: statusID)?.sensitive ?? false)
|
|
}
|
|
|
|
override func prepareForReuse() {
|
|
super.prepareForReuse()
|
|
|
|
avatarRequest?.cancel()
|
|
attachmentsView.attachmentViews.allObjects.forEach { $0.removeFromSuperview() }
|
|
showStatusAutomatically = false
|
|
}
|
|
|
|
@IBAction func collapseButtonPressed() {
|
|
setCollapsed(!collapsed, animated: true)
|
|
delegate?.statusCellCollapsedStateChanged(self)
|
|
}
|
|
|
|
func setCollapsed(_ collapsed: Bool, animated: Bool) {
|
|
self.collapsed = collapsed
|
|
|
|
contentTextView.isHidden = collapsed
|
|
attachmentsView.isHidden = attachmentsView.attachments.count == 0 || collapsed
|
|
|
|
let buttonImage = UIImage(systemName: collapsed ? "chevron.down" : "chevron.up")!
|
|
|
|
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)
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
}
|
|
|
|
@IBAction func replyPressed() {
|
|
delegate?.reply(to: statusID)
|
|
}
|
|
|
|
@IBAction func favoritePressed() {
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
|
|
|
let oldValue = favorited
|
|
favorited = !favorited
|
|
|
|
let realStatus = status.reblog ?? status
|
|
let request = (favorited ? Status.favourite : Status.unfavourite)(realStatus.id)
|
|
mastodonController.run(request) { response in
|
|
DispatchQueue.main.async {
|
|
if case let .success(newStatus, _) = response {
|
|
self.favorited = newStatus.favourited ?? false
|
|
self.mastodonController.persistentContainer.addOrUpdate(status: newStatus, incrementReferenceCount: false)
|
|
UIImpactFeedbackGenerator(style: .light).impactOccurred()
|
|
} else {
|
|
self.favorited = oldValue
|
|
print("Couldn't favorite status \(realStatus.id)")
|
|
// todo: display error message
|
|
UINotificationFeedbackGenerator().notificationOccurred(.error)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction func reblogPressed() {
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
|
|
|
let oldValue = reblogged
|
|
reblogged = !reblogged
|
|
|
|
let realStatus = status.reblog ?? status
|
|
let request = (reblogged ? Status.reblog : Status.unreblog)(realStatus.id)
|
|
mastodonController.run(request) { response in
|
|
DispatchQueue.main.async {
|
|
if case let .success(newStatus, _) = response {
|
|
self.reblogged = newStatus.reblogged ?? false
|
|
self.mastodonController.persistentContainer.addOrUpdate(status: newStatus, incrementReferenceCount: false)
|
|
UIImpactFeedbackGenerator(style: .light).impactOccurred()
|
|
} else {
|
|
self.reblogged = oldValue
|
|
print("Couldn't reblog status \(realStatus.id)")
|
|
// todo: display error message
|
|
UINotificationFeedbackGenerator().notificationOccurred(.error)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction func morePressed() {
|
|
delegate?.showMoreOptions(forStatus: statusID, sourceView: moreButton)
|
|
}
|
|
|
|
@objc func accountPressed() {
|
|
delegate?.selected(account: accountID)
|
|
}
|
|
|
|
func getStatusCellPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
extension BaseStatusTableViewCell: AttachmentViewDelegate {
|
|
func attachmentViewGallery(startingAt index: Int) -> UIViewController {
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
|
|
let sourceViews = status.attachments.map(attachmentsView.getAttachmentView(for:))
|
|
return delegate!.gallery(attachments: status.attachments, sourceViews: sourceViews, startIndex: index)
|
|
}
|
|
|
|
func attachmentViewPresent(_ vc: UIViewController, animated: Bool) {
|
|
delegate?.show(vc)
|
|
}
|
|
}
|
|
|
|
extension BaseStatusTableViewCell: MenuPreviewProvider {
|
|
var navigationDelegate: TuskerNavigationDelegate? { return delegate }
|
|
|
|
func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? {
|
|
guard let mastodonController = mastodonController else { return nil }
|
|
if avatarImageView.frame.contains(location) {
|
|
return (
|
|
content: { ProfileTableViewController(accountID: self.accountID, mastodonController: mastodonController) },
|
|
actions: { self.actionsForProfile(accountID: self.accountID, sourceView: self.avatarImageView) }
|
|
)
|
|
}
|
|
return self.getStatusCellPreviewProviders(for: location, sourceViewController: sourceViewController)
|
|
}
|
|
}
|