chore: Refactor code and apply PR improvements

This commit is contained in:
Marcus Kida 2022-12-13 06:54:58 +01:00
parent 8f32e1a80e
commit 85f4c454a3
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
2 changed files with 42 additions and 33 deletions

View File

@ -10,13 +10,15 @@ import CoreData
import CoreDataStack
import MastodonCore
typealias Provider = UIViewController & NeedsDependency & AuthContextProvider
extension DataSourceFacade {
enum TranslationFailure: Error {
case emptyOrInvalidResponse
}
public static func translateStatus(
provider: UIViewController & NeedsDependency & AuthContextProvider,
provider: Provider,
status: ManagedObjectRecord<Status>
) async throws {
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
@ -28,38 +30,40 @@ extension DataSourceFacade {
return
}
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
}
}
if let reblog = status.reblog {
try await translateAndApply(to: reblog)
try await translateAndApply(provider: provider, status: reblog)
} else {
try await translateAndApply(to: status)
try await translateAndApply(provider: provider, status: status)
}
}
}
private extension DataSourceFacade {
static func translateStatus(provider: Provider, 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
}
}
static func translateAndApply(provider: Provider, status: Status) async throws {
do {
status.translatedContent = try await translateStatus(provider: provider, status: status)
} catch {
status.translatedContent = nil
throw TranslationFailure.emptyOrInvalidResponse
}
}
}

View File

@ -26,8 +26,13 @@ extension Instance {
extension Instance {
public var configurationV2: Mastodon.Entity.V2.Instance.Configuration? {
guard let configurationRaw = configurationV2Raw else { return nil }
guard let configuration = try? JSONDecoder().decode(Mastodon.Entity.V2.Instance.Configuration.self, from: configurationRaw) else {
guard
let configurationRaw = configurationV2Raw,
let configuration = try? JSONDecoder().decode(
Mastodon.Entity.V2.Instance.Configuration.self,
from: configurationRaw
)
else {
return nil
}