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: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 12 additions and 3 deletions

View File

@ -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!)") }