Indicate pending follow requests, feedback on successful async menu actions

Closes #265
This commit is contained in:
Shadowfacts 2022-11-28 21:41:56 -05:00
parent a97a7e0aea
commit 97f00e9d6f
1 changed files with 32 additions and 4 deletions

View File

@ -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 { private func relationshipAction(accountID: String, mastodonController: MastodonController, builder: @escaping @MainActor (RelationshipMO, MastodonController) -> UIMenuElement) -> UIDeferredMenuElement {
return UIDeferredMenuElement.uncached({ @MainActor elementHandler in return UIDeferredMenuElement.uncached({ @MainActor elementHandler in
let relationship = Task { let relationship = Task {
@ -380,14 +391,26 @@ extension MenuActionProvider {
private func followAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement { private func followAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement {
let accountID = relationship.accountID let accountID = relationship.accountID
let following = relationship.following let following = relationship.following
return createAction(identifier: "follow", title: following ? "Unfollow" : "Follow", systemImageName: following ? "person.badge.minus" : "person.badge.plus") { [weak self] _ in let requested = relationship.requested
let request = (following ? Account.unfollow : Account.follow)(accountID) 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 mastodonController.run(request) { response in
switch response { switch response {
case .failure(let error): 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, _): case .success(let relationship, _):
mastodonController.persistentContainer.addOrUpdate(relationship: 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") self?.handleError(error, title: "Error \(block ? "B" : "Unb")locking")
case .success(let relationship, _): case .success(let relationship, _):
mastodonController.persistentContainer.addOrUpdate(relationship: relationship) mastodonController.persistentContainer.addOrUpdate(relationship: relationship)
self?.handleSuccess(title: "\(block ? "B" : "Unb")locked")
} }
} }
} }
@ -415,8 +439,11 @@ extension MenuActionProvider {
return { [weak self] (_: UIAction) in return { [weak self] (_: UIAction) in
let req = block ? Client.block(domain: host) : Client.unblock(domain: host) let req = block ? Client.block(domain: host) : Client.unblock(domain: host)
mastodonController.run(req) { response in 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") 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") self?.handleError(error, title: "Error Unmuting")
case .success(let relationship, _): case .success(let relationship, _):
mastodonController.persistentContainer.addOrUpdate(relationship: relationship) mastodonController.persistentContainer.addOrUpdate(relationship: relationship)
self?.handleSuccess(title: "Unmuted")
} }
} }
} }