Fix follow button not refreshing

This commit is contained in:
Thomas Ricouard 2024-06-15 09:43:43 +02:00
parent ce150ea629
commit c8c379281f
4 changed files with 10 additions and 13 deletions

View file

@ -120,7 +120,8 @@ import SwiftUI
featuredTags.sort { $0.statusesCountInt > $1.statusesCountInt } featuredTags.sort { $0.statusesCountInt > $1.statusesCountInt }
relationship = data.relationships.first relationship = data.relationships.first
if let relationship { if let relationship {
followButtonViewModel = .init(accountId: accountId, followButtonViewModel = .init(client: client,
accountId: accountId,
relationship: relationship, relationship: relationship,
shouldDisplayNotify: true, shouldDisplayNotify: true,
relationshipUpdated: { [weak self] relationship in relationshipUpdated: { [weak self] relationship in

View file

@ -95,7 +95,8 @@ public struct AccountsListRow: View {
let relationShip = viewModel.relationShip let relationShip = viewModel.relationShip
{ {
VStack(alignment: .center) { VStack(alignment: .center) {
FollowButton(viewModel: .init(accountId: viewModel.account.id, FollowButton(viewModel: .init(client: client,
accountId: viewModel.account.id,
relationship: relationShip, relationship: relationShip,
shouldDisplayNotify: false, shouldDisplayNotify: false,
relationshipUpdated: { _ in })) relationshipUpdated: { _ in }))

View file

@ -9,18 +9,20 @@ import SwiftUI
@MainActor @MainActor
@Observable public class FollowButtonViewModel { @Observable public class FollowButtonViewModel {
var client: Client? let client: Client
public let accountId: String public let accountId: String
public let shouldDisplayNotify: Bool public let shouldDisplayNotify: Bool
public let relationshipUpdated: (Relationship) -> Void public let relationshipUpdated: (Relationship) -> Void
public private(set) var relationship: Relationship public private(set) var relationship: Relationship
public init(accountId: String, public init(client: Client,
accountId: String,
relationship: Relationship, relationship: Relationship,
shouldDisplayNotify: Bool, shouldDisplayNotify: Bool,
relationshipUpdated: @escaping ((Relationship) -> Void)) relationshipUpdated: @escaping ((Relationship) -> Void))
{ {
self.client = client
self.accountId = accountId self.accountId = accountId
self.relationship = relationship self.relationship = relationship
self.shouldDisplayNotify = shouldDisplayNotify self.shouldDisplayNotify = shouldDisplayNotify
@ -28,7 +30,6 @@ import SwiftUI
} }
func follow() async throws { func follow() async throws {
guard let client else { return }
do { do {
relationship = try await client.post(endpoint: Accounts.follow(id: accountId, notify: false, reblogs: true)) relationship = try await client.post(endpoint: Accounts.follow(id: accountId, notify: false, reblogs: true))
relationshipUpdated(relationship) relationshipUpdated(relationship)
@ -38,7 +39,6 @@ import SwiftUI
} }
func unfollow() async throws { func unfollow() async throws {
guard let client else { return }
do { do {
relationship = try await client.post(endpoint: Accounts.unfollow(id: accountId)) relationship = try await client.post(endpoint: Accounts.unfollow(id: accountId))
relationshipUpdated(relationship) relationshipUpdated(relationship)
@ -48,7 +48,6 @@ import SwiftUI
} }
func refreshRelationship() async throws { func refreshRelationship() async throws {
guard let client else { return }
let relationships: [Relationship] = try await client.get(endpoint: Accounts.relationships(ids: [accountId])) let relationships: [Relationship] = try await client.get(endpoint: Accounts.relationships(ids: [accountId]))
if let relationship = relationships.first { if let relationship = relationships.first {
self.relationship = relationship self.relationship = relationship
@ -57,7 +56,6 @@ import SwiftUI
} }
func toggleNotify() async throws { func toggleNotify() async throws {
guard let client else { return }
do { do {
relationship = try await client.post(endpoint: Accounts.follow(id: accountId, relationship = try await client.post(endpoint: Accounts.follow(id: accountId,
notify: !relationship.notifying, notify: !relationship.notifying,
@ -69,7 +67,6 @@ import SwiftUI
} }
func toggleReboosts() async throws { func toggleReboosts() async throws {
guard let client else { return }
do { do {
relationship = try await client.post(endpoint: Accounts.follow(id: accountId, relationship = try await client.post(endpoint: Accounts.follow(id: accountId,
notify: relationship.notifying, notify: relationship.notifying,
@ -130,8 +127,5 @@ public struct FollowButton: View {
} }
} }
.buttonStyle(.bordered) .buttonStyle(.bordered)
.onAppear {
viewModel.client = client
}
} }
} }

View file

@ -150,7 +150,8 @@ public struct ListEditView: View {
} }
})) }))
} else { } else {
FollowButton(viewModel: .init(accountId: account.id, FollowButton(viewModel: .init(client: client,
accountId: account.id,
relationship: relationship, relationship: relationship,
shouldDisplayNotify: false, shouldDisplayNotify: false,
relationshipUpdated: { relationship in relationshipUpdated: { relationship in