From c6f683c34c5e730a2bce0cc105fe1509fbc63499 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 7 Oct 2017 17:43:10 -0700 Subject: [PATCH] =?UTF-8?q?Make=20Account,=20rather=20than=20its=20delegat?= =?UTF-8?q?e,=20watch=20for=20refresh=20progress=20changes.=20This=20way?= =?UTF-8?q?=20the=20delegate=20doesn=E2=80=99t=20have=20to=20be=20inited?= =?UTF-8?q?=20with=20its=20Account.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MainWindow/MainWindowController.swift | 1 + Frameworks/Account/Account.swift | 28 +++++++++---------- Frameworks/Account/AccountDelegate.swift | 6 ++-- .../LocalAccount/LocalAccountDelegate.swift | 21 +------------- ToDo.opml | 2 +- 5 files changed, 18 insertions(+), 40 deletions(-) diff --git a/Evergreen/MainWindow/MainWindowController.swift b/Evergreen/MainWindow/MainWindowController.swift index 8aaa9622b..21cc211f4 100644 --- a/Evergreen/MainWindow/MainWindowController.swift +++ b/Evergreen/MainWindow/MainWindowController.swift @@ -32,6 +32,7 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshDidBegin, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshDidFinish, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressDidChange(_:)), name: .AccountRefreshProgressDidChange, object: nil) } // MARK: Notifications diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index be3d835bf..39eaa310f 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -40,6 +40,7 @@ public final class Account: DisplayNameProvider, Hashable { let settingsFile: String let dataFolder: String let database: Database + let delegate: AccountDelegate var topLevelObjects = [AnyObject]() var feedIDDictionary = [String: Feed]() var username: String? @@ -75,20 +76,11 @@ public final class Account: DisplayNameProvider, Hashable { } } - public lazy var delegate: AccountDelegate! = { + init?(dataFolder: String, settingsFile: String, type: AccountType, accountID: String) { // TODO: support various syncing systems. - - switch type { - - case .onMyMac: - return LocalAccountDelegate(account: self) - default: - return nil - } - }() - - init?(dataFolder: String, settingsFile: String, type: AccountType, accountID: String) { + precondition(type == .onMyMac) + self.delegate = LocalAccountDelegate() self.accountID = accountID self.type = type @@ -99,6 +91,8 @@ public final class Account: DisplayNameProvider, Hashable { let databaseFilePath = (dataFolder as NSString).appendingPathComponent("DB.sqlite3") self.database = Database(databaseFilePath: databaseFilePath, accountID: accountID) + NotificationCenter.default.addObserver(self, selector: #selector(downloadProgressDidChange(_:)), name: .DownloadProgressDidChange, object: nil) + pullObjectsFromDisk() } @@ -106,7 +100,7 @@ public final class Account: DisplayNameProvider, Hashable { public func refreshAll() { - delegate.refreshAll() + delegate.refreshAll(for: self) } func update(_ feed: Feed, with parsedFeed: ParsedFeed, _ completion: RSVoidCompletionBlock) { @@ -189,9 +183,13 @@ public final class Account: DisplayNameProvider, Hashable { // TODO } - // MARK: - For use by delegate + // MARK: - Notifications - func noteProgressDidChange() { + @objc func downloadProgressDidChange(_ note: Notification) { + + guard let noteObject = note.object as? DownloadProgress, noteObject === refreshProgress else { + return + } refreshInProgress = refreshProgress.numberRemaining > 0 NotificationCenter.default.post(name: .AccountRefreshProgressDidChange, object: self) diff --git a/Frameworks/Account/AccountDelegate.swift b/Frameworks/Account/AccountDelegate.swift index d5be3f027..5b47aff2b 100644 --- a/Frameworks/Account/AccountDelegate.swift +++ b/Frameworks/Account/AccountDelegate.swift @@ -13,10 +13,8 @@ public protocol AccountDelegate { // Local account does not; some synced accounts might. var supportsSubFolders: Bool { get } + var refreshProgress: DownloadProgress { get } - init(account: Account) - - func refreshAll() - + func refreshAll(for: Account) } diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index a2aa81fcd..65b2d8d2e 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -13,7 +13,6 @@ final class LocalAccountDelegate: AccountDelegate { let supportsSubFolders = false private let refresher = LocalAccountRefresher() - private weak var account: Account? var refreshProgress: DownloadProgress { get { @@ -21,26 +20,8 @@ final class LocalAccountDelegate: AccountDelegate { } } - init(account: Account) { + func refreshAll(for 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) { - - account?.noteProgressDidChange() - } } diff --git a/ToDo.opml b/ToDo.opml index 699551c27..b85270a9f 100644 --- a/ToDo.opml +++ b/ToDo.opml @@ -15,7 +15,7 @@ - +