Make Account, rather than its delegate, watch for refresh progress changes. This way the delegate doesn’t have to be inited with its Account.

This commit is contained in:
Brent Simmons 2017-10-07 17:43:10 -07:00
parent b77569f38b
commit c6f683c34c
5 changed files with 18 additions and 40 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -15,7 +15,7 @@
</head>
<body>
<outline text="App">
<outline text="Make Refresh button work"/>
<outline text="Parse feeds on refresh"/>
<outline text="Update Sparkle"/>
<outline text="Use new app icon"/>
<outline text="Set -NSApplicationCrashOnExceptions YES"/>