Store the account name in a settings file. Set a default name based on the account type.

This commit is contained in:
Brent Simmons 2019-03-27 22:10:14 -07:00
parent eba1dec0f7
commit 2819403d62
3 changed files with 65 additions and 6 deletions

View File

@ -44,11 +44,28 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
public let accountID: String
public let type: AccountType
public var nameForDisplay = ""
public var name = ""
public var nameForDisplay: String {
guard let name = name, !name.isEmpty else {
return defaultName
}
return name
}
public var name: String? {
get {
return settings.name
}
set {
if newValue != settings.name {
settings.name = newValue
}
}
}
public let defaultName: String
public var topLevelFeeds = Set<Feed>()
public var folders: Set<Folder>? = Set<Folder>()
private var feedDictionaryNeedsUpdate = true
private var _idToFeedDictionary = [String: Feed]()
var idToFeedDictionary: [String: Feed] {
@ -74,6 +91,11 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
private let settingsPath: String
private var settings = AccountSettings()
private var settingsDirty = false {
didSet {
queueSaveSettingsIfNeeded()
}
}
private let feedMetadataPath: String
private typealias FeedMetadataDictionary = [String: FeedMetadata]
@ -140,6 +162,19 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
self.feedMetadataPath = (dataFolder as NSString).appendingPathComponent("FeedMetadata.plist")
self.settingsPath = (dataFolder as NSString).appendingPathComponent("Settings.plist")
switch type {
case .onMyMac:
defaultName = NSLocalizedString("On My Mac", comment: "Account name")
case .feedly:
defaultName = "Feedly"
case .feedbin:
defaultName = "Feedbin"
case .feedWrangler:
defaultName = "FeedWrangler"
case .newsBlur:
defaultName = "NewsBlur"
}
NotificationCenter.default.addObserver(self, selector: #selector(downloadProgressDidChange(_:)), name: .DownloadProgressDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
@ -555,6 +590,12 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
}
}
@objc func saveSettingsIfNeeded() {
if settingsDirty {
saveSettings()
}
}
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
@ -689,6 +730,25 @@ private extension Account {
assertionFailure(error.localizedDescription)
}
}
func queueSaveSettingsIfNeeded() {
Account.saveQueue.add(self, #selector(saveSettingsIfNeeded))
}
func saveSettings() {
settingsDirty = false
let encoder = PropertyListEncoder()
encoder.outputFormat = .binary
let url = URL(fileURLWithPath: settingsPath)
do {
let data = try encoder.encode(settings)
try data.write(to: url)
}
catch {
assertionFailure(error.localizedDescription)
}
}
}
// MARK: - Private

View File

@ -9,5 +9,6 @@
import Foundation
final class AccountSettings: Codable {
var name: String?
}

View File

@ -24,8 +24,6 @@ final class LocalAccountDelegate: AccountDelegate {
}
func accountDidInitialize(_ account: Account) {
account.nameForDisplay = NSLocalizedString("On My Mac", comment: "Local Account Name")
}
// MARK: Disk