Tusker/Tusker/Views/Status/StatusTableViewCell.swift

370 lines
15 KiB
Swift
Raw Normal View History

2018-08-17 02:39:16 +00:00
//
// StatusTableViewCell.swift
// Tusker
//
// Created by Shadowfacts on 8/16/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
2018-09-11 14:52:21 +00:00
import Pachyderm
2018-08-17 02:39:16 +00:00
protocol StatusTableViewCellDelegate: TuskerNavigationDelegate {
}
2018-08-28 23:49:31 +00:00
class StatusTableViewCell: UITableViewCell, PreferencesAdaptive {
2018-08-17 02:39:16 +00:00
var delegate: StatusTableViewCellDelegate?
2018-08-17 02:39:16 +00:00
@IBOutlet weak var displayNameLabel: UILabel!
@IBOutlet weak var usernameLabel: UILabel!
2018-08-26 18:49:22 +00:00
@IBOutlet weak var contentLabel: StatusContentLabel!
2018-08-21 23:23:27 +00:00
@IBOutlet weak var avatarImageView: UIImageView!
2018-08-29 00:11:31 +00:00
@IBOutlet weak var reblogLabel: UILabel!
2018-08-31 17:46:33 +00:00
@IBOutlet weak var timestampLabel: UILabel!
2018-09-02 20:59:20 +00:00
@IBOutlet weak var attachmentsView: UIView!
2018-09-09 01:35:40 +00:00
@IBOutlet weak var favoriteButton: UIButton!
@IBOutlet weak var reblogButton: UIButton!
2018-08-17 02:39:16 +00:00
2018-09-18 01:57:46 +00:00
var statusID: String!
2018-09-18 16:59:07 +00:00
var accountID: String!
var rebloggerID: String?
2018-09-09 01:35:40 +00:00
var favorited: Bool = false {
didSet {
favoriteButton.tintColor = favorited ? UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) : tintColor
}
}
var reblogged: Bool = false {
didSet {
reblogButton.tintColor = reblogged ? UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) : tintColor
}
}
2018-08-21 23:23:27 +00:00
var avatarURL: URL?
2018-08-31 17:46:33 +00:00
var updateTimestampWorkItem: DispatchWorkItem?
2018-09-02 20:59:20 +00:00
var attachmentDataTasks: [URLSessionDataTask] = []
2018-08-28 01:27:34 +00:00
override func awakeFromNib() {
displayNameLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
displayNameLabel.isUserInteractionEnabled = true
usernameLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
usernameLabel.isUserInteractionEnabled = true
2018-08-29 00:11:31 +00:00
reblogLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(reblogLabelPressed)))
reblogLabel.isUserInteractionEnabled = true
2018-08-28 01:27:34 +00:00
avatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(accountPressed)))
avatarImageView.isUserInteractionEnabled = true
avatarImageView.layer.masksToBounds = true
2018-09-02 20:59:20 +00:00
attachmentsView.layer.cornerRadius = 5
attachmentsView.layer.masksToBounds = true
contentLabel.navigationDelegate = self
2018-08-28 01:27:34 +00:00
}
2018-08-28 23:49:31 +00:00
func updateUIForPreferences() {
2018-09-18 16:59:07 +00:00
guard let account = MastodonCache.account(for: accountID) else { fatalError("") }
2018-08-28 23:49:31 +00:00
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView)
2018-09-18 16:59:07 +00:00
if let rebloggerID = rebloggerID,
let reblogger = MastodonCache.account(for: rebloggerID) {
reblogLabel.text = "Reblogged by \(reblogger.realDisplayName)"
}
displayNameLabel.text = account.realDisplayName
2018-08-28 23:49:31 +00:00
}
2018-09-18 01:57:46 +00:00
func updateUI(for statusID: String) {
self.statusID = statusID
2018-09-18 16:59:07 +00:00
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") }
let account: Account
if let reblog = status.reblog {
account = reblog.account
2018-09-18 16:59:07 +00:00
rebloggerID = status.account.id
2018-08-29 00:11:31 +00:00
reblogLabel.isHidden = false
} else {
account = status.account
2018-08-29 00:11:31 +00:00
reblogLabel.isHidden = true
2018-09-18 16:59:07 +00:00
rebloggerID = nil
}
2018-09-18 16:59:07 +00:00
self.accountID = account.id
2018-08-28 01:27:34 +00:00
2018-08-28 23:49:31 +00:00
updateUIForPreferences()
usernameLabel.text = "@\(account.acct)"
2018-08-21 23:23:27 +00:00
avatarImageView.image = nil
2018-09-11 14:52:21 +00:00
avatarURL = account.avatar
ImageCache.avatars.get(account.avatar) { (image) in
2018-09-11 14:52:21 +00:00
DispatchQueue.main.async {
self.avatarImageView.image = image
self.avatarURL = nil
2018-08-21 23:23:27 +00:00
}
}
2018-08-31 17:46:33 +00:00
updateTimestamp()
2018-08-21 23:23:27 +00:00
2018-09-11 14:52:21 +00:00
let attachments = status.attachments.filter({ $0.kind == .image })
2018-09-02 20:59:20 +00:00
if attachments.count > 0 {
attachmentsView.isHidden = false
let width = attachmentsView.bounds.width
let height: CGFloat = 200
switch attachments.count {
case 1:
addAttachmentView(frame: CGRect(x: 0, y: 0, width: width, height: height), attachment: attachments[0])
case 2:
addAttachmentView(frame: CGRect(x: 0, y: 0, width: width / 2 - 4, height: height), attachment: attachments[0])
addAttachmentView(frame: CGRect(x: width / 2 + 4, y: 0, width: width / 2 - 4, height: height), attachment: attachments[1])
case 3:
addAttachmentView(frame: CGRect(x: 0, y: 0, width: width / 2 - 4, height: height), attachment: attachments[0])
addAttachmentView(frame: CGRect(x: width / 2 + 4, y: 0, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[1])
addAttachmentView(frame: CGRect(x: width / 2 + 4, y: height / 2 + 4, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[2])
case 4:
addAttachmentView(frame: CGRect(x: 0, y: 0, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[0])
addAttachmentView(frame: CGRect(x: 0, y: height / 2 + 4, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[1])
addAttachmentView(frame: CGRect(x: width / 2 + 4, y: 0, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[2])
addAttachmentView(frame: CGRect(x: width / 2 + 4, y: height / 2 + 4, width: width / 2 - 4, height: height / 2 - 4), attachment: attachments[3])
default:
fatalError("Too many attachments")
}
} else {
attachmentsView.isHidden = true
}
2018-09-09 01:35:40 +00:00
let realStatus = status.reblog ?? status
favorited = realStatus.favourited ?? false
reblogged = realStatus.reblogged ?? false
2018-09-18 01:57:46 +00:00
contentLabel.statusID = statusID
}
2018-08-21 23:23:27 +00:00
2018-08-31 17:46:33 +00:00
func updateTimestamp() {
2018-09-18 16:59:07 +00:00
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
2018-09-18 01:57:46 +00:00
2018-08-31 17:46:33 +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 = delay {
updateTimestampWorkItem = DispatchWorkItem {
self.updateTimestamp()
}
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
} else {
updateTimestampWorkItem = nil
}
}
2018-09-02 20:59:20 +00:00
func addAttachmentView(frame: CGRect, attachment: Attachment) {
let attachmentView = AttachmentView(frame: frame, attachment: attachment)
attachmentView.delegate = self
attachmentsView.addSubview(attachmentView)
}
2018-08-21 23:23:27 +00:00
override func prepareForReuse() {
if let url = avatarURL {
ImageCache.avatars.cancel(url)
2018-08-21 23:23:27 +00:00
}
2018-08-31 17:46:33 +00:00
updateTimestampWorkItem?.cancel()
updateTimestampWorkItem = nil
2018-09-02 20:59:20 +00:00
attachmentsView.subviews.forEach { view in
(view as? AttachmentView)?.task?.cancel()
2018-09-03 20:54:03 +00:00
view.removeFromSuperview()
2018-09-02 20:59:20 +00:00
}
2018-08-21 23:23:27 +00:00
}
2018-08-17 02:39:16 +00:00
2018-09-03 20:54:03 +00:00
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
2018-09-18 01:57:46 +00:00
delegate?.selected(status: statusID)
2018-09-03 20:54:03 +00:00
}
}
2018-08-31 02:30:19 +00:00
@IBAction func replyPressed(_ sender: Any) {
2018-09-18 01:57:46 +00:00
delegate?.reply(to: statusID)
2018-08-31 02:30:19 +00:00
}
2018-08-28 01:27:34 +00:00
@objc func accountPressed() {
2018-09-18 16:59:07 +00:00
delegate?.selected(account: accountID)
2018-08-28 01:27:34 +00:00
}
2018-08-29 00:11:31 +00:00
@objc func reblogLabelPressed() {
2018-09-18 16:59:07 +00:00
guard let rebloggerID = rebloggerID else { return }
delegate?.selected(account: rebloggerID)
2018-08-29 00:11:31 +00:00
}
2018-09-09 01:35:40 +00:00
@IBAction func favoritePressed(_ sender: Any) {
2018-09-18 16:59:07 +00:00
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
2018-09-18 01:57:46 +00:00
let oldValue = favorited
2018-09-09 01:35:40 +00:00
favorited = !favorited
let realStatus: Status = status.reblog ?? status
let request = (favorited ? Status.favourite : Status.unfavourite)(realStatus)
MastodonController.shared.client.run(request) { response in
2018-09-09 01:35:40 +00:00
DispatchQueue.main.async {
self.favorited = realStatus.favourited ?? false
2018-09-11 14:52:21 +00:00
if case .success = response {
self.favorited = realStatus.favourited ?? false
2018-09-11 14:52:21 +00:00
UIImpactFeedbackGenerator(style: .light).impactOccurred()
} else {
self.favorited = oldValue
2018-09-11 14:52:21 +00:00
print("Couldn't favorite status \(realStatus.id)")
// todo: display error message
UINotificationFeedbackGenerator().notificationOccurred(.error)
return
}
2018-09-09 01:35:40 +00:00
}
}
}
@IBAction func reblogPressed(_ sender: Any) {
2018-09-18 16:59:07 +00:00
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
2018-09-18 01:57:46 +00:00
let oldValue = reblogged
2018-09-09 01:35:40 +00:00
reblogged = !reblogged
let realStatus: Status = status.reblog ?? status
let request = (reblogged ? Status.reblog : Status.unreblog)(realStatus)
MastodonController.shared.client.run(request) { response in
2018-09-11 14:52:21 +00:00
self.reblogged = realStatus.reblogged ?? false
2018-09-09 01:35:40 +00:00
DispatchQueue.main.async {
2018-09-11 14:52:21 +00:00
if case .success = response {
2018-09-18 01:57:46 +00:00
self.reblogged = realStatus.reblogged ?? false
2018-09-11 14:52:21 +00:00
UIImpactFeedbackGenerator(style: .light).impactOccurred()
} else {
2018-09-18 01:57:46 +00:00
self.reblogged = oldValue
2018-09-11 14:52:21 +00:00
print("Couldn't reblog status \(realStatus.id)")
// todo: display error message
UINotificationFeedbackGenerator().notificationOccurred(.error)
}
2018-09-09 01:35:40 +00:00
}
}
}
2018-09-11 22:17:48 +00:00
@IBAction func morePressed(_ sender: Any) {
delegate?.showMoreOptions(forStatus: statusID)
2018-09-11 22:17:48 +00:00
}
2018-08-17 02:39:16 +00:00
}
extension StatusTableViewCell: TableViewSwipeActionProvider {
static var favoriteActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 137/131, height: 30)).image { _ in
UIImage(named: "Favorite")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 137/131, height: 30))
}
static var reblogActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 927/558, height: 30)).image { _ in
UIImage(named: "Reblog")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 927/558, height: 30))
}
static var replyActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 205/151, height: 30)).image { _ in
UIImage(named: "Reply")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 205/151, height: 30))
}
static var moreActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 2/1, height: 30)).image { _ in
UIImage(named: "More")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 2/1, height: 30))
}
func leadingSwipeActionsConfiguration() -> UISwipeActionsConfiguration? {
2018-09-18 16:59:07 +00:00
guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
2018-09-18 01:57:46 +00:00
let favoriteTitle: String
2018-09-17 23:37:58 +00:00
let favoriteRequest: Request<Status>
let favoriteColor: UIColor
if status.favourited ?? false {
favoriteTitle = "Unfavorite"
2018-09-17 23:37:58 +00:00
favoriteRequest = Status.unfavourite(status)
favoriteColor = UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1)
} else {
favoriteTitle = "Favorite"
2018-09-17 23:37:58 +00:00
favoriteRequest = Status.favourite(status)
favoriteColor = UIColor(displayP3Red: 1, green: 204/255, blue: 0, alpha: 1)
}
let favorite = UIContextualAction(style: .normal, title: favoriteTitle) { (action, view, completion) in
2018-09-17 23:37:58 +00:00
MastodonController.shared.client.run(favoriteRequest, completion: { response in
DispatchQueue.main.async {
2018-09-18 01:57:46 +00:00
guard case let .success(status, _) = response else {
completion(false)
2018-09-18 01:57:46 +00:00
return
}
2018-09-18 01:57:46 +00:00
completion(true)
2018-09-18 16:59:07 +00:00
MastodonCache.add(status: status)
2018-09-18 01:57:46 +00:00
self.updateUI(for: self.statusID)
}
2018-09-17 23:37:58 +00:00
})
}
favorite.image = StatusTableViewCell.favoriteActionImage
favorite.backgroundColor = favoriteColor
let reblogTitle: String
2018-09-17 23:37:58 +00:00
let reblogRequest: Request<Status>
let reblogColor: UIColor
if status.reblogged ?? false {
reblogTitle = "Unreblog"
2018-09-17 23:37:58 +00:00
reblogRequest = Status.unreblog(status)
reblogColor = UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1)
} else {
reblogTitle = "Reblog"
2018-09-17 23:37:58 +00:00
reblogRequest = Status.reblog(status)
reblogColor = tintColor
}
let reblog = UIContextualAction(style: .normal, title: reblogTitle) { (action, view, completion) in
2018-09-17 23:37:58 +00:00
MastodonController.shared.client.run(reblogRequest, completion: { response in
DispatchQueue.main.async {
2018-09-18 01:57:46 +00:00
guard case let .success(status, _) = response else {
completion(false)
2018-09-18 01:57:46 +00:00
return
}
2018-09-18 01:57:46 +00:00
completion(true)
2018-09-18 16:59:07 +00:00
MastodonCache.add(status: status)
2018-09-18 01:57:46 +00:00
self.updateUI(for: self.statusID)
}
2018-09-17 23:37:58 +00:00
})
}
reblog.image = StatusTableViewCell.reblogActionImage
reblog.backgroundColor = reblogColor
return UISwipeActionsConfiguration(actions: [favorite, reblog])
}
func trailingSwipeActionsConfiguration() -> UISwipeActionsConfiguration? {
let reply = UIContextualAction(style: .normal, title: "Reply") { (action, view, completion) in
completion(true)
2018-09-18 01:57:46 +00:00
self.delegate?.reply(to: self.statusID)
}
reply.image = StatusTableViewCell.replyActionImage
reply.backgroundColor = tintColor
let more = UIContextualAction(style: .normal, title: "More") { (action, view, completion) in
completion(true)
self.delegate?.showMoreOptions(forStatus: self.statusID)
}
more.image = StatusTableViewCell.moreActionImage
more.backgroundColor = .gray
return UISwipeActionsConfiguration(actions: [reply, more])
}
}
extension StatusTableViewCell: ContentLabelNavigationDelegate {
func selected(mention: Mention) {
delegate?.selected(mention: mention)
}
2018-09-11 14:52:21 +00:00
func selected(tag: Hashtag) {
delegate?.selected(tag: tag)
}
func selected(url: URL) {
delegate?.selected(url: url)
}
}
2018-09-02 20:59:20 +00:00
extension StatusTableViewCell: AttachmentViewDelegate {
func showLargeAttachment(for attachmentView: AttachmentView) {
delegate?.showLargeImage(attachmentView.image!, description: attachmentView.attachment.description, animatingFrom: attachmentView)
2018-09-02 20:59:20 +00:00
}
}