chore: Refactor code and apply PR improvements
This commit is contained in:
parent
8f32e1a80e
commit
85f4c454a3
|
@ -10,13 +10,15 @@ import CoreData
|
||||||
import CoreDataStack
|
import CoreDataStack
|
||||||
import MastodonCore
|
import MastodonCore
|
||||||
|
|
||||||
|
typealias Provider = UIViewController & NeedsDependency & AuthContextProvider
|
||||||
|
|
||||||
extension DataSourceFacade {
|
extension DataSourceFacade {
|
||||||
enum TranslationFailure: Error {
|
enum TranslationFailure: Error {
|
||||||
case emptyOrInvalidResponse
|
case emptyOrInvalidResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func translateStatus(
|
public static func translateStatus(
|
||||||
provider: UIViewController & NeedsDependency & AuthContextProvider,
|
provider: Provider,
|
||||||
status: ManagedObjectRecord<Status>
|
status: ManagedObjectRecord<Status>
|
||||||
) async throws {
|
) async throws {
|
||||||
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
|
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
|
||||||
|
@ -28,38 +30,40 @@ extension DataSourceFacade {
|
||||||
return
|
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 {
|
if let reblog = status.reblog {
|
||||||
try await translateAndApply(to: reblog)
|
try await translateAndApply(provider: provider, status: reblog)
|
||||||
} else {
|
} 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,13 @@ extension Instance {
|
||||||
|
|
||||||
extension Instance {
|
extension Instance {
|
||||||
public var configurationV2: Mastodon.Entity.V2.Instance.Configuration? {
|
public var configurationV2: Mastodon.Entity.V2.Instance.Configuration? {
|
||||||
guard let configurationRaw = configurationV2Raw else { return nil }
|
guard
|
||||||
guard let configuration = try? JSONDecoder().decode(Mastodon.Entity.V2.Instance.Configuration.self, from: configurationRaw) else {
|
let configurationRaw = configurationV2Raw,
|
||||||
|
let configuration = try? JSONDecoder().decode(
|
||||||
|
Mastodon.Entity.V2.Instance.Configuration.self,
|
||||||
|
from: configurationRaw
|
||||||
|
)
|
||||||
|
else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue