IceCubes/IceCubesApp/App/Tabs/Settings/TranslationSettingsView.swift

154 lines
4.0 KiB
Swift
Raw Normal View History

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 {
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 13:27:21 +02:00
translationSelector
if preferences.preferredTranslationType == .useDeepl {
2023-03-14 18:50:19 +01:00
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)
}
#if !os(visionOS)
2023-03-14 18:50:19 +01:00
.listRowBackground(theme.primaryBackgroundColor)
#endif
2024-02-14 12:48:14 +01:00
2023-03-14 18:50:19 +01:00
if apiKey.isEmpty {
Section {
Link(destination: URL(string: "https://www.deepl.com/pro-api")!) {
Text("settings.translation.needed-message")
.foregroundColor(.red)
}
}
#if !os(visionOS)
2023-03-14 18:50:19 +01:00
.listRowBackground(theme.primaryBackgroundColor)
#endif
2023-03-14 18:50:19 +01:00
}
}
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 13:27:21 +02:00
backgroundAPIKey
2023-09-19 09:18:20 +02:00
autoDetectSection
2023-03-14 18:50:19 +01:00
}
.navigationTitle("settings.translation.navigation-title")
#if !os(visionOS)
2024-02-14 12:48:14 +01:00
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
#endif
2024-02-14 12:48:14 +01:00
.onChange(of: apiKey) {
writeNewValue()
}
.onAppear(perform: updatePrefs)
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 13:27:21 +02:00
.onAppear(perform: readValue)
2023-03-14 18:50:19 +01:00
}
2023-09-19 09:18:20 +02:00
@ViewBuilder
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 13:27:21 +02:00
private var translationSelector: some View {
2023-09-19 09:18:20 +02:00
@Bindable var preferences = preferences
2024-05-14 12:20:25 +02:00
Picker("Translation Service", selection: $preferences.preferredTranslationType) {
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 13:27:21 +02:00
ForEach(allTTCases, id: \.self) { type in
Text(type.description).tag(type)
}
2023-09-19 09:18:20 +02:00
}
#if !os(visionOS)
2023-09-19 09:18:20 +02:00
.listRowBackground(theme.primaryBackgroundColor)
#endif
2023-09-19 09:18:20 +02:00
}
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 13:27:21 +02:00
var allTTCases: [TranslationType] {
TranslationType.allCases.filter { type in
if type != .useApple {
return true
}
#if canImport(_Translation_SwiftUI)
if #available(iOS 17.4, *) {
return true
} else {
return false
}
#else
return false
#endif
}
}
2023-09-19 09:18:20 +02:00
@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)
}
}
2023-09-19 09:18:20 +02:00
@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")
}
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 13:27:21 +02:00
#if !os(visionOS)
.listRowBackground(theme.primaryBackgroundColor)
#endif
}
@ViewBuilder
private var backgroundAPIKey: some View {
if preferences.preferredTranslationType != .useDeepl,
!apiKey.isEmpty
{
Section {
Text("The DeepL API Key is still stored!")
if preferences.preferredTranslationType == .useServerIfPossible {
Text("It can however still be used as a fallback for your instance's translation service.")
}
Button(role: .destructive) {
withAnimation {
writeNewValue(value: "")
readValue()
}
} label: {
Text("action.delete")
}
}
#if !os(visionOS)
.listRowBackground(theme.primaryBackgroundColor)
#endif
}
2023-09-19 09:18:20 +02:00
}
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() {
Implement Apple Translate (#2065) * Implement a first version of Apple's Translation The user can now choose between his instance's server, DeepL (with API key) and Apple's Translation framework. A translation is cleared if the translation type is changed. The strings aren't yet written, but the translations settings view's inconsistent background is now fixed. * Transfer the old "always_use_deepl" setting The "always_use_deepl"-setting is now deleted, but its content is transferred to the equivalent value in "preferred_translation_type". * Show the user if the DeepL-API key is still stored The user is now shown a prompt if they've switched away from .useDeepl, but there's still an API key stored. The API key is not deleted if the user doesn't instruct the app to do so, so this change makes it more transparent, since a user might not expect the key to be stored and might not want this to be the case. * Localize Labels The labels for the buttons and options are now localized. "DeepL API Key" is written consistently (with uppercase Key) * Run all the strings through localization The strings "DeepL" and "Apple Translate" are now also saved in localizable.strings and addressed through keys. They were taken directly previously, which was inconsistent. * Fix storage The selected value for preferredTranslationType wasn't stored, the synchronization between UserPreferences and Storage is now in place. * Hide Apple Translate if not yet on iOS 17.4 The Apple Translate option is hidden if the user hasn't updated their phone to at least iOS 17.4. If the Apple Translate option is selected but the user has downgraded to before iOS 17.4, the standard instance option is selected. * Consistently show Apple Translate Apple Translate was previously only shown if the standard translate button was visible, that is now fixed. It's now attached to the StatusRowView, which is always present. * Animate the removal of translations The reset of a translation when the translation type is changed is now animated, which is important for iPad users if they've translated a post in the sidebar. * Add support for the Mac Catalyst build The Mac Catalyst Version doesn't allow the import of the api, so compiler flags now check if the import isn't allowed and then remove all references to Apple Translate. * Swift Format * Revert "Run all the strings through localization" This reverts commit 86c5099662add18eaf6326c815de36344602c82d. # Conflicts: # Packages/Env/Sources/Env/TranslationType.swift * Remove the DeepL fallback The DeepL fallback for the instance translation service is removed, error messages are shown if a translation fails. * Allow for the use of an User API Key as fallback The DeepL fallback is reinstated if the user has put in their own API Key * Make the localization keys clear strings * Make Apple and the instance a fallback Apple Translate is now a fallback for both other translation types, the instance service is a fallback for DeepL.
2024-05-13 13:27:21 +02:00
apiKey = DeepLUserAPIHandler.readKey()
2023-03-14 18:50:19 +01:00
}
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
}
}