From 1bb8e64574e5ef9bc1ac2708a70a59b4c2ff1b6f Mon Sep 17 00:00:00 2001 From: Marcus Kida Date: Fri, 2 Jun 2023 10:49:00 +0200 Subject: [PATCH] Fix TabBar avatar loading --- .../Root/MainTab/MainTabBarController.swift | 44 +++++++++++-------- .../Service/API/APIService+HomeTimeline.swift | 7 ++- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift index 3867e8cbe..4f637c68d 100644 --- a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift +++ b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift @@ -292,28 +292,34 @@ extension MainTabBarController { } .store(in: &disposeBag) - if let user = authContext?.mastodonAuthenticationBox.authentication.user(in: context.managedObjectContext) { - self.avatarURLObserver = user.publisher(for: \.avatar) - .sink { [weak self, weak user] _ in - guard let self = self else { return } - guard let user = user else { return } - guard user.managedObjectContext != nil else { return } - self.avatarURL = user.avatarImageURL() - } + NotificationCenter.default.publisher(for: .userFetched) + .receive(on: DispatchQueue.main) + .sink { [weak self] _ in + guard let self = self else { return } + if let user = self.authContext?.mastodonAuthenticationBox.authentication.user(in: self.context.managedObjectContext) { + self.avatarURLObserver = user.publisher(for: \.avatar) + .sink { [weak self, weak user] _ in + guard let self = self else { return } + guard let user = user else { return } + guard user.managedObjectContext != nil else { return } + self.avatarURL = user.avatarImageURL() + } - // a11y - let _profileTabItem = self.tabBar.items?.first { item in item.tag == Tab.me.tag } - guard let profileTabItem = _profileTabItem else { return } - profileTabItem.accessibilityHint = L10n.Scene.AccountList.tabBarHint(user.displayNameWithFallback) + // a11y + let _profileTabItem = self.tabBar.items?.first { item in item.tag == Tab.me.tag } + guard let profileTabItem = _profileTabItem else { return } + profileTabItem.accessibilityHint = L10n.Scene.AccountList.tabBarHint(user.displayNameWithFallback) - context.authenticationService.updateActiveUserAccountPublisher - .sink { [weak self] in - self?.updateUserAccount() + self.context.authenticationService.updateActiveUserAccountPublisher + .sink { [weak self] in + self?.updateUserAccount() + } + .store(in: &self.disposeBag) + } else { + self.avatarURLObserver = nil } - .store(in: &disposeBag) - } else { - self.avatarURLObserver = nil - } + } + .store(in: &disposeBag) let tabBarLongPressGestureRecognizer = UILongPressGestureRecognizer() tabBarLongPressGestureRecognizer.addTarget(self, action: #selector(MainTabBarController.tabBarLongPressGestureRecognizerHandler(_:))) diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+HomeTimeline.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+HomeTimeline.swift index 91d053d5b..a3b4b4b08 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+HomeTimeline.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+HomeTimeline.swift @@ -12,6 +12,10 @@ import CoreDataStack import CommonOSLog import MastodonSDK +public extension Foundation.Notification.Name { + static let userFetched = Notification.Name(rawValue: "org.joinmastodon.app.user-fetched") +} + extension APIService { public func homeTimeline( @@ -44,7 +48,8 @@ extension APIService { // Problem is, that we don't persist the user on disk anymore. So we have to fetch // it when we need it to display on the home timeline. _ = try await authenticatedUserInfo(authenticationBox: authenticationBox).value - + NotificationCenter.default.post(name: .userFetched, object: nil) + try await managedObjectContext.performChanges { guard let me = authenticationBox.authentication.user(in: managedObjectContext) else { assertionFailure()