Refactor way to determine if translation is enabled

This commit is contained in:
Nathan Mattes 2023-10-18 14:34:20 +02:00
parent e1d1ef83e6
commit fdfed0a09b
4 changed files with 52 additions and 72 deletions

View File

@ -49,7 +49,7 @@ extension Instance {
version?.majorServerVersion(greaterThanOrEquals: 4) ?? false // following Tags is support beginning with Mastodon v4.0.0
}
var isTranslationEnabled: Bool {
public var isTranslationEnabled: Bool {
if let configuration = configurationV2 {
return configuration.translation?.enabled == true
}

View File

@ -84,12 +84,14 @@ public struct MastodonAuthentication: Codable, Hashable {
}
public func instance(in context: NSManagedObjectContext) -> Instance? {
guard
let instanceObjectIdURI = instanceObjectIdURI,
guard let instanceObjectIdURI,
let objectID = context.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: instanceObjectIdURI)
else { return nil }
else {
return nil
}
return try? context.existingObject(with: objectID) as? Instance
let instance = try? context.existingObject(with: objectID) as? Instance
return instance
}
public func user(in context: NSManagedObjectContext) -> MastodonUser? {

View File

@ -221,27 +221,16 @@ extension NotificationView.ViewModel {
)
)
.sink { [weak self] authorName, isMuting, isBlocking, isMyselfIsTranslatedIsFollowed in
guard let name = authorName?.string else {
guard let name = authorName?.string, let self, let context = self.context, let authContext = self.authContext else {
notificationView.menuButton.menu = nil
return
}
let (isMyself, isTranslated, isFollowed) = isMyselfIsTranslatedIsFollowed
lazy var instanceConfigurationV2: Mastodon.Entity.V2.Instance.Configuration? = {
guard
let self = self,
let context = self.context,
let authContext = self.authContext
else { return nil }
var configuration: Mastodon.Entity.V2.Instance.Configuration? = nil
context.managedObjectContext.performAndWait {
let authentication = authContext.mastodonAuthenticationBox.authentication
configuration = authentication.instance(in: context.managedObjectContext)?.configurationV2
}
return configuration
}()
let instance = authentication.instance(in: context.managedObjectContext)
let isTranslationEnabled = instance?.isTranslationEnabled ?? false
let menuContext = NotificationView.AuthorMenuContext(
name: name,
@ -250,9 +239,9 @@ extension NotificationView.ViewModel {
isMyself: isMyself,
isBookmarking: false, // no bookmark action display for notification item
isFollowed: isFollowed,
isTranslationEnabled: instanceConfigurationV2?.translation?.enabled == true,
isTranslationEnabled: isTranslationEnabled,
isTranslated: isTranslated,
statusLanguage: ""
statusLanguage: nil
)
let (menu, actions) = notificationView.setupAuthorMenu(menuContext: menuContext)
notificationView.menuButton.menu = menu

View File

@ -669,26 +669,14 @@ extension StatusView.ViewModel {
let (isMuting, isBlocking, isBookmark, isFollowed) = tupleTwo
let (translatedFromLanguage, language) = tupleThree
guard let name = authorName?.string else {
guard let name = authorName?.string, let context = self.context, let authContext = self.authContext else {
statusView.authorView.menuButton.menu = nil
return
}
lazy var instanceConfigurationV2: Mastodon.Entity.V2.Instance.Configuration? = {
guard
let context = self.context,
let authContext = self.authContext
else {
return nil
}
var configuration: Mastodon.Entity.V2.Instance.Configuration? = nil
context.managedObjectContext.performAndWait {
let authentication = authContext.mastodonAuthenticationBox.authentication
configuration = authentication.instance(in: context.managedObjectContext)?.configurationV2
}
return configuration
}()
let instance = authentication.instance(in: context.managedObjectContext)
let isTranslationEnabled = instance?.isTranslationEnabled ?? false
let menuContext = StatusAuthorView.AuthorMenuContext(
name: name,
@ -697,10 +685,11 @@ extension StatusView.ViewModel {
isMyself: isMyself,
isBookmarking: isBookmark,
isFollowed: isFollowed,
isTranslationEnabled: instanceConfigurationV2?.translation?.enabled == true,
isTranslationEnabled: isTranslationEnabled,
isTranslated: translatedFromLanguage != nil,
statusLanguage: language
)
let (menu, actions) = authorView.setupAuthorMenu(menuContext: menuContext)
authorView.menuButton.menu = menu
authorView.authorActions = actions