|
| 1 | +// |
| 2 | +// AISelectionView.swift |
| 3 | +// AIChat |
| 4 | +// |
| 5 | +// Created by ziggzhang on 2024/7/21. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +struct AISelectionView: View { |
| 11 | + @ObservedObject var dataManager = AIGCDataManager.shared |
| 12 | + @Environment(\.presentationMode) var presentationMode |
| 13 | + |
| 14 | + @State private var chatName: String = "" |
| 15 | + @State private var selectedPlatform: AIPlatform = .chatGPT // Default to a valid platform |
| 16 | + @State private var apiKey: String = "" |
| 17 | + @State private var customAPIURL: String = "" |
| 18 | + @State private var isCustomPlatform: Bool = false |
| 19 | + |
| 20 | + var body: some View { |
| 21 | + ScrollView { |
| 22 | + VStack(alignment: .leading, spacing: 16) { |
| 23 | + TextField("Enter Chat Name", text: $chatName) |
| 24 | + .textFieldStyle(RoundedBorderTextFieldStyle()) |
| 25 | + .padding(.horizontal) |
| 26 | + |
| 27 | + Picker("Select AI Platform", selection: $selectedPlatform) { |
| 28 | + ForEach(AIPlatform.allCases) { platform in |
| 29 | + Text(platform.rawValue).tag(platform) |
| 30 | + } |
| 31 | + } |
| 32 | + .pickerStyle(MenuPickerStyle()) |
| 33 | + .padding(.horizontal) |
| 34 | + .onChange(of: selectedPlatform) { newValue in |
| 35 | + // Update isCustomPlatform based on selected platform |
| 36 | + isCustomPlatform = (newValue == .custom) |
| 37 | + } |
| 38 | + |
| 39 | + if isCustomPlatform { |
| 40 | + TextField("Enter Custom API URL", text: $customAPIURL) |
| 41 | + .textFieldStyle(RoundedBorderTextFieldStyle()) |
| 42 | + .padding(.horizontal) |
| 43 | + } |
| 44 | + |
| 45 | + TextField("Enter API Key", text: $apiKey) |
| 46 | + .textFieldStyle(RoundedBorderTextFieldStyle()) |
| 47 | + .padding(.horizontal) |
| 48 | + .textInputAutocapitalization(.none) // Avoid auto-capitalization for API keys |
| 49 | + |
| 50 | + Button("Save") { |
| 51 | + // Handle saving chat based on selection |
| 52 | + if selectedPlatform == .custom { |
| 53 | + // Handle custom API URL case |
| 54 | + print("Custom API URL: \(customAPIURL)") |
| 55 | + // Save or use custom API URL as needed |
| 56 | + } else { |
| 57 | + dataManager.addChat(name: chatName, platform: selectedPlatform, apiKey: apiKey) |
| 58 | + } |
| 59 | + presentationMode.wrappedValue.dismiss() |
| 60 | + } |
| 61 | + .padding(.horizontal) |
| 62 | + .padding(.bottom) |
| 63 | + } |
| 64 | + .padding(.top) |
| 65 | + } |
| 66 | + .navigationTitle("Select AI Platform") |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +struct AISelectionView_Previews: PreviewProvider { |
| 71 | + static var previews: some View { |
| 72 | + AISelectionView() |
| 73 | + } |
| 74 | +} |
0 commit comments