Fix article read filter which wasn't persisting correctly.

This commit is contained in:
Maurice Parker 2020-03-12 12:07:01 -05:00
parent 202ee57697
commit 343f705cd7
3 changed files with 27 additions and 23 deletions

View File

@ -58,7 +58,7 @@ struct AppDefaults {
static var windowState: [AnyHashable : Any]? {
get {
UserDefaults.standard.object(forKey: Key.windowState) as? [AnyHashable : Any]
return UserDefaults.standard.object(forKey: Key.windowState) as? [AnyHashable : Any]
}
set {
UserDefaults.standard.set(newValue, forKey: Key.windowState)

View File

@ -248,11 +248,8 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
// MARK: State Restoration
func saveState(to state: inout [AnyHashable : Any]) {
var readArticlesFilterState = [[AnyHashable: AnyHashable]: Bool]()
for key in readFilterEnabledTable.keys {
readArticlesFilterState[key.userInfo] = readFilterEnabledTable[key]
}
state[UserInfoKey.readArticlesFilterState] = readArticlesFilterState
state[UserInfoKey.readArticlesFilterStateKeys] = readFilterEnabledTable.keys.compactMap { $0.userInfo }
state[UserInfoKey.readArticlesFilterStateValues] = readFilterEnabledTable.values.compactMap( { $0 })
if selectedArticles.count == 1 {
state[UserInfoKey.articlePath] = selectedArticles.first!.pathUserInfo
@ -260,25 +257,24 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
}
func restoreState(from state: [AnyHashable : Any]) {
if let readArticlesFilterState = state[UserInfoKey.readArticlesFilterState] as? [[AnyHashable: AnyHashable]: Bool] {
for key in readArticlesFilterState.keys {
if let feedIdentifier = FeedIdentifier(userInfo: key) {
readFilterEnabledTable[feedIdentifier] = readArticlesFilterState[key]
}
}
}
guard let articlePathUserInfo = state[UserInfoKey.articlePath] as? [AnyHashable : Any],
let accountID = articlePathUserInfo[ArticlePathKey.accountID] as? String,
let account = AccountManager.shared.existingAccount(with: accountID),
let articleID = articlePathUserInfo[ArticlePathKey.articleID] as? String else {
guard let readArticlesFilterStateKeys = state[UserInfoKey.readArticlesFilterStateKeys] as? [[AnyHashable: AnyHashable]],
let readArticlesFilterStateValues = state[UserInfoKey.readArticlesFilterStateValues] as? [Bool] else {
return
}
if isReadFiltered ?? true {
for i in 0..<readArticlesFilterStateKeys.count {
if let feedIdentifier = FeedIdentifier(userInfo: readArticlesFilterStateKeys[i]) {
readFilterEnabledTable[feedIdentifier] = readArticlesFilterStateValues[i]
}
}
if let articlePathUserInfo = state[UserInfoKey.articlePath] as? [AnyHashable : Any],
let accountID = articlePathUserInfo[ArticlePathKey.accountID] as? String,
let account = AccountManager.shared.existingAccount(with: accountID),
let articleID = articlePathUserInfo[ArticlePathKey.articleID] as? String {
exceptionArticleFetcher = SingleArticleFetcher(account: account, articleID: articleID)
fetchAndReplaceArticlesSync()
}
if let selectedIndex = articles.firstIndex(where: { $0.articleID == articleID }) {
tableView.selectRow(selectedIndex)
@ -286,6 +282,12 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr
focus()
}
} else {
fetchAndReplaceArticlesSync()
}
}
// MARK: - Actions

View File

@ -19,6 +19,8 @@ struct UserInfoKey {
static let containerExpandedWindowState = "containerExpandedWindowState"
static let readFeedsFilterState = "readFeedsFilterState"
static let readArticlesFilterState = "readArticlesFilterState"
static let readArticlesFilterStateKeys = "readArticlesFilterStateKey"
static let readArticlesFilterStateValues = "readArticlesFilterStateValue"
static let selectedFeedsState = "selectedFeedsState"
}