From 97f00e9d6f3fc62f34ca67629cf7c02cb492057b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 28 Nov 2022 21:41:56 -0500 Subject: [PATCH] Indicate pending follow requests, feedback on successful async menu actions Closes #265 --- Tusker/Screens/Utilities/Previewing.swift | 36 ++++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index 984ec12f..6f70fbd6 100644 --- a/Tusker/Screens/Utilities/Previewing.swift +++ b/Tusker/Screens/Utilities/Previewing.swift @@ -355,6 +355,17 @@ extension MenuActionProvider { } } + private func handleSuccess(title: String) { + if let toastable = self.toastableViewController { + var config = ToastConfiguration(title: title) + config.systemImageName = "checkmark" + config.dismissAutomaticallyAfter = 2 + DispatchQueue.main.async { + toastable.showToast(configuration: config, animated: true) + } + } + } + private func relationshipAction(accountID: String, mastodonController: MastodonController, builder: @escaping @MainActor (RelationshipMO, MastodonController) -> UIMenuElement) -> UIDeferredMenuElement { return UIDeferredMenuElement.uncached({ @MainActor elementHandler in let relationship = Task { @@ -380,14 +391,26 @@ extension MenuActionProvider { private func followAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement { let accountID = relationship.accountID let following = relationship.following - return createAction(identifier: "follow", title: following ? "Unfollow" : "Follow", systemImageName: following ? "person.badge.minus" : "person.badge.plus") { [weak self] _ in - let request = (following ? Account.unfollow : Account.follow)(accountID) + let requested = relationship.requested + let title = following ? "Unfollow" : requested ? "Cancel Request" : "Follow" + let imageName = following || requested ? "person.badge.minus" : "person.badge.plus" + return createAction(identifier: "follow", title: title, systemImageName: imageName) { [weak self] _ in + let request = (following || requested ? Account.unfollow : Account.follow)(accountID) mastodonController.run(request) { response in switch response { case .failure(let error): - self?.handleError(error, title: "Error \(following ? "Unf" : "F")ollowing") + self?.handleError(error, title: following ? "Error Unfollowing" : requested ? "Error Cancelinng Request" : "Error Following") case .success(let relationship, _): mastodonController.persistentContainer.addOrUpdate(relationship: relationship) + if requested { // was requested, now cancelled + self?.handleSuccess(title: "Follow Request Cancelled") + } else if following { // was following, now unfollowed + self?.handleSuccess(title: "Unfollowed") + } else if relationship.followRequested { // was not following, now requested + self?.handleSuccess(title: "Request Sent") + } else { // was not following, not now requested, assume success + self?.handleSuccess(title: "Followed") + } } } } @@ -407,6 +430,7 @@ extension MenuActionProvider { self?.handleError(error, title: "Error \(block ? "B" : "Unb")locking") case .success(let relationship, _): mastodonController.persistentContainer.addOrUpdate(relationship: relationship) + self?.handleSuccess(title: "\(block ? "B" : "Unb")locked") } } } @@ -415,8 +439,11 @@ extension MenuActionProvider { return { [weak self] (_: UIAction) in let req = block ? Client.block(domain: host) : Client.unblock(domain: host) mastodonController.run(req) { response in - if case .failure(let error) = response { + switch response { + case .failure(let error): self?.handleError(error, title: "Error \(block ? "B" : "Unb")locking") + case .success(_, _): + self?.handleSuccess(title: "Domain \(block ? "B" : "Unb")locked") } } } @@ -446,6 +473,7 @@ extension MenuActionProvider { self?.handleError(error, title: "Error Unmuting") case .success(let relationship, _): mastodonController.persistentContainer.addOrUpdate(relationship: relationship) + self?.handleSuccess(title: "Unmuted") } } }