2022-11-30 16:38:31 +01:00
|
|
|
//
|
|
|
|
// DataSourceFacade+Translate.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by Marcus Kida on 29.11.22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonCore
|
|
|
|
|
|
|
|
extension DataSourceFacade {
|
2022-12-09 16:12:13 +01:00
|
|
|
enum TranslationFailure: Error {
|
|
|
|
case emptyOrInvalidResponse
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:38:31 +01:00
|
|
|
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? {
|
2022-12-09 16:12:13 +01:00
|
|
|
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 {
|
2022-12-09 16:12:13 +01:00
|
|
|
try await translateAndApply(to: reblog)
|
2022-12-02 14:42:50 +01:00
|
|
|
} else {
|
2022-12-09 16:12:13 +01:00
|
|
|
try await translateAndApply(to: status)
|
2022-12-02 14:42:50 +01:00
|
|
|
}
|
2022-11-30 16:38:31 +01:00
|
|
|
}
|
|
|
|
}
|