From 5d8aa94dc37f0bddedd6fc19c453bf2c4f5294b6 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 7 Oct 2017 14:40:14 -0700 Subject: [PATCH] Keep track of refresh beginning and finishing. Validate the refreshAll command (menu, toolbar). --- Evergreen/Base.lproj/MainWindow.storyboard | 1 - .../MainWindow/MainWindowController.swift | 4 ++- Frameworks/Account/Account.swift | 36 +++++++++++++++---- Frameworks/Account/AccountDelegate.swift | 4 ++- Frameworks/Account/AccountManager.swift | 4 +-- .../LocalAccount/LocalAccountDelegate.swift | 30 ++++++++++++++-- .../LocalAccount/LocalAccountRefresher.swift | 2 +- 7 files changed, 65 insertions(+), 16 deletions(-) diff --git a/Evergreen/Base.lproj/MainWindow.storyboard b/Evergreen/Base.lproj/MainWindow.storyboard index 3a38412a1..d256a6ca8 100644 --- a/Evergreen/Base.lproj/MainWindow.storyboard +++ b/Evergreen/Base.lproj/MainWindow.storyboard @@ -1,7 +1,6 @@ - diff --git a/Evergreen/MainWindow/MainWindowController.swift b/Evergreen/MainWindow/MainWindowController.swift index e9228289f..8aaa9622b 100644 --- a/Evergreen/MainWindow/MainWindowController.swift +++ b/Evergreen/MainWindow/MainWindowController.swift @@ -8,6 +8,7 @@ import Cocoa import Data +import Account private let kWindowFrameKey = "MainWindow" @@ -29,7 +30,8 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { NotificationCenter.default.addObserver(self, selector: #selector(appNavigationKeyPressed(_:)), name: .AppNavigationKeyPressed, object: nil) -// NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshProgressDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshDidBegin, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshDidFinish, object: nil) } // MARK: Notifications diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 9ef2d43c7..1af63ffdf 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -12,6 +12,12 @@ import Data import RSParser import Database +public extension Notification.Name { + + public static let AccountRefreshDidBegin = Notification.Name(rawValue: "AccountRefreshDidBegin") + public static let AccountRefreshDidFinish = Notification.Name(rawValue: "AccountRefreshDidFinish") +} + public enum AccountType: Int { // Raw values should not change since they’re stored on disk. @@ -28,7 +34,6 @@ public final class Account: DisplayNameProvider, Hashable { public let accountID: String public let type: AccountType public var nameForDisplay = "" - public let delegate: AccountDelegate public let hashValue: Int let settingsFile: String let dataFolder: String @@ -36,7 +41,19 @@ public final class Account: DisplayNameProvider, Hashable { var topLevelObjects = [AnyObject]() var feedIDDictionary = [String: Feed]() var username: String? - var refreshInProgress = false + + var refreshInProgress = false { + didSet { + if refreshInProgress != oldValue { + if refreshInProgress { + NotificationCenter.default.post(name: .AccountRefreshDidBegin, object: self) + } + else { + NotificationCenter.default.post(name: .AccountRefreshDidFinish, object: self) + } + } + } + } var hasAtLeastOneFeed: Bool { get { @@ -49,16 +66,21 @@ public final class Account: DisplayNameProvider, Hashable { return delegate.supportsSubFolders } } - - init?(dataFolder: String, settingsFile: String, type: AccountType, accountID: String) { + + public lazy var delegate: AccountDelegate! = { + + // TODO: support various syncing systems. switch type { - + case .onMyMac: - self.delegate = LocalAccountDelegate() + return LocalAccountDelegate(account: self) default: return nil } + }() + + init?(dataFolder: String, settingsFile: String, type: AccountType, accountID: String) { self.accountID = accountID self.type = type @@ -76,7 +98,7 @@ public final class Account: DisplayNameProvider, Hashable { public func refreshAll() { - delegate.refreshAll(for: self) + delegate.refreshAll() } func update(_ feed: Feed, with parsedFeed: ParsedFeed, _ completion: RSVoidCompletionBlock) { diff --git a/Frameworks/Account/AccountDelegate.swift b/Frameworks/Account/AccountDelegate.swift index 61b5d5ce0..14dadad56 100644 --- a/Frameworks/Account/AccountDelegate.swift +++ b/Frameworks/Account/AccountDelegate.swift @@ -13,6 +13,8 @@ public protocol AccountDelegate { // Local account does not; some synced accounts might. var supportsSubFolders: Bool { get } - func refreshAll(for account: Account) + init(account: Account) + + func refreshAll() } diff --git a/Frameworks/Account/AccountManager.swift b/Frameworks/Account/AccountManager.swift index 624f6009d..97fe2f0ed 100644 --- a/Frameworks/Account/AccountManager.swift +++ b/Frameworks/Account/AccountManager.swift @@ -44,8 +44,8 @@ public final class AccountManager: UnreadCountProvider { public var refreshInProgress: Bool { get { - for oneAccount in accountsDictionary.values { - if oneAccount.refreshInProgress { + for account in accounts { + if account.refreshInProgress { return true } } diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index 7c93e8fa2..2232a2d56 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -8,13 +8,37 @@ import Foundation -struct LocalAccountDelegate: AccountDelegate { +final class LocalAccountDelegate: AccountDelegate { let supportsSubFolders = false private let refresher = LocalAccountRefresher() - - func refreshAll(for account: Account) { + private weak var account: Account? + init(account: Account) { + + self.account = account + NotificationCenter.default.addObserver(self, selector: #selector(downloadProgressDidChange(_:)), name: .DownloadProgressDidChange, object: refresher.progress) + } + + func refreshAll() { + + guard let account = account else { + return + } + + account.refreshInProgress = true refresher.refreshFeeds(account.flattenedFeeds()) } + + // MARK: - Notifications + + @objc func downloadProgressDidChange(_ note: Notification) { + + if refresher.progress.numberRemaining < 1 { + account?.refreshInProgress = false + } + else { + account?.refreshInProgress = true + } + } } diff --git a/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift b/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift index 3ef5ccb03..4bba375c8 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift @@ -23,7 +23,7 @@ final class LocalAccountRefresher { return downloadSession.progress } } - + public func refreshFeeds(_ feeds: Set) { downloadSession.downloadObjects(feeds as NSSet)