Update profile header to always reflect most recently cached data

This commit is contained in:
Shadowfacts 2020-02-28 19:47:31 -05:00
parent 387623a309
commit 56de0ab359
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 12 additions and 3 deletions

View File

@ -8,6 +8,7 @@
import UIKit import UIKit
import Pachyderm import Pachyderm
import Combine
protocol ProfileHeaderTableViewCellDelegate: TuskerNavigationDelegate { protocol ProfileHeaderTableViewCellDelegate: TuskerNavigationDelegate {
func showMoreOptions(cell: ProfileHeaderTableViewCell) func showMoreOptions(cell: ProfileHeaderTableViewCell)
@ -35,6 +36,8 @@ class ProfileHeaderTableViewCell: UITableViewCell {
var avatarRequest: ImageCache.Request? var avatarRequest: ImageCache.Request?
var headerRequest: ImageCache.Request? var headerRequest: ImageCache.Request?
private var accountUpdater: Cancellable?
override func awakeFromNib() { override func awakeFromNib() {
avatarContainerView.layer.masksToBounds = true avatarContainerView.layer.masksToBounds = true
avatarImageView.layer.masksToBounds = true avatarImageView.layer.masksToBounds = true
@ -62,7 +65,6 @@ class ProfileHeaderTableViewCell: UITableViewCell {
usernameLabel.text = "@\(account.acct)" usernameLabel.text = "@\(account.acct)"
avatarImageView.image = nil
avatarRequest = ImageCache.avatars.get(account.avatar) { [weak self] (data) in avatarRequest = ImageCache.avatars.get(account.avatar) { [weak self] (data) in
guard let self = self, let data = data, self.accountID == accountID else { return } guard let self = self, let data = data, self.accountID == accountID else { return }
self.avatarRequest = nil self.avatarRequest = nil
@ -70,7 +72,6 @@ class ProfileHeaderTableViewCell: UITableViewCell {
self.avatarImageView.image = UIImage(data: data) self.avatarImageView.image = UIImage(data: data)
} }
} }
headerImageView.image = nil
headerRequest = ImageCache.headers.get(account.header) { [weak self] (data) in headerRequest = ImageCache.headers.get(account.header) { [weak self] (data) in
guard let self = self, let data = data, self.accountID == accountID else { return } guard let self = self, let data = data, self.accountID == accountID else { return }
self.headerRequest = nil self.headerRequest = nil
@ -99,6 +100,7 @@ class ProfileHeaderTableViewCell: UITableViewCell {
if let fields = account.fields, !fields.isEmpty { if let fields = account.fields, !fields.isEmpty {
fieldsStackView.isHidden = false fieldsStackView.isHidden = false
fieldsStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
for field in fields { for field in fields {
let nameLabel = UILabel() let nameLabel = UILabel()
nameLabel.text = field.name nameLabel.text = field.name
@ -120,6 +122,13 @@ class ProfileHeaderTableViewCell: UITableViewCell {
} else { } else {
fieldsStackView.isHidden = true 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() { @objc func updateUIForPreferences() {