From a2f2fb83cdc433a411741c34964227941355b68f Mon Sep 17 00:00:00 2001 From: Marcus Kida Date: Mon, 14 Nov 2022 13:12:16 +0100 Subject: [PATCH] Fix authenticated user account not reloaded --- .../Scene/Profile/ProfileViewController.swift | 3 +++ .../Root/MainTab/MainTabBarController.swift | 27 ++++++++++++++++++- Mastodon/Supporting Files/SceneDelegate.swift | 3 +++ .../CoreDataStack/MastodonUser+Property.swift | 4 +++ .../Service/API/APIService+Account.swift | 9 +++++++ .../Service/InstanceService.swift | 3 ++- 6 files changed, 47 insertions(+), 2 deletions(-) diff --git a/Mastodon/Scene/Profile/ProfileViewController.swift b/Mastodon/Scene/Profile/ProfileViewController.swift index 3ce1fd33a..212e92d06 100644 --- a/Mastodon/Scene/Profile/ProfileViewController.swift +++ b/Mastodon/Scene/Profile/ProfileViewController.swift @@ -552,6 +552,9 @@ extension ProfileViewController { userTimelineViewController.viewModel.stateMachine.enter(UserTimelineViewModel.State.Reloading.self) } + // trigger authenticated user account update + viewModel.context.instanceService.updateActiveUserAccountPublisher.send() + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { sender.endRefreshing() } diff --git a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift index c49dcc1a1..64066746a 100644 --- a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift +++ b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift @@ -312,7 +312,12 @@ extension MainTabBarController { guard let profileTabItem = _profileTabItem else { return } let currentUserDisplayName = user.displayNameWithFallback ?? "no user" profileTabItem.accessibilityHint = L10n.Scene.AccountList.tabBarHint(currentUserDisplayName) - + + context.instanceService.updateActiveUserAccountPublisher + .sink { [weak self] in + self?.updateUserAccount() + } + .store(in: &disposeBag) } else { self.avatarURLObserver = nil } @@ -487,6 +492,26 @@ extension MainTabBarController { avatarButton.setNeedsLayout() } + private func updateUserAccount() { + guard let authContext = authContext else { return } + + Task { @MainActor in + let profileResponse = try await context.apiService.authenticatedUserInfo( + authenticationBox: authContext.mastodonAuthenticationBox + ) + + if let user = authContext.mastodonAuthenticationBox.authenticationRecord.object( + in: context.managedObjectContext + )?.user { + user.update( + property: .init( + entity: profileResponse.value, + domain: authContext.mastodonAuthenticationBox.domain + ) + ) + } + } + } } extension MainTabBarController { diff --git a/Mastodon/Supporting Files/SceneDelegate.swift b/Mastodon/Supporting Files/SceneDelegate.swift index 1e97fb179..fad0f0cd5 100644 --- a/Mastodon/Supporting Files/SceneDelegate.swift +++ b/Mastodon/Supporting Files/SceneDelegate.swift @@ -109,6 +109,9 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { // trigger status filter update AppContext.shared.statusFilterService.filterUpdatePublisher.send() + + // trigger authenticated user account update + AppContext.shared.instanceService.updateActiveUserAccountPublisher.send() if let shortcutItem = savedShortCutItem { Task { diff --git a/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/MastodonUser+Property.swift b/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/MastodonUser+Property.swift index cebe7f8af..8d2f77ba7 100644 --- a/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/MastodonUser+Property.swift +++ b/MastodonSDK/Sources/MastodonCore/Extension/CoreDataStack/MastodonUser+Property.swift @@ -10,6 +10,10 @@ import CoreDataStack import MastodonSDK extension MastodonUser.Property { + public init(entity: Mastodon.Entity.Account, domain: String) { + self.init(entity: entity, domain: domain, networkDate: Date()) + } + init(entity: Mastodon.Entity.Account, domain: String, networkDate: Date) { self.init( identifier: entity.id + "@" + domain, diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Account.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Account.swift index 68649d24c..1b6a57a83 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Account.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Account.swift @@ -13,6 +13,15 @@ import MastodonCommon import MastodonSDK extension APIService { + public func authenticatedUserInfo( + authenticationBox: MastodonAuthenticationBox + ) async throws -> Mastodon.Response.Content { + try await accountInfo( + domain: authenticationBox.domain, + userID: authenticationBox.userID, + authorization: authenticationBox.userAuthorization + ) + } public func accountInfo( domain: String, diff --git a/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift b/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift index c63e965bd..7f6669250 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/InstanceService.swift @@ -24,7 +24,8 @@ public final class InstanceService { weak var authenticationService: AuthenticationService? // output - + public let updateActiveUserAccountPublisher = PassthroughSubject() + init( apiService: APIService, authenticationService: AuthenticationService