diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index 1ee1bcc8..465f3f7a 100644 --- a/Tusker/Screens/Utilities/Previewing.swift +++ b/Tusker/Screens/Utilities/Previewing.swift @@ -149,12 +149,7 @@ extension MenuActionProvider { 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) - DispatchQueue.main.async { - toastable.showToast(configuration: config, animated: true) - } - } + self.handleError(error, title: "Error \(bookmarked ? "Unb" : "B")ookmarking") } } }), @@ -210,12 +205,7 @@ extension MenuActionProvider { 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) - DispatchQueue.main.async { - toastable.showToast(configuration: config, animated: true) - } - } + self.handleError(error, title: "Error \(muted ? "Unm" : "M")uting") } } })) @@ -234,12 +224,7 @@ extension MenuActionProvider { 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) - DispatchQueue.main.async { - toastable.showToast(configuration: config, animated: true) - } - } + self.handleError(error, title: "Error \(pinned ? "Unp" :"P")inning") } }) })) @@ -259,12 +244,7 @@ extension MenuActionProvider { 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) - DispatchQueue.main.async { - toastable.showToast(configuration: config, animated: true) - } - } + self?.handleError(error, title: "Error Refreshing Poll") } }) }), at: 0) @@ -350,6 +330,15 @@ extension MenuActionProvider { }) } + private func handleError(_ error: Client.Error, title: String) { + if let toastable = self.toastableViewController { + let config = ToastConfiguration(from: error, with: title, in: toastable, retryAction: nil) + 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 { @@ -375,17 +364,12 @@ 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") { _ in + 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) mastodonController.run(request) { response in switch response { case .failure(let error): - if let toastable = self.toastableViewController { - let config = ToastConfiguration(from: error, with: "Error \(following ? "Unf" : "F")ollowing", in: toastable, retryAction: nil) - DispatchQueue.main.async { - toastable.showToast(configuration: config, animated: true) - } - } + self?.handleError(error, title: "Error \(following ? "Unf" : "F")ollowing") case .success(let relationship, _): mastodonController.persistentContainer.addOrUpdate(relationship: relationship) } @@ -398,21 +382,13 @@ extension MenuActionProvider { let accountID = relationship.accountID let displayName = relationship.account!.displayOrUserName let host = relationship.account!.url.host! - let handleFailure = { (isActionBlock: Bool, error: Client.Error) in - if let toastable = self.toastableViewController { - let config = ToastConfiguration(from: error, with: "Error \(isActionBlock ? "B" : "Unb")locking", in: toastable, retryAction: nil) - DispatchQueue.main.async { - toastable.showToast(configuration: config, animated: true) - } - } - } let handler = { (block: Bool) in - return { (_: UIAction) in + return { [weak self] (_: UIAction) in let req = block ? Account.block(accountID) : Account.unblock(accountID) _ = mastodonController.run(req) { response in switch response { case .failure(let error): - handleFailure(block, error) + self?.handleError(error, title: "Error \(block ? "B" : "Unb")locking") case .success(let relationship, _): mastodonController.persistentContainer.addOrUpdate(relationship: relationship) } @@ -420,11 +396,11 @@ extension MenuActionProvider { } } let domainHandler = { (block: Bool) in - return { (_: UIAction) in + 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 { - handleFailure(block, error) + self?.handleError(error, title: "Error \(block ? "B" : "Unb")locking") } } } @@ -446,27 +422,22 @@ extension MenuActionProvider { @MainActor private func muteAction(for relationship: RelationshipMO, mastodonController: MastodonController) -> UIMenuElement { if relationship.muting || relationship.mutingNotifications { - return UIAction(title: "Unmute", image: UIImage(systemName: "speaker")) { [unowned self] _ in + return UIAction(title: "Unmute", image: UIImage(systemName: "speaker")) { [weak self] _ in let req = Account.unmute(relationship.accountID) mastodonController.run(req) { response in switch response { case .failure(let error): - if let toastable = self.toastableViewController { - let config = ToastConfiguration(from: error, with: "Error Unmuting", in: toastable, retryAction: nil) - DispatchQueue.main.async { - toastable.showToast(configuration: config, animated: true) - } - } + self?.handleError(error, title: "Error Unmuting") case .success(let relationship, _): mastodonController.persistentContainer.addOrUpdate(relationship: relationship) } } } } else { - return UIAction(title: "Mute", image: UIImage(systemName: "speaker.slash")) { [unowned self] _ in + return UIAction(title: "Mute", image: UIImage(systemName: "speaker.slash")) { [weak self] _ in let view = MuteAccountView(account: relationship.account!, mastodonController: mastodonController) let host = UIHostingController(rootView: view) - self.navigationDelegate?.present(host, animated: true) + self?.navigationDelegate?.present(host, animated: true) } } }