// // FollowRequestNotificationTableViewCell.swift // Tusker // // Created by Shadowfacts on 1/4/20. // Copyright © 2020 Shadowfacts. All rights reserved. // import UIKit import Pachyderm class FollowRequestNotificationTableViewCell: UITableViewCell { var delegate: TuskerNavigationDelegate? @IBOutlet weak var stackView: UIStackView! @IBOutlet weak var avatarImageView: UIImageView! @IBOutlet weak var timestampLabel: UILabel! @IBOutlet weak var actionLabel: UILabel! @IBOutlet weak var actionButtonsStackView: UIStackView! @IBOutlet weak var acceptButton: UIButton! @IBOutlet weak var rejectButton: UIButton! var notification: Pachyderm.Notification? var account: Account! var updateTimestampWorkItem: DispatchWorkItem? 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) { self.account = account actionLabel.text = "Request to follow from \(account.realDisplayName)" ImageCache.avatars.get(account.avatar) { (data) in guard self.account == account, let data = data, let image = UIImage(data: data) else { return } 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 { updateTimestampWorkItem = DispatchWorkItem(block: self.updateTimestamp) DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: updateTimestampWorkItem!) } else { updateTimestampWorkItem = nil } } override func prepareForReuse() { super.prepareForReuse() updateTimestampWorkItem?.cancel() updateTimestampWorkItem = nil } // MARK: - Interaction @IBAction func rejectButtonPressed() { let request = Account.rejectFollowRequest(account) MastodonController.client.run(request) { (response) in guard case let .success(relationship, _) = response else { fatalError() } MastodonCache.add(relationship: relationship) DispatchQueue.main.async { UINotificationFeedbackGenerator().notificationOccurred(.success) self.actionButtonsStackView.isHidden = true let label = UILabel() label.textAlignment = .center label.font = .boldSystemFont(ofSize: 17) label.text = NSLocalizedString("Rejected", comment: "rejected follow request label") self.stackView.addArrangedSubview(label) } } } @IBAction func acceptButtonPressed() { let request = Account.authorizeFollowRequest(account) MastodonController.client.run(request) { (response) in guard case let .success(relationship, _) = response else { fatalError() } MastodonCache.add(relationship: relationship) DispatchQueue.main.async { UINotificationFeedbackGenerator().notificationOccurred(.success) self.actionButtonsStackView.isHidden = true let label = UILabel() label.textAlignment = .center label.font = .boldSystemFont(ofSize: 17) label.text = NSLocalizedString("Accepted", comment: "accepted follow request label") self.stackView.addArrangedSubview(label) } } } } 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? { return (content: { return ProfileTableViewController(accountID: self.account.id) }, actions: { return [] }) } }