|
| 1 | +import SwiftUI |
| 2 | +import KotlinSharedUI |
| 3 | + |
| 4 | +struct AiConfigScreen: View { |
| 5 | + @StateObject private var presenter = KotlinPresenter(presenter: SettingsPresenter()) |
| 6 | + @StateObject private var serverPresenter = KotlinPresenter(presenter: FlareServerProviderPresenter()) |
| 7 | + @State private var serverName: String = "" |
| 8 | + var body: some View { |
| 9 | + Form { |
| 10 | + Section { |
| 11 | + TextField("ai_config_server_provider_placeholder", text: $serverName, prompt: Text("ai_config_server_provider_placeholder")) |
| 12 | + .safeAreaInset(edge: .trailing) { |
| 13 | + StateView(state: serverPresenter.state.serverValidation) { _ in |
| 14 | + Image("fa-check").foregroundColor(.green) |
| 15 | + } errorContent: { _ in |
| 16 | + Image("fa-xmark").foregroundColor(.red) |
| 17 | + } loadingContent: { |
| 18 | + ProgressView().frame(width: 20, height: 20) |
| 19 | + } |
| 20 | + } |
| 21 | + .onChange(of: serverName, { oldValue, newValue in |
| 22 | + serverPresenter.state.checkServer(value: newValue) |
| 23 | + }) |
| 24 | + .onChange(of: serverPresenter.state.serverValidation) { oldValue, newValue in |
| 25 | + if case .success = onEnum(of: newValue) { |
| 26 | + serverPresenter.state.confirm() |
| 27 | + } |
| 28 | + } |
| 29 | + } header: { |
| 30 | + Text("ai_config_server_provider_section_header") |
| 31 | + } |
| 32 | + StateView(state: presenter.state.appSettings) { settings in |
| 33 | + Section { |
| 34 | + Toggle(isOn: Binding(get: { |
| 35 | + settings.aiConfig.translation |
| 36 | + }, set: { newValue in |
| 37 | + presenter.state.updateAppSettings { current in |
| 38 | + current.doCopy(version: current.version, aiConfig: current.aiConfig.doCopy(translation: newValue, tldr: current.aiConfig.tldr)) |
| 39 | + } |
| 40 | + })) { |
| 41 | + Text("ai_config_translate") |
| 42 | + } |
| 43 | + Toggle(isOn: Binding(get: { |
| 44 | + settings.aiConfig.tldr |
| 45 | + }, set: { newValue in |
| 46 | + presenter.state.updateAppSettings { current in |
| 47 | + current.doCopy(version: current.version, aiConfig: current.aiConfig.doCopy(translation: current.aiConfig.translation, tldr: newValue)) |
| 48 | + } |
| 49 | + })) { |
| 50 | + Text("ai_config_summarize") |
| 51 | + } |
| 52 | + } header: { |
| 53 | + Text("ai_config_options_section_header") |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + .onChange(of: serverPresenter.state.currentServer, { oldValue, newValue in |
| 58 | + if case .success(let success) = onEnum(of: newValue) { |
| 59 | + serverName = String(success.data) |
| 60 | + } |
| 61 | + }) |
| 62 | + .navigationTitle("ai_config_title") |
| 63 | + } |
| 64 | +} |
0 commit comments