mirror of
https://github.com/TuringSoftware/CrystalFetch.git
synced 2025-12-27 07:48:24 +08:00
Update Worker.swift
This commit is contained in:
parent
ad1247282b
commit
0a113bb0bf
1 changed files with 28 additions and 1 deletions
|
|
@ -71,10 +71,37 @@ class Worker: ObservableObject {
|
|||
}
|
||||
}
|
||||
|
||||
func lookupPossibleLocale(fromBuildDetails buildDetails: UUPDetails, withPreferredLanguage language: String? = nil) -> String {
|
||||
// There is a naming conversation required for UUP API and macOS API
|
||||
// see https://github.com/TuringSoftware/CrystalFetch/issues/2 for details
|
||||
let localeMapper = [
|
||||
"zh-hans-cn": "zh-cn"
|
||||
]
|
||||
|
||||
var decisionLocale = language ?? defaultLocale
|
||||
if !buildDetails.langList.contains(decisionLocale),
|
||||
let convertor = localeMapper[decisionLocale]
|
||||
{
|
||||
// unable to find this locale, check naming conversation if possible
|
||||
decisionLocale = convertor
|
||||
}
|
||||
if !buildDetails.langList.contains(decisionLocale) {
|
||||
// unable to find this locale, use the English if possible
|
||||
decisionLocale = "en-us"
|
||||
}
|
||||
if !buildDetails.langList.contains(decisionLocale) {
|
||||
// unable to find the en-us, use the first value supported if possible
|
||||
decisionLocale = buildDetails.langList.first ?? ""
|
||||
}
|
||||
// if still not possible, throw the error UNSUPPORTED_LANG from server side
|
||||
return decisionLocale
|
||||
}
|
||||
|
||||
func refreshDetails(uuid: String, language: String? = nil, _ onCompletion: @escaping (BuildDetails, BuildEditions) -> Void) {
|
||||
withBusyIndication { [self] in
|
||||
let detailsResponse = try await api.fetchDetails(for: uuid)
|
||||
let editionsResponse = try await api.fetchEditions(for: uuid, language: language ?? defaultLocale)
|
||||
let language = lookupPossibleLocale(fromBuildDetails: detailsResponse, withPreferredLanguage: language)
|
||||
let editionsResponse = try await api.fetchEditions(for: uuid, language: language)
|
||||
onCompletion(BuildDetails(from: detailsResponse), BuildEditions(from: editionsResponse))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue