Skip to content

Instantly share code, notes, and snippets.

struct ChatView: View {
@Bindable private var viewModel: ChatViewModel = ChatViewModel()
var body: some View {
NavigationStack {
VStack {
ChatMessagesList(viewModel: viewModel)
HStack {
MessageTextField(draftMessage: $viewModel.draftMessage)
func translateAllMessages(using session: TranslationSession) async {
Task {
let requests: [TranslationSession.Request] = messages.enumerated().map { (index, message) in
// Assign each request a client identifier.
.init(sourceText: message.text, clientIdentifier: "\(index)")
}
do {
for try await response in session.translate(batch: requests) {
func triggerTranslation() {
if translationConfiguration == nil {
translationConfiguration = TranslationSession.Configuration(
source: .init(identifier: "en-US"),
target: .init(identifier: "es-419")
)
return
}
translationConfiguration?.invalidate()
}
import Translation
@MainActor
@Observable
class ChatViewModel {
var translationConfiguration: TranslationSession.Configuration? = nil
// ...
}
struct ContentView: View {
@State private var showTranslation = false
@State private var displayedText = quote
static let quote = "Here’s to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They’re not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can’t do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do."
var body: some View {
VStack {
// ...
struct ContentView: View {
@State private var showTranslation = false
private var displayedText = quote
static let quote = "Here’s to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They’re not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can’t do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do."
// ...
}
struct ContentView: View {
@State private var showTranslation = false
private var displayedText = quote
static let quote = "Here’s to the crazy ones. The misfits. The rebels. The troublemakers. The round pegs in the square holes. The ones who see things differently. They’re not fond of rules. And they have no respect for the status quo. You can quote them, disagree with them, glorify or vilify them. About the only thing you can’t do is ignore them. Because they change things. They push the human race forward. And while some may see them as the crazy ones, we see genius. Because the people who are crazy enough to think they can change the world, are the ones who do."
var body: some View {
VStack {
Spacer()
@MainActor
struct ChatView: View {
@Bindable private var viewModel: ChatViewModel = ChatViewModel()
var body: some View {
NavigationStack {
VStack {
ChatMessagesList(viewModel: viewModel)
SendMessageButton {
Task {
await viewModel.sendMessage()
}
}
.disabled(!viewModel.canSendMessage)
struct ChatMessagesList: View {
@Bindable var viewModel: ChatViewModel
@State private var loadingViewId: UUID? = UUID()
var body: some View {
ScrollView {
ForEach(viewModel.messages) { message in
MessageCell(message: message)
.padding(message.role.padding)