From 56a0518c80acb44b30793b3ec6f38103d89a6899 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 1 May 2022 23:06:59 -0400 Subject: [PATCH] Add toast error messages to menu actions --- Tusker/Screens/Utilities/Previewing.swift | 40 +++++++++++++++++---- Tusker/Views/Toast/ToastConfiguration.swift | 8 +++-- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index 936a8599ec..65bc0196db 100644 --- a/Tusker/Screens/Utilities/Previewing.swift +++ b/Tusker/Screens/Utilities/Previewing.swift @@ -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) } diff --git a/Tusker/Views/Toast/ToastConfiguration.swift b/Tusker/Views/Toast/ToastConfiguration.swift index 996557b8df..464973dd36 100644 --- a/Tusker/Views/Toast/ToastConfiguration.swift +++ b/Tusker/Views/Toast/ToastConfiguration.swift @@ -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 = """