From 6bc4993d81bb85e139e525dc9147f15eccac4675 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 3 Nov 2022 18:48:39 -0400 Subject: [PATCH] Fix favorite/reblog menu actions not working --- Tusker/Screens/Utilities/Previewing.swift | 45 +++++------------------ 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index 1860c57eee..33dc324d6b 100644 --- a/Tusker/Screens/Utilities/Previewing.swift +++ b/Tusker/Screens/Utilities/Previewing.swift @@ -185,21 +185,11 @@ extension MenuActionProvider { favImage = favImage.withTintColor(UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1), renderingMode: .alwaysOriginal) } toggleableSection.insert(createAction(identifier: "favorite", title: favorited ? "Unfavorite" : "Favorite", image: favImage, handler: { [weak self] _ in - guard let self = self else { return } - let request = (favorited ? Status.favourite : Status.unfavourite)(status.id) - self.mastodonController?.run(request, completion: { response in - 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 \(favorited ? "Unf" : "F")avoriting", in: toastable, retryAction: nil) - DispatchQueue.main.async { - toastable.showToast(configuration: config, animated: true) - } - } - } - }) + guard let self, + let navigationDelegate = self.navigationDelegate else { return } + Task { @MainActor in + await FavoriteService(status: status, mastodonController: mastodonController, presenter: navigationDelegate).toggleFavorite() + } }), at: 0) let reblogged = status.reblogged @@ -208,26 +198,11 @@ extension MenuActionProvider { reblogImage = reblogImage.withTintColor(UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1), renderingMode: .alwaysOriginal) } toggleableSection.insert(createAction(identifier: "reblog", title: reblogged ? "Unreblog" : "Reblog", image: reblogImage, handler: { [weak self] _ in - guard let self = self else { return } - let request: Request - if reblogged { - request = Status.reblog(status.id) - } else { - request = Status.unreblog(status.id) + guard let self, + let navigationDelegate = self.navigationDelegate else { return } + Task { @MainActor in + await ReblogService(status: status, mastodonController: mastodonController, presenter: navigationDelegate).toggleReblog() } - self.mastodonController?.run(request, completion: { response in - 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 \(reblogged ? "Unr" : "R")eblogging", in: toastable, retryAction: nil) - DispatchQueue.main.async { - toastable.showToast(configuration: config, animated: true) - } - } - } - }) }), at: 1) } @@ -362,7 +337,7 @@ extension MenuActionProvider { ] } - private func createAction(identifier: String, title: String, systemImageName: String?, handler: @escaping UIActionHandler) -> UIAction { + private func createAction(identifier: String, title: String, systemImageName: String?, handler: @escaping (UIAction) -> Void) -> UIAction { let image: UIImage? if let name = systemImageName { image = UIImage(systemName: name)