2020-01-05 04:13:23 +00:00
|
|
|
//
|
|
|
|
// FollowRequestNotificationTableViewCell.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 1/4/20.
|
|
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Pachyderm
|
|
|
|
|
|
|
|
class FollowRequestNotificationTableViewCell: UITableViewCell {
|
|
|
|
|
2020-01-20 04:02:07 +00:00
|
|
|
weak var delegate: TuskerNavigationDelegate?
|
2020-01-06 00:39:37 +00:00
|
|
|
var mastodonController: MastodonController! { delegate?.apiController }
|
2020-01-05 04:13:23 +00:00
|
|
|
|
|
|
|
@IBOutlet weak var stackView: UIStackView!
|
|
|
|
@IBOutlet weak var avatarImageView: UIImageView!
|
|
|
|
@IBOutlet weak var timestampLabel: UILabel!
|
2020-03-02 00:40:32 +00:00
|
|
|
@IBOutlet weak var actionLabel: EmojiLabel!
|
2020-01-05 04:13:23 +00:00
|
|
|
@IBOutlet weak var actionButtonsStackView: UIStackView!
|
|
|
|
@IBOutlet weak var acceptButton: UIButton!
|
|
|
|
@IBOutlet weak var rejectButton: UIButton!
|
|
|
|
|
|
|
|
var notification: Pachyderm.Notification?
|
|
|
|
var account: Account!
|
|
|
|
|
2020-01-25 15:06:27 +00:00
|
|
|
var avatarRequest: ImageCache.Request?
|
2020-01-05 04:13:23 +00:00
|
|
|
var updateTimestampWorkItem: DispatchWorkItem?
|
|
|
|
|
2020-01-20 04:10:52 +00:00
|
|
|
deinit {
|
|
|
|
updateTimestampWorkItem?.cancel()
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:13:23 +00:00
|
|
|
override func awakeFromNib() {
|
|
|
|
super.awakeFromNib()
|
|
|
|
|
|
|
|
avatarImageView.layer.masksToBounds = true
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
|
|
|
|
updateUIForPreferences()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func updateUIForPreferences() {
|
|
|
|
avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadiusFraction * 30
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUI(notification: Pachyderm.Notification) {
|
|
|
|
self.notification = notification
|
|
|
|
updateUI(account: notification.account)
|
|
|
|
updateTimestamp()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUI(account: Account) {
|
2020-04-12 16:54:27 +00:00
|
|
|
// todo: update to use managed objects
|
2020-01-05 04:13:23 +00:00
|
|
|
self.account = account
|
2020-03-02 00:40:32 +00:00
|
|
|
if Preferences.shared.hideCustomEmojiInUsernames {
|
2020-04-12 16:54:27 +00:00
|
|
|
actionLabel.text = "Request to follow from \(account.displayName)"
|
2020-03-02 00:40:32 +00:00
|
|
|
actionLabel.removeEmojis()
|
|
|
|
} else {
|
2020-04-12 16:54:27 +00:00
|
|
|
actionLabel.text = "Request to follow from \(account.displayName)"
|
2020-03-02 00:40:32 +00:00
|
|
|
actionLabel.setEmojis(account.emojis, identifier: account.id)
|
|
|
|
}
|
2020-01-25 15:06:27 +00:00
|
|
|
avatarRequest = ImageCache.avatars.get(account.avatar) { [weak self] (data) in
|
|
|
|
guard let self = self, self.account == account, let data = data, let image = UIImage(data: data) else { return }
|
|
|
|
self.avatarRequest = nil
|
2020-01-05 04:13:23 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.avatarImageView.image = image
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateTimestamp() {
|
|
|
|
guard let notification = notification else { return }
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-01-05 04:13:23 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!)
|
|
|
|
} else {
|
|
|
|
updateTimestampWorkItem = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
|
2020-01-25 15:06:27 +00:00
|
|
|
avatarRequest?.cancel()
|
2020-01-05 04:13:23 +00:00
|
|
|
updateTimestampWorkItem?.cancel()
|
|
|
|
updateTimestampWorkItem = nil
|
|
|
|
}
|
|
|
|
|
2020-09-11 02:54:01 +00:00
|
|
|
private func addLabel(_ text: String) {
|
|
|
|
let label = UILabel()
|
|
|
|
label.textAlignment = .center
|
|
|
|
label.font = .boldSystemFont(ofSize: 17)
|
|
|
|
label.text = text
|
|
|
|
self.stackView.addArrangedSubview(label)
|
|
|
|
}
|
|
|
|
|
2020-01-05 04:13:23 +00:00
|
|
|
// MARK: - Interaction
|
|
|
|
|
|
|
|
@IBAction func rejectButtonPressed() {
|
2020-09-11 02:54:01 +00:00
|
|
|
acceptButton.isEnabled = false
|
|
|
|
rejectButton.isEnabled = false
|
|
|
|
|
2020-01-05 04:13:23 +00:00
|
|
|
let request = Account.rejectFollowRequest(account)
|
2020-01-06 00:39:37 +00:00
|
|
|
mastodonController.run(request) { (response) in
|
2020-05-07 03:29:57 +00:00
|
|
|
guard case .success(_, _) = response else { fatalError() }
|
2020-01-05 04:13:23 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
UINotificationFeedbackGenerator().notificationOccurred(.success)
|
|
|
|
self.actionButtonsStackView.isHidden = true
|
2020-09-11 02:54:01 +00:00
|
|
|
self.addLabel(NSLocalizedString("Rejected", comment: "rejected follow request label"))
|
2020-01-05 04:13:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func acceptButtonPressed() {
|
2020-09-11 02:54:01 +00:00
|
|
|
acceptButton.isEnabled = false
|
|
|
|
rejectButton.isEnabled = false
|
|
|
|
|
2020-01-05 04:13:23 +00:00
|
|
|
let request = Account.authorizeFollowRequest(account)
|
2020-01-06 00:39:37 +00:00
|
|
|
mastodonController.run(request) { (response) in
|
2020-05-07 03:29:57 +00:00
|
|
|
guard case .success(_, _) = response else { fatalError() }
|
2020-01-05 04:13:23 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
UINotificationFeedbackGenerator().notificationOccurred(.success)
|
|
|
|
self.actionButtonsStackView.isHidden = true
|
2020-09-11 02:54:01 +00:00
|
|
|
self.addLabel(NSLocalizedString("Accepted", comment: "accepted follow request label"))
|
2020-01-05 04:13:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FollowRequestNotificationTableViewCell: SelectableTableViewCell {
|
|
|
|
func didSelectCell() {
|
|
|
|
delegate?.selected(account: account.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FollowRequestNotificationTableViewCell: MenuPreviewProvider {
|
|
|
|
var navigationDelegate: TuskerNavigationDelegate? { return delegate }
|
|
|
|
|
|
|
|
func getPreviewProviders(for location: CGPoint, sourceViewController: UIViewController) -> PreviewProviders? {
|
2020-01-05 20:25:07 +00:00
|
|
|
guard let mastodonController = mastodonController else { return nil }
|
2020-01-05 04:13:23 +00:00
|
|
|
return (content: {
|
2020-07-05 20:17:56 +00:00
|
|
|
return ProfileViewController(accountID: self.account.id, mastodonController: mastodonController)
|
2020-01-05 04:13:23 +00:00
|
|
|
}, actions: {
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|