This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| struct ErrorView: View { | |
| var body: some View { | |
| Text("Something went wrong, please try sending your message again.") | |
| .foregroundStyle(Color.red) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @MainActor | |
| @Observable | |
| class ChatViewModel { | |
| // Private | |
| private let client: GeminiClient = GeminiClient() | |
| // Public | |
| var error: Error? | |
| var waitingForResponse: Bool = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @MainActor | |
| @Observable | |
| class ChatViewModel { | |
| // Private | |
| private let client: GeminiClient = GeminiClient() | |
| // Public | |
| var error: Error? | |
| var waitingForResponse: Bool = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct ContentView: View { | |
| @State private var showAlert: Bool = false | |
| var body: some View { | |
| Button("Show alert") { | |
| showAlert = true | |
| }.alert( | |
| "Hello!", | |
| isPresented: $showAlert, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct ContentView: View { | |
| @State private var showAlert: Bool = false | |
| var body: some View { | |
| Button("Show alert") { | |
| showAlert = true | |
| }.alert("Hello!", isPresented: $showAlert, actions: {}) | |
| } | |
| } |