Add toast error messages to menu actions

This commit is contained in:
Shadowfacts 2022-05-01 23:06:59 -04:00
parent bf8a294676
commit 56a0518c80
2 changed files with 39 additions and 9 deletions

View File

@ -139,8 +139,15 @@ extension MenuActionProvider {
guard let self = self else { return }
let request = (bookmarked ? Status.unbookmark : Status.bookmark)(status.id)
self.mastodonController?.run(request) { (response) in
if case let .success(status, _) = response {
switch response {
case .success(let status, _):
self.mastodonController?.persistentContainer.addOrUpdate(status: status)
case .failure(let error):
if let toastable = self.toastableViewController {
let config = ToastConfiguration(from: error, with: "Error \(bookmarked ? "Unb" : "B")ookmarking", in: toastable, retryAction: nil)
toastable.showToast(configuration: config, animated: true)
}
}
}
}),
@ -161,8 +168,14 @@ extension MenuActionProvider {
guard let self = self else { return }
let request = (muted ? Status.unmuteConversation : Status.muteConversation)(status.id)
self.mastodonController?.run(request) { (response) in
if case let .success(status, _) = response {
switch response {
case .success(let status, _):
self.mastodonController?.persistentContainer.addOrUpdate(status: status)
case .failure(let error):
if let toastable = self.toastableViewController {
let config = ToastConfiguration(from: error, with: "Error \(muted ? "Unm" : "M")uting", in: toastable, retryAction: nil)
toastable.showToast(configuration: config, animated: true)
}
}
}
}))
@ -177,8 +190,14 @@ extension MenuActionProvider {
let request = (pinned ? Status.unpin : Status.pin)(status.id)
self.mastodonController?.run(request, completion: { [weak self] (response) in
guard let self = self else { return }
if case let .success(status, _) = response {
switch response {
case .success(let status, _):
self.mastodonController?.persistentContainer.addOrUpdate(status: status)
case .failure(let error):
if let toastable = self.toastableViewController {
let config = ToastConfiguration(from: error, with: "Error \(pinned ? "Unp" :"P")inning", in: toastable, retryAction: nil)
toastable.showToast(configuration: config, animated: true)
}
}
})
}))
@ -190,10 +209,16 @@ extension MenuActionProvider {
guard let mastodonController = self?.mastodonController else { return }
let request = Client.getStatus(id: status.id)
mastodonController.run(request, completion: { (response) in
if case let .success(status, _) = response {
switch response {
case .success(let status, _):
// todo: this shouldn't really use the viewContext, but for some reason saving the
// backgroundContext with the new version of the status isn't updating the viewContext
mastodonController.persistentContainer.addOrUpdate(status: status, context: mastodonController.persistentContainer.viewContext)
case .failure(let error):
if let toastable = self?.toastableViewController {
let config = ToastConfiguration(from: error, with: "Error Refreshing Poll", in: toastable, retryAction: nil)
toastable.showToast(configuration: config, animated: true)
}
}
})
}), at: 0)
@ -283,8 +308,11 @@ extension MenuActionProvider {
let request = (following ? Account.unfollow : Account.follow)(accountID)
mastodonController.run(request) { response in
switch response {
case .failure(_):
fatalError()
case .failure(let error):
if let toastable = self.toastableViewController {
let config = ToastConfiguration(from: error, with: "Error \(following ? "Unf" : "F")ollowing", in: toastable, retryAction: nil)
toastable.showToast(configuration: config, animated: true)
}
case .success(let relationship, _):
mastodonController.persistentContainer.addOrUpdate(relationship: relationship)
}

View File

@ -35,12 +35,14 @@ struct ToastConfiguration {
}
extension ToastConfiguration {
init(from error: Client.Error, with title: String, in viewController: UIViewController, retryAction: @escaping (ToastView) -> Void) {
init(from error: Client.Error, with title: String, in viewController: UIViewController, retryAction: ((ToastView) -> Void)?) {
self.init(title: title)
self.subtitle = error.localizedDescription
self.systemImageName = error.systemImageName
self.actionTitle = "Retry"
self.action = retryAction
if let retryAction = retryAction {
self.actionTitle = "Retry"
self.action = retryAction
}
self.longPressAction = { [unowned viewController] toast in
toast.dismissToast(animated: true)
let text = """