From 56de0ab3591ac649db07862cd7c6db801a64591f Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 28 Feb 2020 19:47:31 -0500 Subject: [PATCH] Update profile header to always reflect most recently cached data --- .../ProfileHeaderTableViewCell.swift | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift index c747d94b..555dadd6 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift +++ b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift @@ -8,6 +8,7 @@ import UIKit import Pachyderm +import Combine protocol ProfileHeaderTableViewCellDelegate: TuskerNavigationDelegate { func showMoreOptions(cell: ProfileHeaderTableViewCell) @@ -35,6 +36,8 @@ class ProfileHeaderTableViewCell: UITableViewCell { var avatarRequest: ImageCache.Request? var headerRequest: ImageCache.Request? + private var accountUpdater: Cancellable? + override func awakeFromNib() { avatarContainerView.layer.masksToBounds = true avatarImageView.layer.masksToBounds = true @@ -62,7 +65,6 @@ class ProfileHeaderTableViewCell: UITableViewCell { usernameLabel.text = "@\(account.acct)" - avatarImageView.image = nil avatarRequest = ImageCache.avatars.get(account.avatar) { [weak self] (data) in guard let self = self, let data = data, self.accountID == accountID else { return } self.avatarRequest = nil @@ -70,7 +72,6 @@ class ProfileHeaderTableViewCell: UITableViewCell { self.avatarImageView.image = UIImage(data: data) } } - headerImageView.image = nil headerRequest = ImageCache.headers.get(account.header) { [weak self] (data) in guard let self = self, let data = data, self.accountID == accountID else { return } self.headerRequest = nil @@ -99,6 +100,7 @@ class ProfileHeaderTableViewCell: UITableViewCell { if let fields = account.fields, !fields.isEmpty { fieldsStackView.isHidden = false + fieldsStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } for field in fields { let nameLabel = UILabel() nameLabel.text = field.name @@ -120,8 +122,15 @@ class ProfileHeaderTableViewCell: UITableViewCell { } else { fieldsStackView.isHidden = true } + + if accountUpdater == nil { + accountUpdater = mastodonController.cache.accountSubject + .filter { [unowned self] in $0.id == self.accountID } + .receive(on: DispatchQueue.main) + .sink { [unowned self] in self.updateUI(for: $0.id) } + } } - + @objc func updateUIForPreferences() { guard let account = mastodonController.cache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") }