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 }
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

View file

@ -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 }))

View file

@ -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
}
}
}

View file

@ -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