This file contains 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 | |
struct ChatView: View { | |
@Bindable private var viewModel: ChatViewModel = ChatViewModel() | |
var body: some View { | |
NavigationStack { | |
VStack { | |
ChatMessagesList(viewModel: viewModel) |
This file contains 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
SendMessageButton { | |
Task { | |
await viewModel.sendMessage() | |
} | |
} | |
.disabled(!viewModel.canSendMessage) |
This file contains 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 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) |
This file contains 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 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 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 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 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 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 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> |
NewerOlder