Skip to content

Commit 0427e11

Browse files
committed
Use style instead of inline configuration
1 parent 0d6e84e commit 0427e11

File tree

1 file changed

+47
-30
lines changed

1 file changed

+47
-30
lines changed

firebaseai/FirebaseAIExample/Features/Live/Views/ConnectButton.swift

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ struct ConnectButton: View {
3939
}
4040
}
4141

42+
var body: some View {
43+
Button(action: onClick) {
44+
Label(title, systemImage: image).padding()
45+
}
46+
.buttonStyle(.connect(state: state, gradiantAngle: gradientAngle))
47+
.onAppear {
48+
withAnimation(.linear(duration: 5).repeatForever(autoreverses: false)) {
49+
self.gradientAngle = .degrees(360)
50+
}
51+
}
52+
}
53+
54+
private func onClick() {
55+
Task {
56+
if isConnected {
57+
await onDisconnect()
58+
} else {
59+
await onConnect()
60+
}
61+
}
62+
}
63+
}
64+
65+
struct ConnectButtonStyle: ButtonStyle {
66+
var state: LiveViewModelState
67+
var gradientAngle: Angle
68+
4269
private var color: Color {
4370
switch state {
4471
case .connected: Color(.systemRed)
@@ -55,38 +82,28 @@ struct ConnectButton: View {
5582
}
5683
}
5784

58-
var body: some View {
59-
Button(action: onClick) {
60-
Label(title, systemImage: image).padding()
61-
}
62-
.disabled(state == .connecting)
63-
.overlay(
64-
RoundedRectangle(cornerRadius: 35)
65-
.stroke(
66-
AngularGradient(
67-
gradient: Gradient(colors: gradientColors),
68-
center: .center,
69-
startAngle: gradientAngle,
70-
endAngle: gradientAngle + .degrees(360)
71-
),
72-
lineWidth: 3
73-
)
74-
).tint(color)
75-
.onAppear {
76-
withAnimation(.linear(duration: 5).repeatForever(autoreverses: false)) {
77-
self.gradientAngle = .degrees(360)
78-
}
79-
}
85+
func makeBody(configuration: Configuration) -> some View {
86+
configuration.label
87+
.disabled(state == .connecting)
88+
.overlay(
89+
RoundedRectangle(cornerRadius: 35)
90+
.stroke(
91+
AngularGradient(
92+
gradient: Gradient(colors: gradientColors),
93+
center: .center,
94+
startAngle: gradientAngle,
95+
endAngle: gradientAngle + .degrees(360)
96+
),
97+
lineWidth: 3
98+
)
99+
)
100+
.foregroundStyle(color)
80101
}
102+
}
81103

82-
private func onClick() {
83-
Task {
84-
if isConnected {
85-
await onDisconnect()
86-
} else {
87-
await onConnect()
88-
}
89-
}
104+
extension ButtonStyle where Self == ConnectButtonStyle {
105+
static func connect(state: LiveViewModelState, gradiantAngle: Angle) -> ConnectButtonStyle {
106+
ConnectButtonStyle(state: state, gradientAngle: gradiantAngle)
90107
}
91108
}
92109

0 commit comments

Comments
 (0)