From ffd11b91efefc7985b0669b08dc8eca012fb6eaa Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 28 Jan 2018 11:33:45 -0800 Subject: [PATCH] Register for UserDefaults.didChangeNotification instead of using KVO on NSUserDefaultsController. --- .../Timeline/TimelineViewController.swift | 70 ++++++++----------- 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/Evergreen/MainWindow/Timeline/TimelineViewController.swift b/Evergreen/MainWindow/Timeline/TimelineViewController.swift index d4b712d90..3f9ec035e 100644 --- a/Evergreen/MainWindow/Timeline/TimelineViewController.swift +++ b/Evergreen/MainWindow/Timeline/TimelineViewController.swift @@ -50,10 +50,16 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { } private var didRegisterForNotifications = false - private let timelineFontSizeKVOKey = "values.{AppDefaults.Key.timelineFontSize}" private var reloadAvailableCellsTimer: Timer? private var fetchAndMergeArticlesTimer: Timer? + private var sortDirection = AppDefaults.shared.timelineSortDirection { + didSet { + if sortDirection != oldValue { + sortDirectionDidChange() + } + } + } private var articles = ArticleArray() { didSet { if articles != oldValue { @@ -66,7 +72,9 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { private var fontSize: FontSize = AppDefaults.shared.timelineFontSize { didSet { - fontSizeDidChange() + if fontSize != oldValue { + fontSizeDidChange() + } } } @@ -122,43 +130,24 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil) - NSUserDefaultsController.shared.addObserver(self, forKeyPath: timelineFontSizeKVOKey, options: NSKeyValueObservingOptions(rawValue: 0), context: nil) - - didRegisterForNotifications = true + didRegisterForNotifications = true } } - // MARK: KVO - - override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { - - if let keyPath = keyPath { - - switch (keyPath) { - - case timelineFontSizeKVOKey: - fontSizeInDefaultsDidChange() - return - default: - break - } - } - - super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context) - } - // MARK: Appearance Change private func fontSizeDidChange() { + TimelineCellData.emptyCache() + RSSingleLineRenderer.emptyCache() + RSMultiLineRenderer.emptyCache() + cellAppearance = TimelineCellAppearance(theme: appDelegate.currentTheme, showAvatar: false, fontSize: fontSize) cellAppearanceWithAvatar = TimelineCellAppearance(theme: appDelegate.currentTheme, showAvatar: true, fontSize: fontSize) updateRowHeights() - if tableView.rowHeight != currentRowHeight { - tableView.rowHeight = currentRowHeight - tableView.reloadData() - } + tableView.reloadData() } // MARK: - API @@ -359,18 +348,6 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { } } - func fontSizeInDefaultsDidChange() { - - TimelineCellData.emptyCache() - RSSingleLineRenderer.emptyCache() - RSMultiLineRenderer.emptyCache() - - let updatedFontSize = AppDefaults.shared.timelineFontSize - if updatedFontSize != self.fontSize { - self.fontSize = updatedFontSize - } - } - @objc func accountDidDownloadArticles(_ note: Notification) { guard let feeds = note.userInfo?[Account.UserInfoKey.feeds] as? Set else { @@ -383,6 +360,12 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { } } + @objc func userDefaultsDidChange(_ note: Notification) { + + self.fontSize = AppDefaults.shared.timelineFontSize + self.sortDirection = AppDefaults.shared.timelineSortDirection + } + // MARK: - Reloading Data private func cellForRowView(_ rowView: NSView) -> NSView? { @@ -632,6 +615,11 @@ private extension TimelineViewController { } } + func sortDirectionDidChange() { + + // TODO + } + // MARK: Fetching Articles func fetchArticles() { @@ -647,7 +635,7 @@ private extension TimelineViewController { func updateArticles(with unsortedArticles: Set
) { - let sortedArticles = Array(unsortedArticles).sortedByDate() + let sortedArticles = Array(unsortedArticles).sortedByDate(sortDirection) if articles != sortedArticles { articles = sortedArticles }