2023-03-14 18:50:19 +01:00
|
|
|
import DesignSystem
|
|
|
|
import Env
|
|
|
|
import SwiftUI
|
|
|
|
|
2023-09-19 09:18:20 +02:00
|
|
|
@MainActor
|
2023-03-14 18:50:19 +01:00
|
|
|
struct TranslationSettingsView: View {
|
2023-09-19 09:18:20 +02:00
|
|
|
@Environment(UserPreferences.self) private var preferences
|
2023-09-18 21:03:52 +02:00
|
|
|
@Environment(Theme.self) private var theme
|
2023-03-14 18:50:19 +01:00
|
|
|
|
|
|
|
@State private var apiKey: String = ""
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Form {
|
2023-09-19 09:18:20 +02:00
|
|
|
deepLToggle
|
2023-03-14 18:50:19 +01:00
|
|
|
if preferences.alwaysUseDeepl {
|
|
|
|
Section("settings.translation.user-api-key") {
|
2023-09-19 09:18:20 +02:00
|
|
|
deepLPicker
|
2023-03-14 18:50:19 +01:00
|
|
|
SecureField("settings.translation.user-api-key", text: $apiKey)
|
|
|
|
.textContentType(.password)
|
|
|
|
}
|
2023-09-14 11:04:14 +02:00
|
|
|
.onAppear {
|
|
|
|
readValue()
|
|
|
|
}
|
2023-03-14 18:50:19 +01:00
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
|
|
|
|
if apiKey.isEmpty {
|
|
|
|
Section {
|
|
|
|
Link(destination: URL(string: "https://www.deepl.com/pro-api")!) {
|
|
|
|
Text("settings.translation.needed-message")
|
|
|
|
.foregroundColor(.red)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
}
|
|
|
|
}
|
2023-09-19 09:18:20 +02:00
|
|
|
autoDetectSection
|
2023-03-14 18:50:19 +01:00
|
|
|
}
|
2023-03-17 06:06:09 +01:00
|
|
|
.navigationTitle("settings.translation.navigation-title")
|
2023-03-14 18:50:19 +01:00
|
|
|
.scrollContentBackground(.hidden)
|
|
|
|
.background(theme.secondaryBackgroundColor)
|
2023-09-18 07:01:23 +02:00
|
|
|
.onChange(of: apiKey) {
|
|
|
|
writeNewValue()
|
|
|
|
}
|
2023-03-14 18:50:19 +01:00
|
|
|
.onAppear(perform: updatePrefs)
|
|
|
|
}
|
2023-09-19 09:18:20 +02:00
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private var deepLToggle: some View {
|
|
|
|
@Bindable var preferences = preferences
|
|
|
|
Toggle(isOn: $preferences.alwaysUseDeepl) {
|
|
|
|
Text("settings.translation.always-deepl")
|
|
|
|
}
|
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private var deepLPicker: some View {
|
|
|
|
@Bindable var preferences = preferences
|
|
|
|
Picker("settings.translation.api-key-type", selection: $preferences.userDeeplAPIFree) {
|
|
|
|
Text("DeepL API Free").tag(true)
|
|
|
|
Text("DeepL API Pro").tag(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private var autoDetectSection: some View {
|
|
|
|
@Bindable var preferences = preferences
|
|
|
|
Section {
|
|
|
|
Toggle(isOn: $preferences.autoDetectPostLanguage) {
|
|
|
|
Text("settings.translation.auto-detect-post-language")
|
|
|
|
}
|
|
|
|
} footer: {
|
|
|
|
Text("settings.translation.auto-detect-post-language-footer")
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 18:50:19 +01:00
|
|
|
|
|
|
|
private func writeNewValue() {
|
|
|
|
writeNewValue(value: apiKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func writeNewValue(value: String) {
|
|
|
|
DeepLUserAPIHandler.write(value: value)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func readValue() {
|
|
|
|
if let apiKey = DeepLUserAPIHandler.readIfAllowed() {
|
|
|
|
self.apiKey = apiKey
|
|
|
|
} else {
|
|
|
|
apiKey = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updatePrefs() {
|
|
|
|
DeepLUserAPIHandler.deactivateToggleIfNoKey()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TranslationSettingsView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
TranslationSettingsView()
|
2023-09-19 09:18:20 +02:00
|
|
|
.environment(UserPreferences.shared)
|
2023-03-14 18:50:19 +01:00
|
|
|
}
|
|
|
|
}
|