Accept/reject notification requests (IOS-241)

This commit is contained in:
Nathan Mattes 2024-07-18 16:12:36 +02:00
parent 55d6b281a6
commit 5925d84dd9
2 changed files with 81 additions and 11 deletions

View File

@ -55,8 +55,8 @@ class NotificationRequestTableViewCell: UITableViewCell {
acceptNotificationRequestButton.translatesAutoresizingMaskIntoConstraints = false
acceptNotificationRequestButton.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
acceptNotificationRequestButton.setTitleColor(.white, for: .normal)
acceptNotificationRequestButton.setTitle(L10n.Common.Controls.Actions.confirm, for: .normal)
acceptNotificationRequestButton.setImage(Asset.Editing.checkmark20.image.withRenderingMode(.alwaysTemplate), for: .normal)
acceptNotificationRequestButton.setTitle("Accept", for: .normal)
acceptNotificationRequestButton.setImage(UIImage(systemName: "checkmark"), for: .normal)
acceptNotificationRequestButton.imageView?.contentMode = .scaleAspectFit
acceptNotificationRequestButton.setBackgroundImage(.placeholder(color: Asset.Scene.Notification.confirmFollowRequestButtonBackground.color), for: .normal)
acceptNotificationRequestButton.setInsets(forContentPadding: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8), imageTitlePadding: 8)
@ -71,7 +71,7 @@ class NotificationRequestTableViewCell: UITableViewCell {
acceptNotificationRequestActivityIndicatorView = UIActivityIndicatorView(style: .medium)
acceptNotificationRequestActivityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
acceptNotificationRequestActivityIndicatorView.color = .black
acceptNotificationRequestActivityIndicatorView.color = .white
acceptNotificationRequestActivityIndicatorView.hidesWhenStopped = true
acceptNotificationRequestActivityIndicatorView.stopAnimating()
acceptNotificationRequestButton.addSubview(acceptNotificationRequestActivityIndicatorView)
@ -80,8 +80,8 @@ class NotificationRequestTableViewCell: UITableViewCell {
rejectNotificationRequestButton.translatesAutoresizingMaskIntoConstraints = false
rejectNotificationRequestButton.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
rejectNotificationRequestButton.setTitleColor(.black, for: .normal)
rejectNotificationRequestButton.setTitle(L10n.Common.Controls.Actions.delete, for: .normal)
rejectNotificationRequestButton.setImage(Asset.Circles.forbidden20.image.withRenderingMode(.alwaysTemplate), for: .normal)
rejectNotificationRequestButton.setTitle("Dismiss", for: .normal)
rejectNotificationRequestButton.setImage(UIImage(systemName: "speaker.slash"), for: .normal)
rejectNotificationRequestButton.imageView?.contentMode = .scaleAspectFit
rejectNotificationRequestButton.setInsets(forContentPadding: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8), imageTitlePadding: 8)
rejectNotificationRequestButton.setBackgroundImage(.placeholder(color: Asset.Scene.Notification.deleteFollowRequestButtonBackground.color), for: .normal)

View File

@ -17,11 +17,14 @@ enum NotificationRequestItem: Hashable {
case item(Mastodon.Entity.NotificationRequest)
}
class NotificationRequestsTableViewController: UIViewController, NeedsDependency {
protocol NotificationRequestsTableViewControllerDelegate: AnyObject {
func notificationRequestsUpdated(_ viewController: NotificationRequestsTableViewController)
}
class NotificationRequestsTableViewController: UIViewController, NeedsDependency {
var context: AppContext!
var coordinator: SceneCoordinator!
weak var delegate: NotificationRequestsTableViewControllerDelegate?
let tableView: UITableView
var viewModel: NotificationRequestsViewModel
@ -95,24 +98,91 @@ extension NotificationRequestsTableViewController: AuthContextProvider {
extension NotificationRequestsTableViewController: NotificationRequestTableViewCellDelegate {
func acceptNotificationRequest(_ cell: NotificationRequestTableViewCell, notificationRequest: MastodonSDK.Mastodon.Entity.NotificationRequest) {
print("accept \(notificationRequest.id)")
cell.acceptNotificationRequestActivityIndicatorView.isHidden = false
cell.acceptNotificationRequestActivityIndicatorView.startAnimating()
cell.acceptNotificationRequestButton.tintColor = .clear
cell.acceptNotificationRequestButton.setTitleColor(.clear, for: .normal)
cell.rejectNotificationRequestButton.isUserInteractionEnabled = false
cell.acceptNotificationRequestButton.isUserInteractionEnabled = false
//TODO: Send request, update cell, reload notification requests AND general notifications
Task { [weak self] in
guard let self else { return }
do {
_ = try await context.apiService.acceptNotificationRequests(authenticationBox: authContext.mastodonAuthenticationBox,
id: notificationRequest.id)
let requests = try await context.apiService.notificationRequests(authenticationBox: authContext.mastodonAuthenticationBox).value
if requests.count > 0 {
await MainActor.run { [weak self] in
guard let self else { return }
self.viewModel.requests = requests
var snapshot = NSDiffableDataSourceSnapshot<NotificationRequestsSection, NotificationRequestItem>()
snapshot.appendSections([.main])
snapshot.appendItems(self.viewModel.requests.compactMap { NotificationRequestItem.item($0) } )
self.dataSource?.apply(snapshot)
}
} else {
await MainActor.run { [weak self] in
_ = self?.navigationController?.popViewController(animated: true)
}
}
} catch {
cell.acceptNotificationRequestActivityIndicatorView.stopAnimating()
cell.acceptNotificationRequestButton.tintColor = .white
cell.acceptNotificationRequestButton.setTitleColor(.white, for: .normal)
cell.rejectNotificationRequestButton.isUserInteractionEnabled = true
cell.acceptNotificationRequestButton.isUserInteractionEnabled = true
}
}
}
func rejectNotificationRequest(_ cell: NotificationRequestTableViewCell, notificationRequest: MastodonSDK.Mastodon.Entity.NotificationRequest) {
print("reject \(notificationRequest.id)")
cell.rejectNotificationRequestActivityIndicatorView.isHidden = false
cell.rejectNotificationRequestActivityIndicatorView.startAnimating()
cell.rejectNotificationRequestButton.tintColor = .clear
cell.rejectNotificationRequestButton.setTitleColor(.clear, for: .normal)
cell.rejectNotificationRequestButton.isUserInteractionEnabled = false
cell.acceptNotificationRequestButton.isUserInteractionEnabled = false
Task { [weak self] in
guard let self else { return }
do {
_ = try await context.apiService.rejectNotificationRequests(authenticationBox: authContext.mastodonAuthenticationBox,
id: notificationRequest.id)
let requests = try await context.apiService.notificationRequests(authenticationBox: authContext.mastodonAuthenticationBox).value
if requests.count > 0 {
await MainActor.run { [weak self] in
guard let self else { return }
self.viewModel.requests = requests
var snapshot = NSDiffableDataSourceSnapshot<NotificationRequestsSection, NotificationRequestItem>()
snapshot.appendSections([.main])
snapshot.appendItems(self.viewModel.requests.compactMap { NotificationRequestItem.item($0) } )
self.dataSource?.apply(snapshot)
}
} else {
await MainActor.run { [weak self] in
_ = self?.navigationController?.popViewController(animated: true)
}
}
} catch {
cell.rejectNotificationRequestActivityIndicatorView.stopAnimating()
cell.rejectNotificationRequestButton.tintColor = .black
cell.rejectNotificationRequestButton.setTitleColor(.black, for: .normal)
cell.rejectNotificationRequestButton.isUserInteractionEnabled = true
cell.acceptNotificationRequestButton.isUserInteractionEnabled = true
}
}
}
}