From c8c379281f157f18aad44f2d2ebb3be08e343516 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Sat, 15 Jun 2024 09:43:43 +0200 Subject: [PATCH] Fix follow button not refreshing --- .../Sources/Account/AccountDetailViewModel.swift | 3 ++- .../Account/AccountsList/AccountsListRow.swift | 3 ++- .../Sources/Account/Follow/FollowButton.swift | 14 ++++---------- .../Lists/Sources/Lists/Edit/ListEditView.swift | 3 ++- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Packages/Account/Sources/Account/AccountDetailViewModel.swift b/Packages/Account/Sources/Account/AccountDetailViewModel.swift index b992ddd0..45aae682 100644 --- a/Packages/Account/Sources/Account/AccountDetailViewModel.swift +++ b/Packages/Account/Sources/Account/AccountDetailViewModel.swift @@ -120,7 +120,8 @@ import SwiftUI featuredTags.sort { $0.statusesCountInt > $1.statusesCountInt } relationship = data.relationships.first if let relationship { - followButtonViewModel = .init(accountId: accountId, + followButtonViewModel = .init(client: client, + accountId: accountId, relationship: relationship, shouldDisplayNotify: true, relationshipUpdated: { [weak self] relationship in diff --git a/Packages/Account/Sources/Account/AccountsList/AccountsListRow.swift b/Packages/Account/Sources/Account/AccountsList/AccountsListRow.swift index ce124b49..b9dae523 100644 --- a/Packages/Account/Sources/Account/AccountsList/AccountsListRow.swift +++ b/Packages/Account/Sources/Account/AccountsList/AccountsListRow.swift @@ -95,7 +95,8 @@ public struct AccountsListRow: View { let relationShip = viewModel.relationShip { VStack(alignment: .center) { - FollowButton(viewModel: .init(accountId: viewModel.account.id, + FollowButton(viewModel: .init(client: client, + accountId: viewModel.account.id, relationship: relationShip, shouldDisplayNotify: false, relationshipUpdated: { _ in })) diff --git a/Packages/Account/Sources/Account/Follow/FollowButton.swift b/Packages/Account/Sources/Account/Follow/FollowButton.swift index 5494ccd4..49da0ca9 100644 --- a/Packages/Account/Sources/Account/Follow/FollowButton.swift +++ b/Packages/Account/Sources/Account/Follow/FollowButton.swift @@ -9,18 +9,20 @@ import SwiftUI @MainActor @Observable public class FollowButtonViewModel { - var client: Client? + let client: Client public let accountId: String public let shouldDisplayNotify: Bool public let relationshipUpdated: (Relationship) -> Void public private(set) var relationship: Relationship - public init(accountId: String, + public init(client: Client, + accountId: String, relationship: Relationship, shouldDisplayNotify: Bool, relationshipUpdated: @escaping ((Relationship) -> Void)) { + self.client = client self.accountId = accountId self.relationship = relationship self.shouldDisplayNotify = shouldDisplayNotify @@ -28,7 +30,6 @@ import SwiftUI } func follow() async throws { - guard let client else { return } do { relationship = try await client.post(endpoint: Accounts.follow(id: accountId, notify: false, reblogs: true)) relationshipUpdated(relationship) @@ -38,7 +39,6 @@ import SwiftUI } func unfollow() async throws { - guard let client else { return } do { relationship = try await client.post(endpoint: Accounts.unfollow(id: accountId)) relationshipUpdated(relationship) @@ -48,7 +48,6 @@ import SwiftUI } func refreshRelationship() async throws { - guard let client else { return } let relationships: [Relationship] = try await client.get(endpoint: Accounts.relationships(ids: [accountId])) if let relationship = relationships.first { self.relationship = relationship @@ -57,7 +56,6 @@ import SwiftUI } func toggleNotify() async throws { - guard let client else { return } do { relationship = try await client.post(endpoint: Accounts.follow(id: accountId, notify: !relationship.notifying, @@ -69,7 +67,6 @@ import SwiftUI } func toggleReboosts() async throws { - guard let client else { return } do { relationship = try await client.post(endpoint: Accounts.follow(id: accountId, notify: relationship.notifying, @@ -130,8 +127,5 @@ public struct FollowButton: View { } } .buttonStyle(.bordered) - .onAppear { - viewModel.client = client - } } } diff --git a/Packages/Lists/Sources/Lists/Edit/ListEditView.swift b/Packages/Lists/Sources/Lists/Edit/ListEditView.swift index 4e889f1b..cebeb2a5 100644 --- a/Packages/Lists/Sources/Lists/Edit/ListEditView.swift +++ b/Packages/Lists/Sources/Lists/Edit/ListEditView.swift @@ -150,7 +150,8 @@ public struct ListEditView: View { } })) } else { - FollowButton(viewModel: .init(accountId: account.id, + FollowButton(viewModel: .init(client: client, + accountId: account.id, relationship: relationship, shouldDisplayNotify: false, relationshipUpdated: { relationship in