From 40f5be28f61cb20d8bee2286f92e5a917d77ada9 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 10 Jun 2021 10:36:02 -0400 Subject: [PATCH] Cleanup un/follow menu action --- Tusker/Screens/Utilities/Previewing.swift | 41 ++++++++++++----------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index c23c0882..0f35bb71 100644 --- a/Tusker/Screens/Utilities/Previewing.swift +++ b/Tusker/Screens/Utilities/Previewing.swift @@ -63,25 +63,28 @@ extension MenuPreviewProvider { let request = Client.getRelationships(accounts: [account.id]) // talk about callback hell :/ mastodonController.run(request) { [weak self] (response) in - if let self = self, - case let .success(results, _) = response, - let relationship = results.first { - let following = relationship.following - DispatchQueue.main.async { - elementHandler([ - self.createAction(identifier: "follow", title: following ? "Unfollow" : "Follow", systemImageName: following ? "person.badge.minus" : "person.badge.plus", handler: { (_) in - let request = (following ? Account.unfollow : Account.follow)(accountID) - mastodonController.run(request) { (response) in - switch response { - case .failure(_): - fatalError() - case let .success(relationship, _): - mastodonController.persistentContainer.addOrUpdate(relationship: relationship) - } - } - }) - ]) - } + guard let self = self, + case let .success(results, _) = response, + let relationship = results.first else { + elementHandler([]) + return + } + let following = relationship.following + DispatchQueue.main.async { + let action = self.createAction(identifier: "follow", title: following ? "Unfollow" : "Follow", systemImageName: following ? "person.badge.minus" : "person.badge.plus", handler: { (_) in + let request = (following ? Account.unfollow : Account.follow)(accountID) + mastodonController.run(request) { (response) in + switch response { + case .failure(_): + fatalError() + case let .success(relationship, _): + mastodonController.persistentContainer.addOrUpdate(relationship: relationship) + } + } + }) + elementHandler([ + action + ]) } } }))