Update profile-screen when relationship changes on user list (IOS-192)

This commit is contained in:
Nathan Mattes 2024-02-16 09:30:52 +01:00
parent 03e9ed7e80
commit a1ba189822
2 changed files with 18 additions and 8 deletions

View File

@ -1061,21 +1061,20 @@ extension ProfileViewController {
@objc
func relationshipChanged(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let account = userInfo["account"] as? Mastodon.Entity.Account,
let me = userInfo["me"] as? Mastodon.Entity.Account,
let relationship = userInfo["relationship"] as? Mastodon.Entity.Relationship else {
guard let userInfo = notification.userInfo else {
return
}
if account == viewModel.account {
if let account = userInfo["account"] as? Mastodon.Entity.Account, account == viewModel.account {
viewModel.account = account
}
if me == viewModel.me {
if let me = userInfo["me"] as? Mastodon.Entity.Account, me == viewModel.me {
viewModel.me = me
}
viewModel.relationship = relationship
if let relationship = userInfo["relationship"] as? Mastodon.Entity.Relationship {
viewModel.relationship = relationship
}
}
}

View File

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