Skip to content

Commit 7d61954

Browse files
committed
Remove agent key, use new props
1 parent 9fa33d2 commit 7d61954

File tree

6 files changed

+18
-21
lines changed

6 files changed

+18
-21
lines changed

VoiceAgent/App/AppView.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct AppView: View {
1111

1212
var body: some View {
1313
ZStack(alignment: .top) {
14-
if session.isReady {
14+
if session.isConnected {
1515
interactions()
1616
} else {
1717
start()
@@ -33,20 +33,20 @@ struct AppView: View {
3333
}
3434
#else
3535
.safeAreaInset(edge: .bottom) {
36-
if session.isReady, !keyboardFocus {
36+
if session.isConnected, !keyboardFocus {
3737
ControlBar(chat: $chat)
3838
.transition(.asymmetric(insertion: .move(edge: .bottom).combined(with: .opacity), removal: .opacity))
3939
}
4040
}
4141
#endif
4242
.background(.bg1)
4343
.animation(.default, value: chat)
44-
.animation(.default, value: session.isReady)
44+
.animation(.default, value: session.isConnected)
4545
.animation(.default, value: session.error?.localizedDescription)
4646
.animation(.default, value: localMedia.isCameraEnabled)
4747
.animation(.default, value: localMedia.isScreenShareEnabled)
4848
#if os(iOS)
49-
.sensoryFeedback(.impact, trigger: session.isListening) { !$0 && $1 }
49+
.sensoryFeedback(.impact, trigger: session.agent.agentState) { $0 == nil && $1 == .listening }
5050
#endif
5151
}
5252

VoiceAgent/ControlBar/ControlBar.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ struct ControlBar: View {
126126
Spacer()
127127
}
128128
.frame(width: Constants.buttonWidth)
129-
.disabled(!session.hasAgent)
129+
.disabled(!session.agent.isConnected)
130130
}
131131

132132
@ViewBuilder
@@ -146,7 +146,7 @@ struct ControlBar: View {
146146
borderColor: .separator1
147147
)
148148
)
149-
.disabled(!session.hasAgent)
149+
.disabled(!session.agent.isConnected)
150150
}
151151

152152
@ViewBuilder
@@ -166,7 +166,7 @@ struct ControlBar: View {
166166
borderColor: .separator1
167167
)
168168
)
169-
.disabled(!session.hasAgent)
169+
.disabled(!session.agent.isConnected)
170170
}
171171

172172
@ViewBuilder
@@ -186,7 +186,7 @@ struct ControlBar: View {
186186
borderColor: .separatorSerious
187187
)
188188
)
189-
.disabled(session.connectionState == .disconnected)
189+
.disabled(!session.isConnected)
190190
}
191191
}
192192

VoiceAgent/Helpers/Environment.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ import SwiftUI
33

44
extension EnvironmentValues {
55
@Entry var namespace: Namespace.ID? // don't initialize outside View
6-
@Entry var agent: Agent?
76
}

VoiceAgent/Interactions/TextInteractionView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import SwiftUI
1010
///
1111
/// Additionally, the view shows a complete chat view with text input capabilities.
1212
struct TextInteractionView: View {
13+
@EnvironmentObject private var session: Session
1314
@EnvironmentObject private var localMedia: LocalMedia
14-
@Environment(\.agent) private var agent
1515

1616
@FocusState.Binding var keyboardFocus: Bool
1717

@@ -40,12 +40,12 @@ struct TextInteractionView: View {
4040
HStack {
4141
Spacer()
4242
AgentParticipantView()
43-
.frame(maxWidth: agent?.avatarVideoTrack != nil ? 50 * .grid : 25 * .grid)
43+
.frame(maxWidth: session.agent.avatarVideoTrack != nil ? 50 * .grid : 25 * .grid)
4444
ScreenShareView()
4545
LocalParticipantView()
4646
Spacer()
4747
}
48-
.frame(height: localMedia.isCameraEnabled || localMedia.isScreenShareEnabled || agent?.avatarVideoTrack != nil ? 50 * .grid : 25 * .grid)
48+
.frame(height: localMedia.isCameraEnabled || localMedia.isScreenShareEnabled || session.agent.avatarVideoTrack != nil ? 50 * .grid : 25 * .grid)
4949
.safeAreaPadding()
5050
}
5151
}

VoiceAgent/Participant/AgentParticipantView.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ import LiveKitComponents
55
/// - Note: If both are unavailable, the view will show a placeholder visualizer.
66
struct AgentParticipantView: View {
77
@EnvironmentObject private var session: Session
8-
@Environment(\.agent) private var agent
98
@Environment(\.namespace) private var namespace
109
/// Reveals the avatar camera view when true.
1110
@SceneStorage("videoTransition") private var videoTransition = false
1211

1312
var body: some View {
1413
ZStack {
15-
if let avatarVideoTrack = agent?.avatarVideoTrack {
14+
if let avatarVideoTrack = session.agent.avatarVideoTrack {
1615
SwiftUIVideoView(avatarVideoTrack)
1716
.clipShape(RoundedRectangle(cornerRadius: .cornerRadiusPerPlatform))
1817
.aspectRatio(avatarVideoTrack.aspectRatio, contentMode: .fit)
19-
.padding(.horizontal, agent?.avatarVideoTrack?.aspectRatio == 1 ? 4 * .grid : .zero)
18+
.padding(.horizontal, session.agent.avatarVideoTrack?.aspectRatio == 1 ? 4 * .grid : .zero)
2019
.shadow(radius: 20, y: 10)
2120
.mask(
2221
GeometryReader { proxy in
@@ -31,15 +30,15 @@ struct AgentParticipantView: View {
3130
.onAppear {
3231
videoTransition = true
3332
}
34-
} else if let audioTrack = agent?.audioTrack {
33+
} else if let audioTrack = session.agent.audioTrack {
3534
BarAudioVisualizer(audioTrack: audioTrack,
36-
agentState: agent?.state ?? .listening,
35+
agentState: session.agent.agentState ?? .listening,
3736
barCount: 5,
3837
barSpacingFactor: 0.05,
3938
barMinOpacity: 0.1)
4039
.frame(maxWidth: 75 * .grid, maxHeight: 48 * .grid)
4140
.transition(.opacity)
42-
} else if session.isReady {
41+
} else if session.isConnected {
4342
BarAudioVisualizer(audioTrack: nil,
4443
agentState: .listening,
4544
barCount: 1,
@@ -48,7 +47,7 @@ struct AgentParticipantView: View {
4847
.transition(.opacity)
4948
}
5049
}
51-
.animation(.snappy, value: agent?.audioTrack?.id)
50+
.animation(.snappy, value: session.agent.audioTrack?.id)
5251
.matchedGeometryEffect(id: "agent", in: namespace!)
5352
}
5453
}

VoiceAgent/VoiceAgentApp.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct VoiceAgentApp: App {
1414
// - Create .env.xcconfig with your LIVEKIT_SANDBOX_ID
1515
private static let sandboxId = Bundle.main.object(forInfoDictionaryKey: "LiveKitSandboxId") as! String
1616

17-
@StateObject private var session = Session(
17+
private let session = Session(
1818
tokenSource: SandboxTokenSource(id: Self.sandboxId),
1919
options: SessionOptions(room: Room(roomOptions: RoomOptions(defaultScreenShareCaptureOptions: ScreenShareCaptureOptions(useBroadcastExtension: true))))
2020
)
@@ -23,7 +23,6 @@ struct VoiceAgentApp: App {
2323
WindowGroup {
2424
AppView()
2525
.environmentObject(session)
26-
.environment(\.agent, session.agent)
2726
.environmentObject(LocalMedia(session: session))
2827
}
2928
#if os(macOS)

0 commit comments

Comments
 (0)