From 84cfa923cae8d0de930e5eda2696fde2aa8a7fad Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 2 Aug 2019 18:05:47 -0600 Subject: [PATCH] Use Notification for updating view appearance on preferences change --- Tusker.xcodeproj/project.pbxproj | 8 +++---- .../Preferences+Notification.swift | 13 ++++++++++ Tusker/Preferences/PreferencesAdaptive.swift | 15 ------------ .../ConversationTableViewController.swift | 12 +--------- .../NotificationsTableViewController.swift | 10 -------- .../Screens/Preferences/PreferencesView.swift | 4 ++++ .../Profile/ProfileTableViewController.swift | 21 ++++------------ .../TimelineTableViewController.swift | 12 +--------- .../LargeAccountDetailView.swift | 6 +++-- .../ComposeStatusReplyView.swift | 5 ++-- .../ActionNotificationTableViewCell.swift | 6 +++-- .../FollowNotificationTableViewCell.swift | 20 +++++++++------- .../ProfileHeaderTableViewCell.swift | 18 +++++++------- .../ConversationMainStatusTableViewCell.swift | 18 +++++++------- Tusker/Views/Status/StatusTableViewCell.swift | 24 ++++++++++--------- 15 files changed, 82 insertions(+), 110 deletions(-) create mode 100644 Tusker/Preferences/Preferences+Notification.swift delete mode 100644 Tusker/Preferences/PreferencesAdaptive.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 114cdbd0..fb294841 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -104,7 +104,6 @@ D663625F2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */; }; D663626221360B1900C9CBA2 /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = D663626121360B1900C9CBA2 /* Preferences.swift */; }; D663626421360D2300C9CBA2 /* AvatarStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = D663626321360D2300C9CBA2 /* AvatarStyle.swift */; }; - D663626A2136163000C9CBA2 /* PreferencesAdaptive.swift in Sources */ = {isa = PBXBuildFile; fileRef = D66362692136163000C9CBA2 /* PreferencesAdaptive.swift */; }; D663626C21361C6700C9CBA2 /* Account+Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = D663626B21361C6700C9CBA2 /* Account+Preferences.swift */; }; D66362712136338600C9CBA2 /* ComposeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D66362702136338600C9CBA2 /* ComposeViewController.swift */; }; D66362752137068A00C9CBA2 /* Visibility+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = D66362742137068A00C9CBA2 /* Visibility+Helpers.swift */; }; @@ -170,6 +169,7 @@ D6D58DF922074B74009C8DD9 /* LinkLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D58DF822074B74009C8DD9 /* LinkLabel.swift */; }; D6DD353B22F25D2E00A9563A /* TimelineSegment.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DD353A22F25D2E00A9563A /* TimelineSegment.swift */; }; D6DD353D22F28CD000A9563A /* ContentWarningCopyMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DD353C22F28CD000A9563A /* ContentWarningCopyMode.swift */; }; + D6DD353F22F502EC00A9563A /* Preferences+Notification.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DD353E22F502EC00A9563A /* Preferences+Notification.swift */; }; D6E0DC8E216EDF1E00369478 /* Previewing.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6E0DC8D216EDF1E00369478 /* Previewing.swift */; }; D6E6F26321603F8B006A8599 /* CharacterCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6E6F26221603F8B006A8599 /* CharacterCounter.swift */; }; D6E6F26521604242006A8599 /* CharacterCounterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6E6F26421604242006A8599 /* CharacterCounterTests.swift */; }; @@ -338,7 +338,6 @@ D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationMainStatusTableViewCell.swift; sourceTree = ""; }; D663626121360B1900C9CBA2 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = ""; }; D663626321360D2300C9CBA2 /* AvatarStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarStyle.swift; sourceTree = ""; }; - D66362692136163000C9CBA2 /* PreferencesAdaptive.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesAdaptive.swift; sourceTree = ""; }; D663626B21361C6700C9CBA2 /* Account+Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Account+Preferences.swift"; sourceTree = ""; }; D66362702136338600C9CBA2 /* ComposeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeViewController.swift; sourceTree = ""; }; D66362742137068A00C9CBA2 /* Visibility+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Visibility+Helpers.swift"; sourceTree = ""; }; @@ -409,6 +408,7 @@ D6D58DF822074B74009C8DD9 /* LinkLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkLabel.swift; sourceTree = ""; }; D6DD353A22F25D2E00A9563A /* TimelineSegment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = TimelineSegment.swift; path = ../../../../../../System/Volumes/Data/Users/shadowfacts/Dev/iOS/Tusker/Pachyderm/TimelineSegment.swift; sourceTree = ""; }; D6DD353C22F28CD000A9563A /* ContentWarningCopyMode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ContentWarningCopyMode.swift; path = ../../../../../../../System/Volumes/Data/Users/shadowfacts/Dev/iOS/Tusker/Tusker/Preferences/ContentWarningCopyMode.swift; sourceTree = ""; }; + D6DD353E22F502EC00A9563A /* Preferences+Notification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "Preferences+Notification.swift"; path = "../../../../../../../System/Volumes/Data/Users/shadowfacts/Dev/iOS/Tusker/Tusker/Preferences/Preferences+Notification.swift"; sourceTree = ""; }; D6E0DC8D216EDF1E00369478 /* Previewing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Previewing.swift; sourceTree = ""; }; D6E6F26221603F8B006A8599 /* CharacterCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterCounter.swift; sourceTree = ""; }; D6E6F26421604242006A8599 /* CharacterCounterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterCounterTests.swift; sourceTree = ""; }; @@ -802,9 +802,9 @@ children = ( D663626121360B1900C9CBA2 /* Preferences.swift */, D663626321360D2300C9CBA2 /* AvatarStyle.swift */, - D66362692136163000C9CBA2 /* PreferencesAdaptive.swift */, 0427033522B30B3D000D31B6 /* Preference.swift */, D6DD353C22F28CD000A9563A /* ContentWarningCopyMode.swift */, + D6DD353E22F502EC00A9563A /* Preferences+Notification.swift */, ); path = Preferences; sourceTree = ""; @@ -1428,6 +1428,7 @@ D627FF7F217E95E000CC0648 /* DraftTableViewCell.swift in Sources */, D663626421360D2300C9CBA2 /* AvatarStyle.swift in Sources */, D679C09F215850EF00DA27FE /* XCBActions.swift in Sources */, + D6DD353F22F502EC00A9563A /* Preferences+Notification.swift in Sources */, D6538945214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift in Sources */, D6E0DC8E216EDF1E00369478 /* Previewing.swift in Sources */, D6BED174212667E900F02DA0 /* StatusTableViewCell.swift in Sources */, @@ -1458,7 +1459,6 @@ D646C95A213B5D0500269FB5 /* LargeImageInteractionController.swift in Sources */, D6F953EC212519E700CF0F2B /* TimelineTableViewController.swift in Sources */, 04586B4122B2FFB10021BD04 /* PreferencesView.swift in Sources */, - D663626A2136163000C9CBA2 /* PreferencesAdaptive.swift in Sources */, D667E5EB21349EF80057A976 /* ProfileHeaderTableViewCell.swift in Sources */, 04D14BB022B34A2800642648 /* GalleryViewController.swift in Sources */, D641C77D213CB024004B4513 /* FollowNotificationTableViewCell.swift in Sources */, diff --git a/Tusker/Preferences/Preferences+Notification.swift b/Tusker/Preferences/Preferences+Notification.swift new file mode 100644 index 00000000..a890d4f9 --- /dev/null +++ b/Tusker/Preferences/Preferences+Notification.swift @@ -0,0 +1,13 @@ +// +// Preferences+Notification.swift +// Tusker +// +// Created by Shadowfacts on 8/2/19. +// Copyright © 2019 Shadowfacts. All rights reserved. +// + +import Foundation + +extension Notification.Name { + static let preferencesChanged = Notification.Name("Tusker.preferencesChanged") +} diff --git a/Tusker/Preferences/PreferencesAdaptive.swift b/Tusker/Preferences/PreferencesAdaptive.swift deleted file mode 100644 index 6d0393a7..00000000 --- a/Tusker/Preferences/PreferencesAdaptive.swift +++ /dev/null @@ -1,15 +0,0 @@ -// -// PreferencesAdaptive.swift -// Tusker -// -// Created by Shadowfacts on 8/28/18. -// Copyright © 2018 Shadowfacts. All rights reserved. -// - -import Foundation - -protocol PreferencesAdaptive { - - func updateUIForPreferences() - -} diff --git a/Tusker/Screens/Conversation/ConversationTableViewController.swift b/Tusker/Screens/Conversation/ConversationTableViewController.swift index 2b7d70e6..42a15321 100644 --- a/Tusker/Screens/Conversation/ConversationTableViewController.swift +++ b/Tusker/Screens/Conversation/ConversationTableViewController.swift @@ -59,17 +59,7 @@ class ConversationTableViewController: EnhancedTableViewController { } } } - - override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - - for cell in tableView.visibleCells { - if let cell = cell as? PreferencesAdaptive { - cell.updateUIForPreferences() - } - } - } - + override var previewActionItems: [UIPreviewActionItem] { var actions = [UIPreviewActionItem]() if let status = MastodonCache.status(for: mainStatusID), diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index 1268c942..a743753e 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -64,16 +64,6 @@ class NotificationsTableViewController: EnhancedTableViewController { userActivity = UserActivityManager.checkNotificationsActivity() } - override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - - for cell in tableView.visibleCells { - if let cell = cell as? PreferencesAdaptive { - cell.updateUIForPreferences() - } - } - } - // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { diff --git a/Tusker/Screens/Preferences/PreferencesView.swift b/Tusker/Screens/Preferences/PreferencesView.swift index 3b69e7ea..185c98d3 100644 --- a/Tusker/Screens/Preferences/PreferencesView.swift +++ b/Tusker/Screens/Preferences/PreferencesView.swift @@ -24,6 +24,10 @@ struct PreferencesView : View { } .listStyle(.grouped) .navigationBarTitle(Text("Preferences"), displayMode: .inline) + .onDisappear { + // todo: this onDisappear callback is not called in beta 4, check again in beta 5 + NotificationCenter.default.post(name: .preferencesChanged, object: nil) + } // } } } diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index 54410b04..cc474e1f 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -10,7 +10,7 @@ import UIKit import Pachyderm import SafariServices -class ProfileTableViewController: EnhancedTableViewController, PreferencesAdaptive { +class ProfileTableViewController: EnhancedTableViewController { var accountID: String! { didSet { @@ -89,23 +89,10 @@ class ProfileTableViewController: EnhancedTableViewController, PreferencesAdapti embedChild(loadingVC!) shouldLoadOnAccountIDSet = true } - } - - override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - for cell in tableView.visibleCells { - if let cell = cell as? PreferencesAdaptive { - cell.updateUIForPreferences() - } - } - - if let accountID = accountID, - MastodonCache.account(for: accountID) != nil { - updateUIForPreferences() - } + NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } - + override var previewActionItems: [UIPreviewActionItem] { var actions = [UIPreviewActionItem]() if let account = MastodonCache.account(for: accountID) { @@ -140,7 +127,7 @@ class ProfileTableViewController: EnhancedTableViewController, PreferencesAdapti } } - func updateUIForPreferences() { + @objc func updateUIForPreferences() { guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } navigationItem.title = account.realDisplayName } diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index fb36e354..04da25ae 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -77,17 +77,7 @@ class TimelineTableViewController: EnhancedTableViewController { self.older = pagination?.older } } - - override func viewWillAppear(_ animated: Bool) { - super.viewWillAppear(animated) - - for cell in tableView.visibleCells { - if let cell = cell as? PreferencesAdaptive { - cell.updateUIForPreferences() - } - } - } - + // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { diff --git a/Tusker/Views/Account Detail/LargeAccountDetailView.swift b/Tusker/Views/Account Detail/LargeAccountDetailView.swift index 92dd6817..3b41785f 100644 --- a/Tusker/Views/Account Detail/LargeAccountDetailView.swift +++ b/Tusker/Views/Account Detail/LargeAccountDetailView.swift @@ -9,7 +9,7 @@ import UIKit import Pachyderm -class LargeAccountDetailView: UIView, PreferencesAdaptive { +class LargeAccountDetailView: UIView { var avatarImageView = UIImageView() var displayNameLabel = UILabel() @@ -44,6 +44,8 @@ class LargeAccountDetailView: UIView, PreferencesAdaptive { usernameLabel.trailingAnchor.constraint(equalTo: trailingAnchor), usernameLabel.topAnchor.constraint(equalTo: displayNameLabel.bottomAnchor) ]) + + NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } override func layoutSubviews() { @@ -52,7 +54,7 @@ class LargeAccountDetailView: UIView, PreferencesAdaptive { updateUIForPreferences() } - func updateUIForPreferences() { + @objc func updateUIForPreferences() { avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) } diff --git a/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift b/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift index df557273..ccbcca3d 100644 --- a/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift +++ b/Tusker/Views/Compose Status Reply/ComposeStatusReplyView.swift @@ -9,7 +9,7 @@ import UIKit import Pachyderm -class ComposeStatusReplyView: UIView, PreferencesAdaptive { +class ComposeStatusReplyView: UIView { @IBOutlet weak var avatarImageView: UIImageView! @IBOutlet weak var displayNameLabel: UILabel! @@ -24,9 +24,10 @@ class ComposeStatusReplyView: UIView, PreferencesAdaptive { super.awakeFromNib() updateUIForPreferences() + NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } - func updateUIForPreferences() { + @objc func updateUIForPreferences() { avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) } diff --git a/Tusker/Views/Notifications/ActionNotificationTableViewCell.swift b/Tusker/Views/Notifications/ActionNotificationTableViewCell.swift index 4d6e9313..253bb598 100644 --- a/Tusker/Views/Notifications/ActionNotificationTableViewCell.swift +++ b/Tusker/Views/Notifications/ActionNotificationTableViewCell.swift @@ -9,7 +9,7 @@ import UIKit import Pachyderm -class ActionNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { +class ActionNotificationTableViewCell: UITableViewCell { var delegate: StatusTableViewCellDelegate? { didSet { @@ -45,9 +45,11 @@ class ActionNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { actionAvatarImageView.layer.masksToBounds = true actionLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(actionPressed))) actionLabel.isUserInteractionEnabled = true + + NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } - func updateUIForPreferences() { + @objc func updateUIForPreferences() { guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } opAvatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: opAvatarImageView) diff --git a/Tusker/Views/Notifications/FollowNotificationTableViewCell.swift b/Tusker/Views/Notifications/FollowNotificationTableViewCell.swift index d14c7e50..1e3f1ad8 100644 --- a/Tusker/Views/Notifications/FollowNotificationTableViewCell.swift +++ b/Tusker/Views/Notifications/FollowNotificationTableViewCell.swift @@ -9,7 +9,7 @@ import UIKit import Pachyderm -class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { +class FollowNotificationTableViewCell: UITableViewCell { var delegate: StatusTableViewCellDelegate? @@ -29,16 +29,10 @@ class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { super.awakeFromNib() avatarImageView.layer.masksToBounds = true + + NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } - func updateUIForPreferences() { - let account = MastodonCache.account(for: accountID)! - - avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) - followLabel.text = "Followed by \(account.realDisplayName)" - displayNameLabel.text = account.realDisplayName - } - func updateUI(for notification: Pachyderm.Notification) { self.notification = notification let account = notification.account @@ -59,6 +53,14 @@ class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive { updateTimestamp() } + @objc func updateUIForPreferences() { + let account = MastodonCache.account(for: accountID)! + + avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) + followLabel.text = "Followed by \(account.realDisplayName)" + displayNameLabel.text = account.realDisplayName + } + func updateTimestamp() { timestampLabel.text = notification.createdAt.timeAgoString() let delay: DispatchTimeInterval? diff --git a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift index 8087226f..3d9fdda4 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift +++ b/Tusker/Views/Profile Header/ProfileHeaderTableViewCell.swift @@ -13,7 +13,7 @@ protocol ProfileHeaderTableViewCellDelegate: TuskerNavigationDelegate { func showMoreOptions() } -class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { +class ProfileHeaderTableViewCell: UITableViewCell { var delegate: ProfileHeaderTableViewCellDelegate? @@ -40,14 +40,8 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { avatarImageView.isUserInteractionEnabled = true headerImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(headerPressed))) headerImageView.isUserInteractionEnabled = true - } - - func updateUIForPreferences() { - guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } - avatarContainerView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarContainerView) - avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) - displayNameLabel.text = account.realDisplayName + NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) } func updateUI(for accountID: String) { @@ -118,6 +112,14 @@ class ProfileHeaderTableViewCell: UITableViewCell, PreferencesAdaptive { } } + @objc func updateUIForPreferences() { + guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } + + avatarContainerView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarContainerView) + avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) + displayNameLabel.text = account.realDisplayName + } + override func prepareForReuse() { if let url = avatarURL { ImageCache.avatars.cancel(url) diff --git a/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift b/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift index 691bfc87..ec230480 100644 --- a/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift +++ b/Tusker/Views/Status/ConversationMainStatusTableViewCell.swift @@ -10,7 +10,7 @@ import UIKit import Combine import Pachyderm -class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive { +class ConversationMainStatusTableViewCell: UITableViewCell { var delegate: StatusTableViewCellDelegate? { didSet { @@ -65,6 +65,8 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive attachmentsView.layer.cornerRadius = 5 attachmentsView.layer.masksToBounds = true + NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) + statusUpdater = MastodonCache.statusSubject .filter { $0.id == self.statusID } .receive(on: DispatchQueue.main) @@ -76,13 +78,6 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive .sink(receiveValue: updateUI(account:)) } - func updateUIForPreferences() { - guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } - - avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) - displayNameLabel.text = account.realDisplayName - } - func updateUI(statusID: String) { guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") } @@ -126,6 +121,13 @@ class ConversationMainStatusTableViewCell: UITableViewCell, PreferencesAdaptive } } + @objc func updateUIForPreferences() { + guard let account = MastodonCache.account(for: accountID) else { fatalError("Missing cached account \(accountID!)") } + + avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) + displayNameLabel.text = account.realDisplayName + } + func updateTimestamp() { guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } diff --git a/Tusker/Views/Status/StatusTableViewCell.swift b/Tusker/Views/Status/StatusTableViewCell.swift index fc57f921..c456755f 100644 --- a/Tusker/Views/Status/StatusTableViewCell.swift +++ b/Tusker/Views/Status/StatusTableViewCell.swift @@ -13,7 +13,7 @@ import Pachyderm protocol StatusTableViewCellDelegate: TuskerNavigationDelegate { } -class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { +class StatusTableViewCell: UITableViewCell { var delegate: StatusTableViewCellDelegate? { didSet { @@ -71,6 +71,8 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { attachmentsView.layer.cornerRadius = 5 attachmentsView.layer.masksToBounds = true + NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil) + statusUpdater = MastodonCache.statusSubject .filter { $0.id == self.statusID || $0.id == self.reblogStatusID } .receive(on: DispatchQueue.main) @@ -90,16 +92,6 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { }) } - func updateUIForPreferences() { - guard let account = MastodonCache.account(for: accountID) else { fatalError("") } - avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) - if let rebloggerID = rebloggerID, - let reblogger = MastodonCache.account(for: rebloggerID) { - reblogLabel.text = "Reblogged by \(reblogger.realDisplayName)" - } - displayNameLabel.text = account.realDisplayName - } - func updateUI(statusID: String) { guard var status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") } self.statusID = statusID @@ -148,6 +140,16 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive { } } } + + @objc func updateUIForPreferences() { + guard let account = MastodonCache.account(for: accountID) else { fatalError("") } + avatarImageView.layer.cornerRadius = Preferences.shared.avatarStyle.cornerRadius(for: avatarImageView) + if let rebloggerID = rebloggerID, + let reblogger = MastodonCache.account(for: rebloggerID) { + reblogLabel.text = "Reblogged by \(reblogger.realDisplayName)" + } + displayNameLabel.text = account.realDisplayName + } func updateTimestamp() { guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }