2019-09-05 21:38:04 +00:00
|
|
|
//
|
|
|
|
// ActionNotificationGroupTableViewCell.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/5/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Pachyderm
|
|
|
|
import SwiftSoup
|
|
|
|
|
|
|
|
class ActionNotificationGroupTableViewCell: UITableViewCell {
|
|
|
|
|
2020-01-20 04:02:07 +00:00
|
|
|
weak var delegate: TuskerNavigationDelegate?
|
2020-01-06 00:54:28 +00:00
|
|
|
var mastodonController: MastodonController! { delegate?.apiController }
|
2019-09-05 21:38:04 +00:00
|
|
|
|
|
|
|
@IBOutlet weak var actionImageView: UIImageView!
|
|
|
|
@IBOutlet weak var actionAvatarStackView: UIStackView!
|
|
|
|
@IBOutlet weak var timestampLabel: UILabel!
|
|
|
|
@IBOutlet weak var actionLabel: UILabel!
|
|
|
|
@IBOutlet weak var statusContentLabel: UILabel!
|
|
|
|
|
|
|
|
var group: NotificationGroup!
|
|
|
|
var statusID: String!
|
|
|
|
|
2020-01-25 15:06:27 +00:00
|
|
|
var avatarRequests = [String: ImageCache.Request]()
|
2019-09-05 21:38:04 +00:00
|
|
|
var updateTimestampWorkItem: DispatchWorkItem?
|
|
|
|
|
2020-01-20 04:10:52 +00:00
|
|
|
deinit {
|
|
|
|
updateTimestampWorkItem?.cancel()
|
|
|
|
}
|
|
|
|
|
2019-09-05 21:38:04 +00:00
|
|
|
override func awakeFromNib() {
|
|
|
|
super.awakeFromNib()
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func updateUIForPreferences() {
|
2020-01-06 00:54:28 +00:00
|
|
|
let people = group.notificationIDs.compactMap(mastodonController.cache.notification(for:)).map { $0.account }
|
2019-09-05 21:38:04 +00:00
|
|
|
updateActionLabel(people: people)
|
|
|
|
|
|
|
|
for case let imageView as UIImageView in actionAvatarStackView.arrangedSubviews {
|
|
|
|
imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: imageView)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUI(group: NotificationGroup) {
|
|
|
|
guard group.kind == .favourite || group.kind == .reblog else {
|
|
|
|
fatalError("Invalid notification type \(group.kind) for ActionNotificationGroupTableViewCell")
|
|
|
|
}
|
|
|
|
self.group = group
|
|
|
|
|
2020-01-06 00:54:28 +00:00
|
|
|
guard let firstNotification = mastodonController.cache.notification(for: group.notificationIDs.first!) else { fatalError() }
|
2019-09-05 21:38:04 +00:00
|
|
|
let status = firstNotification.status!
|
|
|
|
self.statusID = status.id
|
|
|
|
|
|
|
|
updateUIForPreferences()
|
|
|
|
|
|
|
|
switch group.kind {
|
|
|
|
case .favourite:
|
|
|
|
actionImageView.image = UIImage(systemName: "star.fill")
|
|
|
|
case .reblog:
|
|
|
|
actionImageView.image = UIImage(systemName: "repeat")
|
|
|
|
default:
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
2020-01-06 00:54:28 +00:00
|
|
|
let people = group.notificationIDs.compactMap(mastodonController.cache.notification(for:)).map { $0.account }
|
2019-09-05 21:38:04 +00:00
|
|
|
|
|
|
|
actionAvatarStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
|
|
|
|
for account in people {
|
|
|
|
let imageView = UIImageView()
|
|
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
imageView.layer.masksToBounds = true
|
|
|
|
imageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadiusFraction * 30
|
2020-01-25 15:06:27 +00:00
|
|
|
avatarRequests[account.id] = ImageCache.avatars.get(account.avatar) { [weak self] (data) in
|
|
|
|
guard let self = self, let data = data, self.group.id == group.id else { return }
|
2019-09-05 21:38:04 +00:00
|
|
|
DispatchQueue.main.async {
|
2020-01-25 15:06:27 +00:00
|
|
|
self.avatarRequests.removeValue(forKey: account.id)
|
2019-09-05 21:38:04 +00:00
|
|
|
imageView.image = UIImage(data: data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
actionAvatarStackView.addArrangedSubview(imageView)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
imageView.widthAnchor.constraint(equalToConstant: 30),
|
|
|
|
imageView.heightAnchor.constraint(equalToConstant: 30)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTimestamp()
|
|
|
|
|
|
|
|
updateActionLabel(people: people)
|
|
|
|
|
|
|
|
let doc = try! SwiftSoup.parse(status.content)
|
|
|
|
statusContentLabel.text = try! doc.text()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateTimestamp() {
|
|
|
|
guard let id = group.notificationIDs.first,
|
2020-01-06 00:54:28 +00:00
|
|
|
let notification = mastodonController.cache.notification(for: id) else {
|
2019-09-05 23:53:08 +00:00
|
|
|
fatalError("Missing cached notification")
|
2019-09-05 21:38:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
timestampLabel.text = notification.createdAt.timeAgoString()
|
|
|
|
|
|
|
|
let delay: DispatchTimeInterval?
|
|
|
|
switch notification.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()
|
|
|
|
}
|
2020-01-20 04:10:52 +00:00
|
|
|
}
|
2019-09-05 21:38:04 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
|
|
|
|
} else {
|
|
|
|
updateTimestampWorkItem = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateActionLabel(people: [Account]) {
|
|
|
|
let verb: String
|
|
|
|
switch group.kind {
|
|
|
|
case .favourite:
|
|
|
|
verb = "Favorited"
|
|
|
|
case .reblog:
|
|
|
|
verb = "Reblogged"
|
|
|
|
default:
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
let peopleStr: String
|
|
|
|
// todo: figure out how to localize this
|
2020-04-12 16:54:27 +00:00
|
|
|
// todo: update to use managed objects
|
2019-09-05 21:38:04 +00:00
|
|
|
switch people.count {
|
|
|
|
case 1:
|
2020-04-12 16:54:27 +00:00
|
|
|
peopleStr = people.first!.displayName
|
2019-09-05 21:38:04 +00:00
|
|
|
case 2:
|
2020-04-12 16:54:27 +00:00
|
|
|
peopleStr = people.first!.displayName + " and " + people.last!.displayName
|
2019-09-05 21:38:04 +00:00
|
|
|
default:
|
2020-04-12 16:54:27 +00:00
|
|
|
peopleStr = people.dropLast().map { $0.displayName }.joined(separator: ", ") + ", and " + people.last!.displayName
|
2019-09-05 21:38:04 +00:00
|
|
|
}
|
|
|
|
actionLabel.text = "\(verb) by \(peopleStr)"
|
|
|
|
}
|
2019-09-06 03:16:45 +00:00
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
|
2020-01-25 15:06:27 +00:00
|
|
|
avatarRequests.values.forEach { $0.cancel() }
|
2019-09-06 03:16:45 +00:00
|
|
|
updateTimestampWorkItem?.cancel()
|
|
|
|
updateTimestampWorkItem = nil
|
|
|
|
}
|
2019-12-14 16:59:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ActionNotificationGroupTableViewCell: SelectableTableViewCell {
|
|
|
|
func didSelectCell() {
|
|
|
|
guard let delegate = delegate else { return }
|
2020-01-06 00:54:28 +00:00
|
|
|
let notifications = group.notificationIDs.compactMap(mastodonController.cache.notification(for:))
|
2019-12-14 16:59:31 +00:00
|
|
|
let accountIDs = notifications.map { $0.account.id }
|
|
|
|
let action: StatusActionAccountListTableViewController.ActionType
|
|
|
|
switch notifications.first!.kind {
|
|
|
|
case .favourite:
|
|
|
|
action = .favorite
|
|
|
|
case .reblog:
|
|
|
|
action = .reblog
|
|
|
|
default:
|
|
|
|
fatalError()
|
2019-09-06 03:16:45 +00:00
|
|
|
}
|
2019-12-14 16:59:31 +00:00
|
|
|
let vc = delegate.statusActionAccountList(action: action, statusID: statusID, statusState: .unknown, accountIDs: accountIDs)
|
|
|
|
delegate.show(vc)
|
2019-09-06 03:16:45 +00:00
|
|
|
}
|
2019-09-06 21:56:45 +00:00
|
|
|
}
|
2019-09-05 21:38:04 +00:00
|
|
|
|
2019-09-06 21:56:45 +00:00
|
|
|
extension ActionNotificationGroupTableViewCell: MenuPreviewProvider {
|
2019-12-14 18:36:05 +00:00
|
|
|
var navigationDelegate: TuskerNavigationDelegate? { return delegate }
|
2019-09-06 21:56:45 +00:00
|
|
|
|
|
|
|
func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? {
|
|
|
|
return (content: {
|
2020-01-06 00:54:28 +00:00
|
|
|
let notifications = self.group.notificationIDs.compactMap(self.mastodonController.cache.notification(for:))
|
2019-09-07 21:10:58 +00:00
|
|
|
let accountIDs = notifications.map { $0.account.id }
|
|
|
|
let action: StatusActionAccountListTableViewController.ActionType
|
|
|
|
switch notifications.first!.kind {
|
|
|
|
case .favourite:
|
|
|
|
action = .favorite
|
|
|
|
case .reblog:
|
|
|
|
action = .reblog
|
|
|
|
default:
|
|
|
|
fatalError()
|
|
|
|
}
|
2019-11-28 23:36:58 +00:00
|
|
|
return self.delegate?.statusActionAccountList(action: action, statusID: self.statusID, statusState: .unknown, accountIDs: accountIDs)
|
2019-09-06 21:56:45 +00:00
|
|
|
}, actions: {
|
2019-11-17 23:49:48 +00:00
|
|
|
return []
|
2019-09-06 21:56:45 +00:00
|
|
|
})
|
|
|
|
}
|
2019-09-05 21:38:04 +00:00
|
|
|
}
|