diff --git a/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/Instance.swift b/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/Instance.swift index eebb16be4..006c9881f 100644 --- a/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/Instance.swift +++ b/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/Instance.swift @@ -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 } diff --git a/MastodonSDK/Sources/MastodonCore/MastodonAuthentication.swift b/MastodonSDK/Sources/MastodonCore/MastodonAuthentication.swift index a9880472a..0b1a9db49 100644 --- a/MastodonSDK/Sources/MastodonCore/MastodonAuthentication.swift +++ b/MastodonSDK/Sources/MastodonCore/MastodonAuthentication.swift @@ -84,12 +84,14 @@ public struct MastodonAuthentication: Codable, Hashable { } public func instance(in context: NSManagedObjectContext) -> Instance? { - guard - let instanceObjectIdURI = instanceObjectIdURI, - let objectID = context.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: instanceObjectIdURI) - else { return nil } - - return try? context.existingObject(with: objectID) as? Instance + guard let instanceObjectIdURI, + let objectID = context.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: instanceObjectIdURI) + else { + return nil + } + + let instance = try? context.existingObject(with: objectID) as? Instance + return instance } public func user(in context: NSManagedObjectContext) -> MastodonUser? { diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift b/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift index 5879eed4f..b51011e54 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/NotificationView+ViewModel.swift @@ -221,38 +221,27 @@ 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 menuContext = NotificationView.AuthorMenuContext( + + let (isMyself, isTranslated, isFollowed) = isMyselfIsTranslatedIsFollowed + + let authentication = authContext.mastodonAuthenticationBox.authentication + let instance = authentication.instance(in: context.managedObjectContext) + let isTranslationEnabled = instance?.isTranslationEnabled ?? false + + let menuContext = NotificationView.AuthorMenuContext( name: name, isMuting: isMuting, isBlocking: isBlocking, 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 diff --git a/MastodonSDK/Sources/MastodonUI/View/Content/StatusView+ViewModel.swift b/MastodonSDK/Sources/MastodonUI/View/Content/StatusView+ViewModel.swift index 0e0449a1e..86948b5b5 100644 --- a/MastodonSDK/Sources/MastodonUI/View/Content/StatusView+ViewModel.swift +++ b/MastodonSDK/Sources/MastodonUI/View/Content/StatusView+ViewModel.swift @@ -658,57 +658,46 @@ extension StatusView.ViewModel { $translation, $language ) - + Publishers.CombineLatest3( publisherOne.eraseToAnyPublisher(), publishersTwo.eraseToAnyPublisher(), publishersThree.eraseToAnyPublisher() ).eraseToAnyPublisher() - .sink { tupleOne, tupleTwo, tupleThree in - let (authorName, isMyself) = tupleOne - let (isMuting, isBlocking, isBookmark, isFollowed) = tupleTwo - let (translatedFromLanguage, language) = tupleThree - - guard let name = authorName?.string 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 + .sink { tupleOne, tupleTwo, tupleThree in + let (authorName, isMyself) = tupleOne + let (isMuting, isBlocking, isBookmark, isFollowed) = tupleTwo + let (translatedFromLanguage, language) = tupleThree + + guard let name = authorName?.string, let context = self.context, let authContext = self.authContext else { + statusView.authorView.menuButton.menu = nil + return } + + let authentication = authContext.mastodonAuthenticationBox.authentication + let instance = authentication.instance(in: context.managedObjectContext) + let isTranslationEnabled = instance?.isTranslationEnabled ?? false + + let menuContext = StatusAuthorView.AuthorMenuContext( + name: name, + isMuting: isMuting, + isBlocking: isBlocking, + isMyself: isMyself, + isBookmarking: isBookmark, + isFollowed: isFollowed, + isTranslationEnabled: isTranslationEnabled, + isTranslated: translatedFromLanguage != nil, + statusLanguage: language + ) - 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 menuContext = StatusAuthorView.AuthorMenuContext( - name: name, - isMuting: isMuting, - isBlocking: isBlocking, - isMyself: isMyself, - isBookmarking: isBookmark, - isFollowed: isFollowed, - isTranslationEnabled: instanceConfigurationV2?.translation?.enabled == true, - isTranslated: translatedFromLanguage != nil, - statusLanguage: language - ) - let (menu, actions) = authorView.setupAuthorMenu(menuContext: menuContext) - authorView.menuButton.menu = menu - authorView.authorActions = actions - authorView.menuButton.showsMenuAsPrimaryAction = true - } - .store(in: &disposeBag) + let (menu, actions) = authorView.setupAuthorMenu(menuContext: menuContext) + authorView.menuButton.menu = menu + authorView.authorActions = actions + authorView.menuButton.showsMenuAsPrimaryAction = true + } + .store(in: &disposeBag) } - + private func bindFilter(statusView: StatusView) { $isFiltered .sink { isFiltered in