diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Translate.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Translate.swift index ba2b1b86d..fc95cb018 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Translate.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Translate.swift @@ -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 ) 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 } } } diff --git a/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/Instance.swift b/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/Instance.swift index 619abc91e..f54f4514d 100644 --- a/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/Instance.swift +++ b/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/Instance.swift @@ -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 }