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.
This commit is contained in:
Paul Schuetz 2024-05-13 12:47:00 +02:00
parent 7de6a0f007
commit 218553d1cf
No known key found for this signature in database
GPG key ID: 4FDE1FB89050039E

View file

@ -315,29 +315,35 @@ import SwiftUI
return
}
withAnimation {
isLoadingTranslation = true
}
if preferredTranslationType != .useDeepl {
let translation: Translation? = try? await client.post(endpoint: Statuses.translate(id: finalStatus.id,
lang: userLang))
await translateWithInstance(userLang: userLang)
if translation == nil {
await translateWithDeepL(userLang: userLang)
} else {
withAnimation {
self.translation = translation
isLoadingTranslation = false
}
}
if self.translation == nil {
instanceTranslationError = true
}
} else {
await translateWithDeepL(userLang: userLang)
if translation == nil {
await translateWithInstance(userLang: userLang)
}
}
var hasShown = false
#if canImport(_Translation_SwiftUI)
if translation == nil,
#available(iOS 17.4, *) {
showAppleTranslation = true
hasShown = true
}
#endif
if !hasShown,
translation == nil {
if preferredTranslationType == .useDeepl {
deeplTranslationError = true
} else {
instanceTranslationError = true
}
}
}
@ -355,6 +361,20 @@ import SwiftUI
isLoadingTranslation = false
}
}
func translateWithInstance(userLang: String) async {
withAnimation {
isLoadingTranslation = true
}
let translation: Translation? = try? await client.post(endpoint: Statuses.translate(id: finalStatus.id,
lang: userLang))
withAnimation {
self.translation = translation
isLoadingTranslation = false
}
}
private func getDeepLClient() -> DeepLClient {
let userAPIfree = UserPreferences.shared.userDeeplAPIFree