Slighty refactor relationship-update-notification-handling (IOS-192)

Also: consider menu-changes from profile-screen
This commit is contained in:
Nathan Mattes 2024-02-16 10:34:28 +01:00
parent a1ba189822
commit ffb5c59d12
2 changed files with 23 additions and 30 deletions

View File

@ -810,9 +810,7 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate {
self.viewModel.isUpdating = false self.viewModel.isUpdating = false
let userInfo = [ let userInfo = [
"account": self.viewModel.account,
"relationship": newRelationship, "relationship": newRelationship,
"me": self.viewModel.me
] ]
NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo)
@ -841,9 +839,7 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate {
self.viewModel.isUpdating = false self.viewModel.isUpdating = false
let userInfo = [ let userInfo = [
"account": self.viewModel.account,
"relationship": newRelationship, "relationship": newRelationship,
"me": self.viewModel.me
] ]
NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo)
@ -871,9 +867,7 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate {
self.viewModel.isUpdating = false self.viewModel.isUpdating = false
let userInfo = [ let userInfo = [
"account": self.viewModel.account,
"relationship": newRelationship, "relationship": newRelationship,
"me": self.viewModel.me
] ]
NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo)
@ -917,9 +911,7 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate {
self.viewModel.isUpdating = false self.viewModel.isUpdating = false
let userInfo = [ let userInfo = [
"account": self.viewModel.account,
"relationship": newRelationship, "relationship": newRelationship,
"me": self.viewModel.me
] ]
NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo)
@ -970,9 +962,9 @@ extension ProfileViewController: MastodonMenuDelegate {
button: nil, button: nil,
barButtonItem: self.moreMenuBarButtonItem barButtonItem: self.moreMenuBarButtonItem
)) { [weak self] newRelationship in )) { [weak self] newRelationship in
guard let self else { return } NotificationCenter.default.post(name: .relationshipChanged, object: nil, userInfo: [
"relationship": newRelationship
self.viewModel.relationship = newRelationship ])
} }
} }
case .reportUser(_), .shareUser(_): case .reportUser(_), .shareUser(_):
@ -1061,20 +1053,24 @@ extension ProfileViewController {
@objc @objc
func relationshipChanged(_ notification: Notification) { 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 return
} }
if let account = userInfo["account"] as? Mastodon.Entity.Account, account == viewModel.account { Task {
viewModel.account = account 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 { if let updatedMe = try? await context.apiService.authenticatedUserInfo(authenticationBox: authContext.mastodonAuthenticationBox).value {
viewModel.me = me viewModel.me = updatedMe
FileManager.default.store(account: updatedMe, forUserID: authContext.mastodonAuthenticationBox.authentication.userIdentifier())
} }
if let relationship = userInfo["relationship"] as? Mastodon.Entity.Relationship {
viewModel.relationship = relationship viewModel.relationship = relationship
viewModel.isUpdating = false
} }
} }
} }

View File

@ -62,10 +62,7 @@ extension UserTableViewCellDelegate where Self: NeedsDependency & AuthContextPro
// Otherwise the relationship might still be `pending` // Otherwise the relationship might still be `pending`
try await Task.sleep(for: .seconds(1)) try await Task.sleep(for: .seconds(1))
guard let relationship = try await self.context.apiService.relationship(forAccounts: [account], authenticationBox: authContext.mastodonAuthenticationBox).value.first, 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 isMe: Bool let isMe: Bool
if let me { if let me {
@ -78,14 +75,14 @@ extension UserTableViewCellDelegate where Self: NeedsDependency & AuthContextPro
view.viewModel.relationship = relationship view.viewModel.relationship = relationship
view.updateButtonState(with: relationship, isMe: isMe) view.updateButtonState(with: relationship, isMe: isMe)
if let relationship {
let userInfo = [ let userInfo = [
"account": updatedAccount,
"relationship": relationship, "relationship": relationship,
"me": updatedMe
] ]
NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo) NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo)
} }
}
} }
} }