From 3474967700ff1a41659047b516c753f2cf7ce4e4 Mon Sep 17 00:00:00 2001 From: Viktor <54028958+viktorkalyniuk@users.noreply.github.com> Date: Wed, 23 Aug 2023 13:06:15 +0300 Subject: [PATCH] release v1.2 (#5) - Support for api's that use keys - Removed idle instances. --------- Co-authored-by: Viktor Kalyniuk <@> Co-authored-by: Simon Rieger --- .../TranslationParsing.swift | 5 +++-- .../ObservableObjects/InstanceURL.swift | 5 +++++ LiTranslate/Constants/Keys.swift | 1 + LiTranslate/Constants/Links.swift | 4 ++-- .../InputBottomButtons.swift | 4 +++- .../Languages/ChooseLanguagesView.swift | 8 +++++-- .../Component/Settings/SelfHostingView.swift | 21 +++++++++++++++---- 7 files changed, 37 insertions(+), 11 deletions(-) diff --git a/LiTranslate/Application/LibreTranslateAPI/TranslationParsing.swift b/LiTranslate/Application/LibreTranslateAPI/TranslationParsing.swift index 15165f5..4e379ae 100644 --- a/LiTranslate/Application/LibreTranslateAPI/TranslationParsing.swift +++ b/LiTranslate/Application/LibreTranslateAPI/TranslationParsing.swift @@ -8,14 +8,15 @@ import Foundation struct TranslationParsing { - static func parse(url: String, text: String, inputLanguage: Languages, outputLanguage: Languages, handler: @escaping(TranslationDecode) -> Void) { + static func parse(url: String, text: String, inputLanguage: Languages, outputLanguage: Languages, key: String, handler: @escaping(TranslationDecode) -> Void) { if let url = URL(string: url) { let session = URLSession.shared let parameters: [String: String] = [ "q": "\(text)", "source": "\(inputLanguage.rawValue)", "target": "\(outputLanguage.rawValue)", - "format": "text" + "format": "text", + "api_key": "\(key)" ] var request = URLRequest(url: url) diff --git a/LiTranslate/Application/ObservableObjects/InstanceURL.swift b/LiTranslate/Application/ObservableObjects/InstanceURL.swift index b214cad..08a0c89 100644 --- a/LiTranslate/Application/ObservableObjects/InstanceURL.swift +++ b/LiTranslate/Application/ObservableObjects/InstanceURL.swift @@ -11,6 +11,7 @@ import SwiftUI class InstanceURL: ObservableObject { @AppStorage(Keys.libreTranslateDefaultURL) var defaultURL: Links.LibreTranslateMirrors = Links.LibreTranslateMirrors.de @AppStorage(Keys.libreTranslateSelfHostURL) var selfHostURL: String = "" + @AppStorage(Keys.libreTranslateSelfHostKey) var selfHostKey: String = "" func getURL() -> String { if selfHostURL.isEmpty { @@ -19,4 +20,8 @@ class InstanceURL: ObservableObject { return selfHostURL } + + func getKey() -> String { + return selfHostKey + } } diff --git a/LiTranslate/Constants/Keys.swift b/LiTranslate/Constants/Keys.swift index 02a654b..11abd95 100644 --- a/LiTranslate/Constants/Keys.swift +++ b/LiTranslate/Constants/Keys.swift @@ -10,6 +10,7 @@ import Foundation struct Keys { static let bookmarks: String = "bookmarks" static let libreTranslateSelfHostURL: String = "libreTranslateSelfHostURL" + static let libreTranslateSelfHostKey: String = "libreTranslateSelfHostKey" static let libreTranslateDefaultURL: String = "libreTranslateURL" static let languageInputSelection: String = "languageInputSelection" static let languageOutputSelection: String = "languageOutputSelection" diff --git a/LiTranslate/Constants/Links.swift b/LiTranslate/Constants/Links.swift index a385a6c..313bef4 100644 --- a/LiTranslate/Constants/Links.swift +++ b/LiTranslate/Constants/Links.swift @@ -14,9 +14,9 @@ struct Links { case de = "https://libretranslate.de/translate" case argosopentech = "https://translate.argosopentech.com/translate" // case skitzen = "https://translate.api.skitzen.com/translate" - case fortytwo = "https://translate.fortytwo-it.com/translate" +// case fortytwo = "https://translate.fortytwo-it.com/translate" case terraprint = "https://translate.terraprint.co/translate" - case vern = "https://lt.vern.cc/translate" +// case vern = "https://lt.vern.cc/translate" } struct Info { diff --git a/LiTranslate/Presentation/Component/Languages/BottomButtonsPanels/InputBottomButtons.swift b/LiTranslate/Presentation/Component/Languages/BottomButtonsPanels/InputBottomButtons.swift index a5c3930..ce5c365 100644 --- a/LiTranslate/Presentation/Component/Languages/BottomButtonsPanels/InputBottomButtons.swift +++ b/LiTranslate/Presentation/Component/Languages/BottomButtonsPanels/InputBottomButtons.swift @@ -42,7 +42,9 @@ struct InputBottomButtons: View { .parse(url: instance.getURL(), text: textData.input, inputLanguage: selection.input, - outputLanguage: selection.output) { data in + outputLanguage: selection.output, + key: instance.getKey() + ) { data in DispatchQueue.main.async { textData.output = data.translatedText } diff --git a/LiTranslate/Presentation/Component/Languages/ChooseLanguagesView.swift b/LiTranslate/Presentation/Component/Languages/ChooseLanguagesView.swift index b064832..aa49304 100644 --- a/LiTranslate/Presentation/Component/Languages/ChooseLanguagesView.swift +++ b/LiTranslate/Presentation/Component/Languages/ChooseLanguagesView.swift @@ -27,7 +27,9 @@ struct ChooseLanguagesView: View { .parse(url: instance.getURL(), text: textData.input, inputLanguage: selection.input, - outputLanguage: selection.output) { data in + outputLanguage: selection.output, + key: instance.getKey() + ) { data in DispatchQueue.main.async { textData.output = data.translatedText } @@ -48,7 +50,9 @@ struct ChooseLanguagesView: View { .parse(url: instance.getURL(), text: textData.input, inputLanguage: selection.input, - outputLanguage: selection.output) { data in + outputLanguage: selection.output, + key: instance.getKey() + ) { data in DispatchQueue.main.async { textData.output = data.translatedText } diff --git a/LiTranslate/Presentation/Component/Settings/SelfHostingView.swift b/LiTranslate/Presentation/Component/Settings/SelfHostingView.swift index 150b318..fd3c623 100644 --- a/LiTranslate/Presentation/Component/Settings/SelfHostingView.swift +++ b/LiTranslate/Presentation/Component/Settings/SelfHostingView.swift @@ -12,10 +12,13 @@ struct SelfHostingView: View { @Environment(\.presentationMode) var mode: Binding - @State private var input: String = "" + @State private var inputURL: String = "" + @State private var inputKey: String = "" private let selfHostingTitle = "Self Hosting" private let textFieldTitle = "url" + private let selfApiTitle = "API Key (if needed)" + private let textApiTitle = "key" private let useInstanceTitle = "Use default instance" private let infoTitle = "Info" private let instructionTitle = "Instruction" @@ -24,9 +27,18 @@ struct SelfHostingView: View { var body: some View { List { Section(selfHostingTitle) { - TextField(textFieldTitle, text: $input) + TextField(textFieldTitle, text: $inputURL) .onAppear { - input = instance.selfHostURL + inputURL = instance.selfHostURL + } + .listRowBackground(Colors.Background.primaryView) + .foregroundColor(Colors.Foreground.label) + } + .textCase(nil) + Section(selfApiTitle) { + TextField(textApiTitle, text: $inputKey) + .onAppear { + inputKey = instance.selfHostKey } .listRowBackground(Colors.Background.primaryView) .foregroundColor(Colors.Foreground.label) @@ -59,7 +71,8 @@ struct SelfHostingView: View { .background(Colors.Background.mainView) .toolbar { Button { - instance.selfHostURL = input + instance.selfHostURL = inputURL + instance.selfHostKey = inputKey self.mode.wrappedValue.dismiss() } label: { Text(doneTitle)