From 5d751cd9949ac76165cdfa523409840a35634725 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 15 Jun 2020 23:22:34 -0400 Subject: [PATCH] Prevent redundant status database lookups --- .../ComposeStatusReplyView.swift | 2 +- .../Views/Status/BaseStatusTableViewCell.swift | 13 ++++++++----- .../ConversationMainStatusTableViewCell.swift | 6 +++--- .../Status/TimelineStatusTableViewCell.swift | 5 +++-- Tusker/Views/StatusContentTextView.swift | 16 ++++++---------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift b/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift index b2ca19f8..bdc16152 100644 --- a/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift +++ b/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift @@ -43,7 +43,7 @@ class ComposeStatusReplyView: UIView { displayNameLabel.updateForAccountDisplayName(account: status.account) usernameLabel.text = "@\(status.account.acct)" statusContentTextView.overrideMastodonController = mastodonController - statusContentTextView.statusID = status.id + statusContentTextView.setTextFrom(status: status) avatarRequest = ImageCache.avatars.get(status.account.avatar) { [weak self] (data) in guard let self = self, let data = data else { return } diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index 159e6db8..61e2e023 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -86,7 +86,7 @@ class BaseStatusTableViewCell: UITableViewCell { accessibilityElements = [displayNameLabel!, contentWarningLabel!, collapseButton!, contentTextView!, attachmentsView!] attachmentsView.isAccessibilityElement = true - NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil) } open func createObserversIfNecessary() { @@ -125,8 +125,7 @@ class BaseStatusTableViewCell: UITableViewCell { let account = status.account self.accountID = account.id updateUI(account: account) - - updateUIForPreferences() + updateUIForPreferences(account: account) attachmentsView.updateUI(status: status) attachmentsView.isAccessibilityElement = status.attachments.count > 0 @@ -134,7 +133,7 @@ class BaseStatusTableViewCell: UITableViewCell { updateStatusState(status: status) - contentTextView.statusID = statusID + contentTextView.setTextFrom(status: status) contentWarningLabel.text = status.spoilerText contentWarningLabel.isHidden = status.spoilerText.isEmpty @@ -191,8 +190,12 @@ class BaseStatusTableViewCell: UITableViewCell { } } - @objc func updateUIForPreferences() { + @objc func preferencesChanged() { guard let mastodonController = mastodonController, let account = mastodonController.persistentContainer.account(for: accountID) else { return } + updateUIForPreferences(account: account) + } + + func updateUIForPreferences(account: AccountMO) { avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) displayNameLabel.updateForAccountDisplayName(account: account) attachmentsView.contentHidden = Preferences.shared.blurAllMedia || (mastodonController.persistentContainer.status(for: statusID)?.sensitive ?? false) diff --git a/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift b/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift index b182fb06..b259ce59 100644 --- a/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift +++ b/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift @@ -63,9 +63,9 @@ class ConversationMainStatusTableViewCell: BaseStatusTableViewCell { profileAccessibilityElement.accessibilityLabel = account.displayNameWithoutCustomEmoji } - @objc override func updateUIForPreferences() { - super.updateUIForPreferences() - + override func updateUIForPreferences(account: AccountMO) { + super.updateUIForPreferences(account: account) + favoriteAndReblogCountStackView.isHidden = !Preferences.shared.showFavoriteAndReblogCounts } diff --git a/Tusker/Views/Status/TimelineStatusTableViewCell.swift b/Tusker/Views/Status/TimelineStatusTableViewCell.swift index 24a4a4fb..7a2b1327 100644 --- a/Tusker/Views/Status/TimelineStatusTableViewCell.swift +++ b/Tusker/Views/Status/TimelineStatusTableViewCell.swift @@ -69,6 +69,7 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell { status = rebloggedStatus realStatusID = rebloggedStatus.id reblogLabel.isHidden = false + updateRebloggerLabel(reblogger: status.account) } else { reblogStatusID = nil rebloggerID = nil @@ -85,8 +86,8 @@ class TimelineStatusTableViewCell: BaseStatusTableViewCell { timestampLabel.isHidden = !pinImageView.isHidden } - @objc override func updateUIForPreferences() { - super.updateUIForPreferences() + @objc override func preferencesChanged() { + super.preferencesChanged() if let rebloggerID = rebloggerID, let reblogger = mastodonController.persistentContainer.account(for: rebloggerID) { updateRebloggerLabel(reblogger: reblogger) diff --git a/Tusker/Views/StatusContentTextView.swift b/Tusker/Views/StatusContentTextView.swift index 943eb64d..be565d70 100644 --- a/Tusker/Views/StatusContentTextView.swift +++ b/Tusker/Views/StatusContentTextView.swift @@ -11,16 +11,12 @@ import Pachyderm class StatusContentTextView: ContentTextView { - var statusID: String? { - didSet { - guard let statusID = statusID else { return } - guard let mastodonController = mastodonController, - let status = mastodonController.persistentContainer.status(for: statusID) else { - fatalError("Can't set StatusContentTextView text without cached status for \(statusID)") - } - setTextFromHtml(status.content) - setEmojis(status.emojis) - } + private var statusID: String? + + func setTextFrom(status: StatusMO) { + statusID = status.id + setTextFromHtml(status.content) + setEmojis(status.emojis) } override func getMention(for url: URL, text: String) -> Mention? {