Skip to content

Instantly share code, notes, and snippets.

import SwiftUI
struct ErrorView: View {
var body: some View {
Text("Something went wrong, please try sending your message again.")
.foregroundStyle(Color.red)
}
}
import SwiftUI
struct LoadingView: View {
var body: some View {
Image(systemName: "ellipsis.rectangle.fill")
.symbolRenderingMode(.hierarchical)
.foregroundStyle(Color.gray)
.font(.largeTitle)
.symbolEffect(.variableColor, options: .repeating, isActive: true)
@MainActor
@Observable
class ChatViewModel {
// Private
private let client: GeminiClient = GeminiClient()
// Public
var error: Error?
var waitingForResponse: Bool = false
@MainActor
@Observable
class ChatViewModel {
// Private
private let client: GeminiClient = GeminiClient()
// Public
var error: Error?
var waitingForResponse: Bool = false
import Foundation
import GoogleGenerativeAI
class GeminiClient {
private lazy var model = GenerativeModel(name: "gemini-pro", apiKey: APIKey.default)
private lazy var chat: Chat = model.startChat()
func send(message: String) async -> Result<String, GenerateContentError> {
do {
class GeminiClient {
enum APIKey {
// Fetch the API key from `GeminiChat.plist`
static var `default`: String {
guard let filePath = Bundle.main.path(forResource: "GeminiChat", ofType: "plist")
else {
fatalError("Couldn't find file 'GeminiChat.plist'.")
}
let plist = NSDictionary(contentsOfFile: filePath)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>API_KEY</key>
<string>123456789</string>
</dict>
</plist>
struct ContentView: View {
private let viewModel = HobbyViewModel()
@State private var showAlert: Bool = false
@State private var randomHobby: Hobby?
var body: some View {
Button("Random hobby") {
randomHobby = viewModel.randomHobby()
struct ContentView: View {
@State private var showAlert: Bool = false
var body: some View {
Button("Show alert") {
showAlert = true
}.alert(
"Hello!",
isPresented: $showAlert,
struct ContentView: View {
@State private var showAlert: Bool = false
var body: some View {
Button("Show alert") {
showAlert = true
}.alert("Hello!", isPresented: $showAlert, actions: {})
}
}