From 91e3b63f1f33cd408df779434ce3596699bace2a Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Fri, 16 Feb 2024 12:16:55 +0100 Subject: [PATCH] Rework relationship-notification (IOS-192) They get sent from the DataSourceFacade now --- .../Provider/DataSourceFacade+Block.swift | 8 +- .../Provider/DataSourceFacade+Follow.swift | 19 +++- .../Provider/DataSourceFacade+Mute.swift | 6 ++ .../Provider/DataSourceFacade+Status.swift | 8 +- .../Provider/DataSourceFacade+UserView.swift | 33 +------ ...er+NotificationTableViewCellDelegate.swift | 1 - .../Scene/Profile/ProfileViewController.swift | 98 ++++++------------- .../UserTableViewCell+ViewModel.swift | 9 -- 8 files changed, 62 insertions(+), 120 deletions(-) diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Block.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Block.swift index 3473d6e80..02bf7893b 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Block.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Block.swift @@ -26,11 +26,11 @@ extension DataSourceFacade { authenticationBox: authBox ) - try await dependency.context.apiService.getBlocked( - authenticationBox: authBox - ) + let userInfo = [ + "relationship": response.value, + ] - dependency.context.authenticationService.fetchFollowingAndBlockedAsync() + NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) return response.value } diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift index 249ff5358..2c16c8241 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift @@ -26,6 +26,10 @@ extension DataSourceFacade { dependency.context.authenticationService.fetchFollowingAndBlockedAsync() + NotificationCenter.default.post(name: .relationshipChanged, object: nil, userInfo: [ + "relationship": response + ]) + return response } @@ -70,6 +74,10 @@ extension DataSourceFacade { break } + NotificationCenter.default.post(name: .relationshipChanged, object: nil, userInfo: [ + "relationship": newRelationship + ]) + await notificationView.configure(notification: notification, authenticationBox: dependency.authContext.mastodonAuthenticationBox) } catch { // reset state when failure @@ -101,8 +109,15 @@ extension DataSourceFacade { dependency: NeedsDependency & AuthContextProvider, account: Mastodon.Entity.Account ) async throws { - _ = try await dependency.context.apiService.toggleShowReblogs( + let newRelationship = try await dependency.context.apiService.toggleShowReblogs( for: account, - authenticationBox: dependency.authContext.mastodonAuthenticationBox) + authenticationBox: dependency.authContext.mastodonAuthenticationBox + ) + + let userInfo = [ + "relationship": newRelationship, + ] + + NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) } } diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Mute.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Mute.swift index 68462e462..3e70d7b09 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Mute.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Mute.swift @@ -22,6 +22,12 @@ extension DataSourceFacade { account: account ) + let userInfo = [ + "relationship": response.value, + ] + + NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) + return response.value } } diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift index 87d1c90f9..dbfc931ba 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift @@ -199,8 +199,8 @@ extension DataSourceFacade { account: menuContext.author ) - if let completion { - completion(newRelationship as! T) + if let completion, let relationship = newRelationship as? T { + completion(relationship) } } } @@ -225,8 +225,8 @@ extension DataSourceFacade { account: menuContext.author ) - if let completion { - completion(newRelationship as! T) + if let completion, let relationship = newRelationship as? T { + completion(relationship) } } } diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+UserView.swift b/Mastodon/Protocol/Provider/DataSourceFacade+UserView.swift index 097f430b5..de70e6304 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+UserView.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+UserView.swift @@ -13,42 +13,11 @@ extension DataSourceFacade { buttonState: UserView.ButtonState ) async throws { switch buttonState { - case .follow: + case .follow, .request, .unfollow, .blocked, .pending: _ = try await DataSourceFacade.responseToUserFollowAction( dependency: dependency, account: account ) - - dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followingUserIds.append(account.id) - case .request: - _ = try await DataSourceFacade.responseToUserFollowAction( - dependency: dependency, - account: account - ) - - dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followRequestedUserIDs.append(account.id) - case .unfollow: - _ = try await DataSourceFacade.responseToUserFollowAction( - dependency: dependency, - account: account - ) - - dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followingUserIds.removeAll(where: { $0 == account.id }) - case .blocked: - _ = try await DataSourceFacade.responseToUserBlockAction( - dependency: dependency, - account: account - ) - - dependency.authContext.mastodonAuthenticationBox.inMemoryCache.blockedUserIds.append(account.id) - - case .pending: - _ = try await DataSourceFacade.responseToUserFollowAction( - dependency: dependency, - account: account - ) - - dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followRequestedUserIDs.removeAll(where: { $0 == account.id }) case .none, .loading: break //no-op } diff --git a/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift b/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift index c7b769f4c..2e9e7ad50 100644 --- a/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift +++ b/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift @@ -115,7 +115,6 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut return } - try await DataSourceFacade.responseToUserFollowRequestAction( dependency: self, notification: notification, diff --git a/Mastodon/Scene/Profile/ProfileViewController.swift b/Mastodon/Scene/Profile/ProfileViewController.swift index 525e93827..47c93d3c1 100644 --- a/Mastodon/Scene/Profile/ProfileViewController.swift +++ b/Mastodon/Scene/Profile/ProfileViewController.swift @@ -807,13 +807,6 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate { dependency: self, account: account ) - - self.viewModel.isUpdating = false - let userInfo = [ - "relationship": newRelationship, - ] - - NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) } } alertController.addAction(unblockAction) @@ -838,6 +831,7 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate { self.viewModel.isUpdating = false + // we need to trigger this here as domain block doesn't return a relationship let userInfo = [ "relationship": newRelationship, ] @@ -862,16 +856,7 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate { let unmuteAction = UIAlertAction(title: L10n.Common.Controls.Friendship.unmute, style: .default) { [weak self] _ in guard let self else { return } Task { - - let newRelationship = try await DataSourceFacade.responseToUserMuteAction(dependency: self, account: account) - - self.viewModel.isUpdating = false - let userInfo = [ - "relationship": newRelationship, - ] - - NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) - + _ = try await DataSourceFacade.responseToUserMuteAction(dependency: self, account: account) } } alertController.addAction(unmuteAction) @@ -882,42 +867,10 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate { Task { [weak self] in guard let self else { return } - let newRelationship = try await DataSourceFacade.responseToUserFollowAction( + _ = try await DataSourceFacade.responseToUserFollowAction( dependency: self, account: viewModel.account ) - - self.viewModel.relationship = newRelationship - let mastodonAuthenticationBox = self.viewModel.authContext.mastodonAuthenticationBox - - let account = self.viewModel.account - if let domain = account.domain, let updatedAccount = try await self.viewModel.context.apiService.fetchUser( - username: self.viewModel.account.acct, - domain: domain, - authenticationBox: mastodonAuthenticationBox - ) { - self.viewModel.account = updatedAccount - } - - let me = self.viewModel.me - if let domain = me.domain, let updatedMe = try? await self.viewModel.context.apiService.fetchUser( - username: me.acct, - domain: domain, - authenticationBox: mastodonAuthenticationBox - ) { - FileManager.default.store(account: updatedMe, forUserID: self.viewModel.authContext.mastodonAuthenticationBox) - self.viewModel.me = updatedMe - } - - self.viewModel.isUpdating = false - let userInfo = [ - "relationship": newRelationship, - ] - - NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) - - - self.viewModel.isUpdating = false } } } @@ -961,11 +914,7 @@ extension ProfileViewController: MastodonMenuDelegate { statusViewModel: nil, button: nil, barButtonItem: self.moreMenuBarButtonItem - )) { [weak self] newRelationship in - NotificationCenter.default.post(name: .relationshipChanged, object: nil, userInfo: [ - "relationship": newRelationship - ]) - } + )) } case .reportUser(_), .shareUser(_): Task { @@ -1053,24 +1002,37 @@ extension ProfileViewController { @objc func relationshipChanged(_ notification: Notification) { - guard let userInfo = notification.userInfo, let relationship = userInfo["relationship"] as? Mastodon.Entity.Relationship, viewModel.account.id == relationship.id else { + guard let userInfo = notification.userInfo, let relationship = userInfo["relationship"] as? Mastodon.Entity.Relationship else { return } - 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 - } + viewModel.isUpdating = true + if viewModel.account.id == relationship.id { + // if relationship belongs to an other account + Task { + 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 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 + self.profileHeaderViewController.viewModel.relationship = relationship + self.profileHeaderViewController.profileHeaderView.viewModel.relationship = relationship + } + + viewModel.isUpdating = false + } + } else if viewModel.account == viewModel.me { + // update my profile + Task { + if let updatedMe = try? await context.apiService.authenticatedUserInfo(authenticationBox: authContext.mastodonAuthenticationBox).value { + viewModel.me = updatedMe + viewModel.account = updatedMe + FileManager.default.store(account: updatedMe, forUserID: authContext.mastodonAuthenticationBox.authentication.userIdentifier()) + } + + viewModel.isUpdating = false } - 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 657296009..a0c4a9bd1 100644 --- a/Mastodon/Scene/Share/View/TableviewCell/UserTableViewCell+ViewModel.swift +++ b/Mastodon/Scene/Share/View/TableviewCell/UserTableViewCell+ViewModel.swift @@ -74,16 +74,7 @@ extension UserTableViewCellDelegate where Self: NeedsDependency & AuthContextPro await MainActor.run { view.viewModel.relationship = relationship view.updateButtonState(with: relationship, isMe: isMe) - - if let relationship { - let userInfo = [ - "relationship": relationship, - ] - - NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) - } } - } } }