From 1e2947ceba9b559f40d46052a9d3922c5d1613cb Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 9 May 2022 21:23:04 -0400 Subject: [PATCH] Fix crash when accept/reject follow request fails --- ...llowRequestNotificationTableViewCell.swift | 39 +++++++++++++++---- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/Tusker/Views/Notifications/FollowRequestNotificationTableViewCell.swift b/Tusker/Views/Notifications/FollowRequestNotificationTableViewCell.swift index eaf24cdd..e1613f61 100644 --- a/Tusker/Views/Notifications/FollowRequestNotificationTableViewCell.swift +++ b/Tusker/Views/Notifications/FollowRequestNotificationTableViewCell.swift @@ -134,13 +134,24 @@ class FollowRequestNotificationTableViewCell: UITableViewCell { acceptButton.isEnabled = false rejectButton.isEnabled = false - let request = Account.rejectFollowRequest(account) - mastodonController.run(request) { (response) in - guard case .success(_, _) = response else { fatalError() } - DispatchQueue.main.async { + Task { + let request = Account.rejectFollowRequest(account) + do { + _ = try await mastodonController.run(request) + UINotificationFeedbackGenerator().notificationOccurred(.success) self.actionButtonsStackView.isHidden = true self.addLabel(NSLocalizedString("Rejected", comment: "rejected follow request label")) + } catch let error as Client.Error { + acceptButton.isEnabled = true + rejectButton.isEnabled = true + if let toastable = delegate?.toastableViewController { + let config = ToastConfiguration(from: error, with: "Rejecting Follow", in: toastable) { [weak self] toast in + toast.dismissToast(animated: true) + self?.rejectButtonPressed() + } + toastable.showToast(configuration: config, animated: true) + } } } } @@ -149,13 +160,25 @@ class FollowRequestNotificationTableViewCell: UITableViewCell { acceptButton.isEnabled = false rejectButton.isEnabled = false - let request = Account.authorizeFollowRequest(account) - mastodonController.run(request) { (response) in - guard case .success(_, _) = response else { fatalError() } - DispatchQueue.main.async { + Task { + let request = Account.authorizeFollowRequest(account) + do { + _ = try await mastodonController.run(request) + UINotificationFeedbackGenerator().notificationOccurred(.success) self.actionButtonsStackView.isHidden = true self.addLabel(NSLocalizedString("Accepted", comment: "accepted follow request label")) + } catch let error as Client.Error { + acceptButton.isEnabled = true + rejectButton.isEnabled = true + + if let toastable = delegate?.toastableViewController { + let config = ToastConfiguration(from: error, with: "Accepting Follow", in: toastable) { [weak self] toast in + toast.dismissToast(animated: true) + self?.acceptButtonPressed() + } + toastable.showToast(configuration: config, animated: true) + } } } }