diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 7c7355982..d951d22d0 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -63,7 +63,6 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, let currentNameForDisplay = nameForDisplay if newValue != settings.name { settings.name = newValue - settingsDirty = true if currentNameForDisplay != nameForDisplay { postDisplayNameDidChangeNotification() } @@ -79,7 +78,6 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, set { if newValue != settings.isActive { settings.isActive = newValue - settingsDirty = true NotificationCenter.default.post(name: .AccountStateDidChange, object: self, userInfo: nil) } } @@ -103,7 +101,6 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, set { if newValue != settings.username { settings.username = newValue - settingsDirty = true } } } @@ -228,7 +225,6 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(childrenDidChange(_:)), name: .ChildrenDidChange, object: nil) - delegate.credentials = try? retrieveBasicCredentials() pullObjectsFromDisk() @@ -701,6 +697,13 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } } +// MARK: - AccountSettingsDelegate + +extension Account: AccountSettingsDelegate { + func valueDidChange(_ accountSettings: AccountSettings, key: AccountSettings.CodingKeys) { + settingsDirty = true + } +} // MARK: - FeedMetadataDelegate @@ -732,10 +735,12 @@ private extension Account { func importSettings() { let url = URL(fileURLWithPath: settingsPath) guard let data = try? Data(contentsOf: url) else { + settings.delegate = self return } let decoder = PropertyListDecoder() settings = (try? decoder.decode(AccountSettings.self, from: data)) ?? AccountSettings() + settings.delegate = self } func importFeedMetadata() { diff --git a/Frameworks/Account/AccountDelegate.swift b/Frameworks/Account/AccountDelegate.swift index 6766ef848..b9cce3990 100644 --- a/Frameworks/Account/AccountDelegate.swift +++ b/Frameworks/Account/AccountDelegate.swift @@ -9,12 +9,13 @@ import Foundation import RSWeb -public protocol AccountDelegate { +protocol AccountDelegate { // Local account does not; some synced accounts might. var supportsSubFolders: Bool { get } var server: String? { get } var credentials: Credentials? { get set } + var settings: AccountSettings? { get set } var refreshProgress: DownloadProgress { get } @@ -24,16 +25,6 @@ public protocol AccountDelegate { func accountDidInitialize(_ account: Account) - // Called at the end of initializing an Account using data from disk. - // Delegate has complete control over what goes in userInfo and what it means. - // Called even if userInfo is nil, since the delegate might have other - // things to do at init time anyway. - func update(account: Account, withUserInfo: NSDictionary?) - - // Saved to disk with other Account data. Could be called at any time. - // And called many times. - func userInfo(for: Account) -> NSDictionary? - static func validateCredentials(transport: Transport, credentials: Credentials, completionHandler handler: @escaping (Result) -> Void) } diff --git a/Frameworks/Account/AccountSettings.swift b/Frameworks/Account/AccountSettings.swift index 9b97ae102..8b6e112e9 100644 --- a/Frameworks/Account/AccountSettings.swift +++ b/Frameworks/Account/AccountSettings.swift @@ -8,10 +8,46 @@ import Foundation +protocol AccountSettingsDelegate: class { + func valueDidChange(_ accountSettings: AccountSettings, key: AccountSettings.CodingKeys) +} + final class AccountSettings: Codable { - var name: String? - var isActive: Bool = true - var username: String? + enum CodingKeys: String, CodingKey { + case name + case isActive + case username + } + + var name: String? { + didSet { + if name != oldValue { + valueDidChange(.name) + } + } + } + + var isActive: Bool = true { + didSet { + if isActive != oldValue { + valueDidChange(.isActive) + } + } + } + + var username: String? { + didSet { + if username != oldValue { + valueDidChange(.username) + } + } + } + + weak var delegate: AccountSettingsDelegate? + + func valueDidChange(_ key: CodingKeys) { + delegate?.valueDidChange(self, key: key) + } } diff --git a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift index 76d7fa7b2..9cb2c1370 100644 --- a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift @@ -21,6 +21,9 @@ final class FeedbinAccountDelegate: AccountDelegate { } } + var settings: AccountSettings? + + init(transport: Transport) { caller = FeedbinAPICaller(transport: transport) } @@ -42,18 +45,7 @@ final class FeedbinAccountDelegate: AccountDelegate { } func accountDidInitialize(_ account: Account) { - } - - // MARK: Disk - - func update(account: Account, withUserInfo: NSDictionary?) { - - - } - - func userInfo(for: Account) -> NSDictionary? { - - return nil + credentials = try? account.retrieveBasicCredentials() } } diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index 2f5e525c7..ae006bb7b 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -10,11 +10,12 @@ import Foundation import RSWeb final class LocalAccountDelegate: AccountDelegate { - + let supportsSubFolders = false let server: String? = nil var credentials: Credentials? - + var settings: AccountSettings? + private let refresher = LocalAccountRefresher() var refreshProgress: DownloadProgress { @@ -33,12 +34,4 @@ final class LocalAccountDelegate: AccountDelegate { func accountDidInitialize(_ account: Account) { } - // MARK: Disk - - func update(account: Account, withUserInfo: NSDictionary?) { - } - - func userInfo(for: Account) -> NSDictionary? { - return nil - } }