From ffb5c59d12c83a991fbd73245830dbb949ffaa6c Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Fri, 16 Feb 2024 10:34:28 +0100 Subject: [PATCH] Slighty refactor relationship-update-notification-handling (IOS-192) Also: consider menu-changes from profile-screen --- .../Scene/Profile/ProfileViewController.swift | 36 +++++++++---------- .../UserTableViewCell+ViewModel.swift | 17 ++++----- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/Mastodon/Scene/Profile/ProfileViewController.swift b/Mastodon/Scene/Profile/ProfileViewController.swift index 50efca93d..525e93827 100644 --- a/Mastodon/Scene/Profile/ProfileViewController.swift +++ b/Mastodon/Scene/Profile/ProfileViewController.swift @@ -810,9 +810,7 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate { self.viewModel.isUpdating = false let userInfo = [ - "account": self.viewModel.account, "relationship": newRelationship, - "me": self.viewModel.me ] NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) @@ -841,9 +839,7 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate { self.viewModel.isUpdating = false let userInfo = [ - "account": self.viewModel.account, "relationship": newRelationship, - "me": self.viewModel.me ] NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) @@ -871,9 +867,7 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate { self.viewModel.isUpdating = false let userInfo = [ - "account": self.viewModel.account, "relationship": newRelationship, - "me": self.viewModel.me ] NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) @@ -917,9 +911,7 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate { self.viewModel.isUpdating = false let userInfo = [ - "account": self.viewModel.account, "relationship": newRelationship, - "me": self.viewModel.me ] NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) @@ -970,9 +962,9 @@ extension ProfileViewController: MastodonMenuDelegate { button: nil, barButtonItem: self.moreMenuBarButtonItem )) { [weak self] newRelationship in - guard let self else { return } - - self.viewModel.relationship = newRelationship + NotificationCenter.default.post(name: .relationshipChanged, object: nil, userInfo: [ + "relationship": newRelationship + ]) } } case .reportUser(_), .shareUser(_): @@ -1061,20 +1053,24 @@ extension ProfileViewController { @objc func relationshipChanged(_ notification: Notification) { - guard let userInfo = notification.userInfo else { + guard let userInfo = notification.userInfo, let relationship = userInfo["relationship"] as? Mastodon.Entity.Relationship, viewModel.account.id == relationship.id else { return } - if let account = userInfo["account"] as? Mastodon.Entity.Account, account == viewModel.account { - viewModel.account = account - } + Task { + viewModel.isUpdating = true + let account = viewModel.account + if let domain = account.domain, + let updatedAccount = try? await context.apiService.fetchUser(username: account.acct, domain: domain, authenticationBox: authContext.mastodonAuthenticationBox) { + viewModel.account = updatedAccount + } - if let me = userInfo["me"] as? Mastodon.Entity.Account, me == viewModel.me { - viewModel.me = me - } - - if let relationship = userInfo["relationship"] as? Mastodon.Entity.Relationship { + if let updatedMe = try? await context.apiService.authenticatedUserInfo(authenticationBox: authContext.mastodonAuthenticationBox).value { + viewModel.me = updatedMe + FileManager.default.store(account: updatedMe, forUserID: authContext.mastodonAuthenticationBox.authentication.userIdentifier()) + } viewModel.relationship = relationship + viewModel.isUpdating = false } } } diff --git a/Mastodon/Scene/Share/View/TableviewCell/UserTableViewCell+ViewModel.swift b/Mastodon/Scene/Share/View/TableviewCell/UserTableViewCell+ViewModel.swift index 86daa2c21..657296009 100644 --- a/Mastodon/Scene/Share/View/TableviewCell/UserTableViewCell+ViewModel.swift +++ b/Mastodon/Scene/Share/View/TableviewCell/UserTableViewCell+ViewModel.swift @@ -62,10 +62,7 @@ extension UserTableViewCellDelegate where Self: NeedsDependency & AuthContextPro // Otherwise the relationship might still be `pending` try await Task.sleep(for: .seconds(1)) - guard let relationship = try await self.context.apiService.relationship(forAccounts: [account], authenticationBox: authContext.mastodonAuthenticationBox).value.first, - let updatedAccount = try await context.apiService.fetchUser(username: account.acct, domain: account.domain ?? "", authenticationBox: authContext.mastodonAuthenticationBox) else { return } - - let updatedMe = try await context.apiService.authenticatedUserInfo(authenticationBox: authContext.mastodonAuthenticationBox).value + let relationship = try await self.context.apiService.relationship(forAccounts: [account], authenticationBox: authContext.mastodonAuthenticationBox).value.first let isMe: Bool if let me { @@ -78,13 +75,13 @@ extension UserTableViewCellDelegate where Self: NeedsDependency & AuthContextPro view.viewModel.relationship = relationship view.updateButtonState(with: relationship, isMe: isMe) - let userInfo = [ - "account": updatedAccount, - "relationship": relationship, - "me": updatedMe - ] + if let relationship { + let userInfo = [ + "relationship": relationship, + ] - NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) + NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) + } } }