Using AsyncButton from button kit to improve following and related buttons (#1888)

This commit is contained in:
Thomas Durand 2024-01-14 10:51:54 +01:00 committed by GitHub
parent 7c8ea23ae9
commit 788fab930b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 25 deletions

View file

@ -9,6 +9,15 @@
"version" : "2.1.0" "version" : "2.1.0"
} }
}, },
{
"identity" : "buttonkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Dean151/ButtonKit",
"state" : {
"revision" : "377f5bab4ed047704316d531e0826d4de5ebf6a4",
"version" : "0.1.1"
}
},
{ {
"identity" : "emojitext", "identity" : "emojitext",
"kind" : "remoteSourceControl", "kind" : "remoteSourceControl",

View file

@ -21,6 +21,7 @@ let package = Package(
.package(name: "Models", path: "../Models"), .package(name: "Models", path: "../Models"),
.package(name: "StatusKit", path: "../StatusKit"), .package(name: "StatusKit", path: "../StatusKit"),
.package(name: "Env", path: "../Env"), .package(name: "Env", path: "../Env"),
.package(url: "https://github.com/Dean151/ButtonKit", from: "0.1.1"),
], ],
targets: [ targets: [
.target( .target(
@ -30,6 +31,7 @@ let package = Package(
.product(name: "Models", package: "Models"), .product(name: "Models", package: "Models"),
.product(name: "StatusKit", package: "StatusKit"), .product(name: "StatusKit", package: "StatusKit"),
.product(name: "Env", package: "Env"), .product(name: "Env", package: "Env"),
.product(name: "ButtonKit", package: "ButtonKit")
], ],
swiftSettings: [ swiftSettings: [
.enableExperimentalFeature("StrictConcurrency"), .enableExperimentalFeature("StrictConcurrency"),

View file

@ -1,3 +1,4 @@
import ButtonKit
import Combine import Combine
import Foundation import Foundation
import Models import Models
@ -13,7 +14,6 @@ import SwiftUI
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 private(set) var isUpdating: Bool = false
public init(accountId: String, public init(accountId: String,
relationship: Relationship, relationship: Relationship,
@ -26,31 +26,29 @@ import SwiftUI
self.relationshipUpdated = relationshipUpdated self.relationshipUpdated = relationshipUpdated
} }
func follow() async { func follow() async throws {
guard let client else { return } guard let client else { return }
isUpdating = true
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)
} catch { } catch {
print("Error while following: \(error.localizedDescription)") print("Error while following: \(error.localizedDescription)")
throw error
} }
isUpdating = false
} }
func unfollow() async { func unfollow() async throws {
guard let client else { return } guard let client else { return }
isUpdating = true
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)
} catch { } catch {
print("Error while unfollowing: \(error.localizedDescription)") print("Error while unfollowing: \(error.localizedDescription)")
throw error
} }
isUpdating = false
} }
func toggleNotify() async { func toggleNotify() async throws {
guard let client else { return } 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,
@ -59,10 +57,11 @@ import SwiftUI
relationshipUpdated(relationship) relationshipUpdated(relationship)
} catch { } catch {
print("Error while following: \(error.localizedDescription)") print("Error while following: \(error.localizedDescription)")
throw error
} }
} }
func toggleReboosts() async { func toggleReboosts() async throws {
guard let client else { return } 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,
@ -71,6 +70,7 @@ import SwiftUI
relationshipUpdated(relationship) relationshipUpdated(relationship)
} catch { } catch {
print("Error while switching reboosts: \(error.localizedDescription)") print("Error while switching reboosts: \(error.localizedDescription)")
throw error
} }
} }
} }
@ -85,13 +85,11 @@ public struct FollowButton: View {
public var body: some View { public var body: some View {
VStack(alignment: .trailing) { VStack(alignment: .trailing) {
Button { AsyncButton {
Task { if viewModel.relationship.following {
if viewModel.relationship.following { try await viewModel.unfollow()
await viewModel.unfollow() } else {
} else { try await viewModel.follow()
await viewModel.follow()
}
} }
} label: { } label: {
if viewModel.relationship.requested == true { if viewModel.relationship.requested == true {
@ -106,29 +104,26 @@ public struct FollowButton: View {
viewModel.shouldDisplayNotify viewModel.shouldDisplayNotify
{ {
HStack { HStack {
Button { AsyncButton {
Task { try await viewModel.toggleNotify()
await viewModel.toggleNotify()
}
} label: { } label: {
Image(systemName: viewModel.relationship.notifying ? "bell.fill" : "bell") Image(systemName: viewModel.relationship.notifying ? "bell.fill" : "bell")
} }
.accessibilityLabel("accessibility.tabs.profile.user-notifications.label") .accessibilityLabel("accessibility.tabs.profile.user-notifications.label")
.accessibilityValue(viewModel.relationship.notifying ? "accessibility.general.toggle.on" : "accessibility.general.toggle.off") .accessibilityValue(viewModel.relationship.notifying ? "accessibility.general.toggle.on" : "accessibility.general.toggle.off")
Button { AsyncButton {
Task { try await viewModel.toggleReboosts()
await viewModel.toggleReboosts()
}
} label: { } label: {
Image(viewModel.relationship.showingReblogs ? "Rocket.Fill" : "Rocket") Image(viewModel.relationship.showingReblogs ? "Rocket.Fill" : "Rocket")
} }
.accessibilityLabel("accessibility.tabs.profile.user-reblogs.label") .accessibilityLabel("accessibility.tabs.profile.user-reblogs.label")
.accessibilityValue(viewModel.relationship.showingReblogs ? "accessibility.general.toggle.on" : "accessibility.general.toggle.off") .accessibilityValue(viewModel.relationship.showingReblogs ? "accessibility.general.toggle.on" : "accessibility.general.toggle.off")
} }
.asyncButtonStyle(.none)
.disabledWhenLoading()
} }
} }
.buttonStyle(.bordered) .buttonStyle(.bordered)
.disabled(viewModel.isUpdating)
.onAppear { .onAppear {
viewModel.client = client viewModel.client = client
} }