Modify AccountSettings so that it works like FeedMetadata and can be leveraged by the AccountDelegates

This commit is contained in:
Maurice Parker 2019-05-05 07:21:26 -05:00
parent d12a056ca3
commit 6e7477fd89
5 changed files with 57 additions and 40 deletions

View File

@ -63,7 +63,6 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
let currentNameForDisplay = nameForDisplay let currentNameForDisplay = nameForDisplay
if newValue != settings.name { if newValue != settings.name {
settings.name = newValue settings.name = newValue
settingsDirty = true
if currentNameForDisplay != nameForDisplay { if currentNameForDisplay != nameForDisplay {
postDisplayNameDidChangeNotification() postDisplayNameDidChangeNotification()
} }
@ -79,7 +78,6 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
set { set {
if newValue != settings.isActive { if newValue != settings.isActive {
settings.isActive = newValue settings.isActive = newValue
settingsDirty = true
NotificationCenter.default.post(name: .AccountStateDidChange, object: self, userInfo: nil) NotificationCenter.default.post(name: .AccountStateDidChange, object: self, userInfo: nil)
} }
} }
@ -103,7 +101,6 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
set { set {
if newValue != settings.username { if newValue != settings.username {
settings.username = newValue 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(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(childrenDidChange(_:)), name: .ChildrenDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(childrenDidChange(_:)), name: .ChildrenDidChange, object: nil)
delegate.credentials = try? retrieveBasicCredentials()
pullObjectsFromDisk() 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 // MARK: - FeedMetadataDelegate
@ -732,10 +735,12 @@ private extension Account {
func importSettings() { func importSettings() {
let url = URL(fileURLWithPath: settingsPath) let url = URL(fileURLWithPath: settingsPath)
guard let data = try? Data(contentsOf: url) else { guard let data = try? Data(contentsOf: url) else {
settings.delegate = self
return return
} }
let decoder = PropertyListDecoder() let decoder = PropertyListDecoder()
settings = (try? decoder.decode(AccountSettings.self, from: data)) ?? AccountSettings() settings = (try? decoder.decode(AccountSettings.self, from: data)) ?? AccountSettings()
settings.delegate = self
} }
func importFeedMetadata() { func importFeedMetadata() {

View File

@ -9,12 +9,13 @@
import Foundation import Foundation
import RSWeb import RSWeb
public protocol AccountDelegate { protocol AccountDelegate {
// Local account does not; some synced accounts might. // Local account does not; some synced accounts might.
var supportsSubFolders: Bool { get } var supportsSubFolders: Bool { get }
var server: String? { get } var server: String? { get }
var credentials: Credentials? { get set } var credentials: Credentials? { get set }
var settings: AccountSettings? { get set }
var refreshProgress: DownloadProgress { get } var refreshProgress: DownloadProgress { get }
@ -24,16 +25,6 @@ public protocol AccountDelegate {
func accountDidInitialize(_ account: Account) 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<Bool, Error>) -> Void) static func validateCredentials(transport: Transport, credentials: Credentials, completionHandler handler: @escaping (Result<Bool, Error>) -> Void)
} }

View File

@ -8,10 +8,46 @@
import Foundation import Foundation
protocol AccountSettingsDelegate: class {
func valueDidChange(_ accountSettings: AccountSettings, key: AccountSettings.CodingKeys)
}
final class AccountSettings: Codable { final class AccountSettings: Codable {
var name: String? enum CodingKeys: String, CodingKey {
var isActive: Bool = true case name
var username: String? 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)
}
} }

View File

@ -21,6 +21,9 @@ final class FeedbinAccountDelegate: AccountDelegate {
} }
} }
var settings: AccountSettings?
init(transport: Transport) { init(transport: Transport) {
caller = FeedbinAPICaller(transport: transport) caller = FeedbinAPICaller(transport: transport)
} }
@ -42,18 +45,7 @@ final class FeedbinAccountDelegate: AccountDelegate {
} }
func accountDidInitialize(_ account: Account) { func accountDidInitialize(_ account: Account) {
} credentials = try? account.retrieveBasicCredentials()
// MARK: Disk
func update(account: Account, withUserInfo: NSDictionary?) {
}
func userInfo(for: Account) -> NSDictionary? {
return nil
} }
} }

View File

@ -10,11 +10,12 @@ import Foundation
import RSWeb import RSWeb
final class LocalAccountDelegate: AccountDelegate { final class LocalAccountDelegate: AccountDelegate {
let supportsSubFolders = false let supportsSubFolders = false
let server: String? = nil let server: String? = nil
var credentials: Credentials? var credentials: Credentials?
var settings: AccountSettings?
private let refresher = LocalAccountRefresher() private let refresher = LocalAccountRefresher()
var refreshProgress: DownloadProgress { var refreshProgress: DownloadProgress {
@ -33,12 +34,4 @@ final class LocalAccountDelegate: AccountDelegate {
func accountDidInitialize(_ account: Account) { func accountDidInitialize(_ account: Account) {
} }
// MARK: Disk
func update(account: Account, withUserInfo: NSDictionary?) {
}
func userInfo(for: Account) -> NSDictionary? {
return nil
}
} }