From fa34df26df0949e48f98a11c455f30909df3d7d7 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Fri, 20 Oct 2023 18:53:27 +0200 Subject: [PATCH] Determine button-state based on freshly loaded relationship --- Mastodon/Diffable/User/UserSection.swift | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Mastodon/Diffable/User/UserSection.swift b/Mastodon/Diffable/User/UserSection.swift index 5667b3450..3c6c962af 100644 --- a/Mastodon/Diffable/User/UserSection.swift +++ b/Mastodon/Diffable/User/UserSection.swift @@ -34,6 +34,36 @@ extension UserSection { case .account(let account): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: UserTableViewCell.self), for: indexPath) as! UserTableViewCell cell.configure(tableView: tableView, account: account, delegate: userTableViewCellDelegate) + cell.userView.setButtonState(.loading) + Task { + do { + + guard let relationship = try await context.apiService.relationship(forAccounts: [account], authenticationBox: authContext.mastodonAuthenticationBox).value.first else { + return await MainActor.run { + cell.userView.setButtonState(.none) + } + } + + let buttonState: UserView.ButtonState + if relationship.following { + buttonState = .unfollow + } else if relationship.blocking || (relationship.domainBlocking ?? false) { + buttonState = .blocked + } else if relationship.requested ?? false { + buttonState = .pending + } else { + buttonState = .follow + } + + await MainActor.run { + cell.userView.setButtonState(buttonState) + } + } catch { + await MainActor.run { + cell.userView.setButtonState(.none) + } + } + } return cell