mastodon-ios/Mastodon/Protocol/Provider/DataSourceFacade+Translate....

66 lines
1.9 KiB
Swift
Raw Normal View History

//
// DataSourceFacade+Translate.swift
// Mastodon
//
// Created by Marcus Kida on 29.11.22.
//
import UIKit
import CoreData
import CoreDataStack
import MastodonCore
extension DataSourceFacade {
enum TranslationFailure: Error {
case emptyOrInvalidResponse
}
public static func translateStatus(
provider: UIViewController & NeedsDependency & AuthContextProvider,
status: ManagedObjectRecord<Status>
) async throws {
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
await selectionFeedbackGenerator.selectionChanged()
2022-12-02 12:12:46 +01:00
guard
let status = status.object(in: provider.context.managedObjectContext)
else {
return
}
2022-12-02 14:42:50 +01:00
func translate(status: Status) async throws -> String? {
do {
let value = try await provider.context
.apiService
.translateStatus(
statusID: status.id,
authenticationBox: provider.authContext.mastodonAuthenticationBox
).value
guard let content = value.content else {
throw TranslationFailure.emptyOrInvalidResponse
}
return content
} catch {
throw TranslationFailure.emptyOrInvalidResponse
}
}
func translateAndApply(to status: Status) async throws {
do {
status.translatedContent = try await translate(status: status)
} catch {
status.translatedContent = nil
throw TranslationFailure.emptyOrInvalidResponse
}
}
2022-12-02 12:12:46 +01:00
2022-12-02 14:42:50 +01:00
if let reblog = status.reblog {
try await translateAndApply(to: reblog)
2022-12-02 14:42:50 +01:00
} else {
try await translateAndApply(to: status)
2022-12-02 14:42:50 +01:00
}
}
}