Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions Sources/LiveKitComponents/UI/Buttons/AsyncButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2025 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import SwiftUI

/// A drop-in replacement `Button` that executes an async action and shows a busy label when in progress.
///
/// - Parameters:
/// - action: The async action to execute.
/// - label: The label to show when not busy.
/// - busyLabel: The label to show when busy. Defaults to an empty view.
public struct AsyncButton<Label: View, BusyLabel: View>: View {
private let action: () async -> Void

@ViewBuilder private let label: Label
@ViewBuilder private let busyLabel: BusyLabel

@State private var isBusy = false

public init(
action: @escaping () async -> Void,
@ViewBuilder label: () -> Label,
@ViewBuilder busyLabel: () -> BusyLabel = EmptyView.init
) {
self.action = action
self.label = label()
self.busyLabel = busyLabel()
}

public var body: some View {
Button {
isBusy = true
Task {
await action()
isBusy = false
}
} label: {
if isBusy {
if busyLabel is EmptyView {
label
} else {
busyLabel
}
} else {
label
}
}
.disabled(isBusy)
}
}
62 changes: 62 additions & 0 deletions Sources/LiveKitComponents/UI/Chat/ChatScrollView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2025 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import LiveKit
import SwiftUI

public struct ChatScrollView<Content: View>: View {
public typealias MessageBuilder = (ReceivedMessage) -> Content

@LiveKitConversation private var conversation
@ViewBuilder private let messageBuilder: MessageBuilder

public init(messageBuilder: @escaping MessageBuilder) {
self.messageBuilder = messageBuilder
}

public var body: some View {
ScrollViewReader { scrollView in
ScrollView {
LazyVStack {
ForEach(conversation.messages.values.reversed()) { message in
messageBuilder(message)
.upsideDown()
.id(message.id)
}
}
}
.onChange(of: conversation.messages.count) { _ in
scrollView.scrollTo(conversation.messages.keys.last)
}
.upsideDown()
.animation(.default, value: conversation.messages)
}
}
}

private struct UpsideDown: ViewModifier {
func body(content: Content) -> some View {
content
.rotationEffect(.radians(Double.pi))
.scaleEffect(x: -1, y: 1, anchor: .center)
}
}

private extension View {
func upsideDown() -> some View {
modifier(UpsideDown())
}
}
11 changes: 11 additions & 0 deletions Sources/LiveKitComponents/UI/Visualizer/BarAudioVisualizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ public struct BarAudioVisualizer: View {
animationProperties = PhaseAnimationProperties(barCount: barCount)
}

public init(agent: Agent?,
barColor: Color = .primary,
barCount: Int = 5,
barCornerRadius: CGFloat = 100,
barSpacingFactor: CGFloat = 0.015,
barMinOpacity: CGFloat = 0.16,
isCentered: Bool = true)
{
self.init(audioTrack: agent?.audioTrack, agentState: agent?.state ?? .listening, barColor: barColor, barCount: barCount, barCornerRadius: barCornerRadius, barSpacingFactor: barSpacingFactor, barMinOpacity: barMinOpacity, isCentered: isCentered)
}

public var body: some View {
GeometryReader { geometry in
let highlightingSequence = animationProperties.highlightingSequence(agentState: agentState)
Expand Down
Loading