Skip to content

Commit

Permalink
Make Apple and the instance a fallback
Browse files Browse the repository at this point in the history
Apple Translate is now a fallback for both other translation types,
the instance service is a fallback for DeepL.
  • Loading branch information
Havhingstor committed May 13, 2024
1 parent 7de6a0f commit 218553d
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions Packages/StatusKit/Sources/StatusKit/Row/StatusRowViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand All @@ -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
Expand Down

0 comments on commit 218553d

Please sign in to comment.