Update screen when relationship changes on another screen (IOS-192)

This commit is contained in:
Nathan Mattes 2024-02-16 09:19:16 +01:00
parent 04e3a573a0
commit ecc4535058
2 changed files with 65 additions and 12 deletions

View File

@ -166,6 +166,8 @@ extension ProfileViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ProfileViewController.relationshipChanged(_:)), name: .relationshipChanged, object: nil)
view.backgroundColor = .secondarySystemBackground
let barAppearance = UINavigationBarAppearance()
if isModal {
@ -809,7 +811,13 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate {
)
self.viewModel.isUpdating = false
self.viewModel.relationship = newRelationship
let userInfo = [
"account": self.viewModel.account,
"relationship": newRelationship,
"me": self.viewModel.me
]
NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo)
}
}
alertController.addAction(unblockAction)
@ -828,15 +836,19 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate {
let unblockAction = UIAlertAction(title: L10n.Common.Controls.Actions.unblockDomain(domain), style: .default) { [weak self] _ in
guard let self else { return }
Task {
_ = try await DataSourceFacade.responseToDomainBlockAction(
dependency: self,
account: account
)
_ = try await DataSourceFacade.responseToDomainBlockAction(dependency: self, account: account)
guard let newRelationship = try await self.context.apiService.relationship(forAccounts: [account], authenticationBox: self.authContext.mastodonAuthenticationBox).value.first else { return }
self.viewModel.isUpdating = false
self.viewModel.relationship = newRelationship
let userInfo = [
"account": self.viewModel.account,
"relationship": newRelationship,
"me": self.viewModel.me
]
NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo)
}
}
alertController.addAction(unblockAction)
@ -856,13 +868,18 @@ 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
)
let newRelationship = try await DataSourceFacade.responseToUserMuteAction(dependency: self, account: account)
self.viewModel.isUpdating = false
self.viewModel.relationship = newRelationship
let userInfo = [
"account": self.viewModel.account,
"relationship": newRelationship,
"me": self.viewModel.me
]
NotificationCenter.default.post(name: .relationshipChanged, object: self, userInfo: userInfo)
}
}
alertController.addAction(unmuteAction)
@ -900,6 +917,16 @@ extension ProfileViewController: ProfileHeaderViewControllerDelegate {
self.viewModel.me = updatedMe
}
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)
self.viewModel.isUpdating = false
}
}
@ -1029,3 +1056,28 @@ extension ProfileViewController: DataSourceProvider {
viewModel.mediaUserTimelineViewModel.dataController.update(status: status, intent: intent)
}
}
//MARK: - Notifications
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 {
return
}
if account == viewModel.account {
viewModel.account = account
}
if me == viewModel.me {
viewModel.me = me
}
viewModel.relationship = relationship
}
}

View File

@ -13,6 +13,7 @@ import MastodonSDK
public extension Foundation.Notification.Name {
static let userFetched = Notification.Name(rawValue: "org.joinmastodon.app.user-fetched")
static let relationshipChanged = Notification.Name(rawValue: "org.joinmastodon.app.relationship-changed")
}
extension APIService {