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 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,7 +30,16 @@ extension DataSourceFacade {
return return
} }
func translate(status: Status) async throws -> String? { if let reblog = status.reblog {
try await translateAndApply(provider: provider, status: reblog)
} else {
try await translateAndApply(provider: provider, status: status)
}
}
}
private extension DataSourceFacade {
static func translateStatus(provider: Provider, status: Status) async throws -> String? {
do { do {
let value = try await provider.context let value = try await provider.context
.apiService .apiService
@ -47,19 +58,12 @@ extension DataSourceFacade {
} }
} }
func translateAndApply(to status: Status) async throws { static func translateAndApply(provider: Provider, status: Status) async throws {
do { do {
status.translatedContent = try await translate(status: status) status.translatedContent = try await translateStatus(provider: provider, status: status)
} catch { } catch {
status.translatedContent = nil status.translatedContent = nil
throw TranslationFailure.emptyOrInvalidResponse throw TranslationFailure.emptyOrInvalidResponse
} }
} }
if let reblog = status.reblog {
try await translateAndApply(to: reblog)
} else {
try await translateAndApply(to: status)
}
}
} }

View File

@ -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
} }