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
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() {

View File

@ -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<Bool, Error>) -> Void)
}

View File

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

View File

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

View File

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