From 1b0648cd189d414ad2fb32438dac5d0ea6db8853 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 8 Jan 2018 21:34:39 -0800 Subject: [PATCH] Make feed icons show up more promptly and reliably in the timeline. --- Evergreen/Images/ImageDownloader.swift | 4 ++- .../Timeline/TimelineViewController.swift | 33 +++++++++++++++++++ .../RSCore/NSTableView+Extensions.swift | 7 ++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Evergreen/Images/ImageDownloader.swift b/Evergreen/Images/ImageDownloader.swift index 7d819a539..e6d2ae87f 100644 --- a/Evergreen/Images/ImageDownloader.swift +++ b/Evergreen/Images/ImageDownloader.swift @@ -131,6 +131,8 @@ private extension ImageDownloader { func postImageDidBecomeAvailableNotification(_ url: String) { - NotificationCenter.default.post(name: .ImageDidBecomeAvailable, object: self, userInfo: [UserInfoKey.url: url]) + DispatchQueue.main.async { + NotificationCenter.default.post(name: .ImageDidBecomeAvailable, object: self, userInfo: [UserInfoKey.url: url]) + } } } diff --git a/Evergreen/MainWindow/Timeline/TimelineViewController.swift b/Evergreen/MainWindow/Timeline/TimelineViewController.swift index 818c31e54..d822d7d98 100644 --- a/Evergreen/MainWindow/Timeline/TimelineViewController.swift +++ b/Evergreen/MainWindow/Timeline/TimelineViewController.swift @@ -51,6 +51,7 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { private var didRegisterForNotifications = false private let timelineFontSizeKVOKey = "values.{AppDefaults.Key.timelineFontSize}" + private var reloadAvailableCellsTimer: Timer? private var articles = ArticleArray() { didSet { @@ -117,6 +118,7 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(feedIconDidBecomeAvailable(_:)), name: .FeedIconDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(avatarDidBecomeAvailable(_:)), name: .AvatarDidBecomeAvailable, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(imageDidBecomeAvailable(_:)), name: .ImageDidBecomeAvailable, object: nil) NSUserDefaultsController.shared.addObserver(self, forKeyPath: timelineFontSizeKVOKey, options: NSKeyValueObservingOptions(rawValue: 0), context: nil) @@ -347,6 +349,11 @@ class TimelineViewController: NSViewController, UndoableCommandRunner { } } + @objc func imageDidBecomeAvailable(_ note: Notification) { + + queueReloadAvailableCells() + } + func fontSizeInDefaultsDidChange() { TimelineCellData.emptyCache() @@ -554,6 +561,32 @@ extension TimelineViewController: NSTableViewDelegate { private extension TimelineViewController { + func reloadAvailableCells() { + + if let indexesToReload = tableView.indexesOfAvailableRows() { + reloadCells(for: indexesToReload) + } + } + + func queueReloadAvailableCells() { + + invalidateReloadTimer() + reloadAvailableCellsTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { (timer) in + self.reloadAvailableCells() + self.invalidateReloadTimer() + } + } + + func invalidateReloadTimer() { + + if let timer = reloadAvailableCellsTimer { + if timer.isValid { + timer.invalidate() + } + reloadAvailableCellsTimer = nil + } + } + func updateShowAvatars() { if showFeedNames { diff --git a/Frameworks/RSCore/RSCore/NSTableView+Extensions.swift b/Frameworks/RSCore/RSCore/NSTableView+Extensions.swift index 5c7304626..dd79b419f 100755 --- a/Frameworks/RSCore/RSCore/NSTableView+Extensions.swift +++ b/Frameworks/RSCore/RSCore/NSTableView+Extensions.swift @@ -30,6 +30,13 @@ public extension NSTableView { return indexes.isEmpty ? nil : indexes } + func indexesOfAvailableRows() -> IndexSet? { + + var indexes = IndexSet() + enumerateAvailableRowViews { indexes.insert($1) } + return indexes.isEmpty ? nil : indexes + } + func scrollTo(row: Int) { guard let scrollView = self.enclosingScrollView else {