Use Notification for updating view appearance on preferences change

This commit is contained in:
Shadowfacts 2019-08-02 18:05:47 -06:00
parent 199f95c465
commit 84cfa923ca
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
15 changed files with 82 additions and 110 deletions

View File

@ -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 = "<group>"; };
D663626121360B1900C9CBA2 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; };
D663626321360D2300C9CBA2 /* AvatarStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarStyle.swift; sourceTree = "<group>"; };
D66362692136163000C9CBA2 /* PreferencesAdaptive.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesAdaptive.swift; sourceTree = "<group>"; };
D663626B21361C6700C9CBA2 /* Account+Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Account+Preferences.swift"; sourceTree = "<group>"; };
D66362702136338600C9CBA2 /* ComposeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeViewController.swift; sourceTree = "<group>"; };
D66362742137068A00C9CBA2 /* Visibility+Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Visibility+Helpers.swift"; sourceTree = "<group>"; };
@ -409,6 +408,7 @@
D6D58DF822074B74009C8DD9 /* LinkLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkLabel.swift; sourceTree = "<group>"; };
D6DD353A22F25D2E00A9563A /* TimelineSegment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = TimelineSegment.swift; path = ../../../../../../System/Volumes/Data/Users/shadowfacts/Dev/iOS/Tusker/Pachyderm/TimelineSegment.swift; sourceTree = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
D6E0DC8D216EDF1E00369478 /* Previewing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Previewing.swift; sourceTree = "<group>"; };
D6E6F26221603F8B006A8599 /* CharacterCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterCounter.swift; sourceTree = "<group>"; };
D6E6F26421604242006A8599 /* CharacterCounterTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterCounterTests.swift; sourceTree = "<group>"; };
@ -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 = "<group>";
@ -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 */,

View File

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

View File

@ -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()
}

View File

@ -60,16 +60,6 @@ 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),

View File

@ -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 {

View File

@ -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)
}
// }
}
}

View File

@ -10,7 +10,7 @@ import UIKit
import Pachyderm
import SafariServices
class ProfileTableViewController: EnhancedTableViewController, PreferencesAdaptive {
class ProfileTableViewController: EnhancedTableViewController {
var accountID: String! {
didSet {
@ -89,21 +89,8 @@ 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] {
@ -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
}

View File

@ -78,16 +78,6 @@ class TimelineTableViewController: EnhancedTableViewController {
}
}
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 {

View File

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

View File

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

View File

@ -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)

View File

@ -9,7 +9,7 @@
import UIKit
import Pachyderm
class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive {
class FollowNotificationTableViewCell: UITableViewCell {
var delegate: StatusTableViewCellDelegate?
@ -29,14 +29,8 @@ class FollowNotificationTableViewCell: UITableViewCell, PreferencesAdaptive {
super.awakeFromNib()
avatarImageView.layer.masksToBounds = true
}
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
NotificationCenter.default.addObserver(self, selector: #selector(updateUIForPreferences), name: .preferencesChanged, object: nil)
}
func updateUI(for notification: Pachyderm.Notification) {
@ -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?

View File

@ -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)

View File

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

View File

@ -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
@ -149,6 +141,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!)") }