Convert containerExpandedWindowState to UserDefaults.

This commit is contained in:
Brent Simmons committed 2025-11-30 21:21:53 -08:00
1 parent bdf73d6eb0
commit 2ea00619c3
2 files changed
+54 -7

No files matched your search

+10
View File
@@ -65,6 +65,7 @@ final class AppDefaults {
static let hideReadFeeds = "hideReadFeeds"
static let isShowingExtractedArticle = "isShowingExtractedArticle"
static let articleWindowScrollY = "articleWindowScrollY"
static let containerExpandedWindowState = "containerExpandedWindowState"
}
let isDeveloperBuild: Bool = {
@@ -271,6 +272,15 @@ final class AppDefaults {
}
}
var containerExpandedWindowState: [[String: String]]? {
get {
UserDefaults.standard.array(forKey: Key.containerExpandedWindowState) as? [[String: String]]
}
set {
UserDefaults.standard.set(newValue, forKey: Key.containerExpandedWindowState)
}
}
static func registerDefaults() {
let defaults: [String : Any] = [Key.userInterfaceColorPalette: UserInterfaceColorPalette.automatic.rawValue,
Key.timelineGroupByFeed: false,
+44 -7
View File
@@ -312,12 +312,30 @@ final class SceneCoordinator: NSObject, UndoableCommandRunner {
func restoreWindowState(_ activity: NSUserActivity?) {
if let activity = activity, let windowState = activity.userInfo?[UserInfoKey.windowState] as? [AnyHashable: Any] {
if let containerExpandedWindowState = windowState[UserInfoKey.containerExpandedWindowState] as? [[AnyHashable: AnyHashable]] {
let containerIdentifiers = containerExpandedWindowState.compactMap( { ContainerIdentifier(userInfo: $0) })
expandedTable = Set(containerIdentifiers)
// Migrate legacy containerExpandedWindowState to UserDefaults if not already set
let containerStateToUse: [[String: String]]
if let storedState = AppDefaults.shared.containerExpandedWindowState {
containerStateToUse = storedState
} else if let legacyState = windowState[UserInfoKey.containerExpandedWindowState] as? [[AnyHashable: AnyHashable]] {
// Convert [[AnyHashable: AnyHashable]] to [[String: String]]
let convertedState = legacyState.compactMap { dict -> [String: String]? in
var stringDict = [String: String]()
for (key, value) in dict {
if let keyString = key as? String, let valueString = value as? String {
stringDict[keyString] = valueString
}
}
return stringDict.isEmpty ? nil : stringDict
}
AppDefaults.shared.containerExpandedWindowState = convertedState
containerStateToUse = convertedState
} else {
containerStateToUse = []
}
let containerIdentifiers = containerStateToUse.compactMap { ContainerIdentifier(userInfo: $0) }
expandedTable = Set(containerIdentifiers)
if let readArticlesFilterState = windowState[UserInfoKey.readArticlesFilterState] as? [[AnyHashable: AnyHashable]: Bool] {
for key in readArticlesFilterState.keys {
if let feedIdentifier = FeedIdentifier(userInfo: key) {
@@ -343,6 +361,12 @@ final class SceneCoordinator: NSObject, UndoableCommandRunner {
} else {
// Restore containerExpandedWindowState from UserDefaults
if let storedState = AppDefaults.shared.containerExpandedWindowState {
let containerIdentifiers = storedState.compactMap { ContainerIdentifier(userInfo: $0) }
expandedTable = Set(containerIdentifiers)
}
rebuildBackingStores(initialLoad: true)
// Use state from UserDefaults
@@ -1656,6 +1680,7 @@ private extension SceneCoordinator {
func markExpanded(_ containerID: ContainerIdentifier) {
expandedTable.insert(containerID)
saveExpandedTableToUserDefaults()
}
func markExpanded(_ containerIdentifiable: ContainerIdentifiable) {
@@ -1672,6 +1697,7 @@ private extension SceneCoordinator {
func unmarkExpanded(_ containerID: ContainerIdentifier) {
expandedTable.remove(containerID)
saveExpandedTableToUserDefaults()
}
func unmarkExpanded(_ containerIdentifiable: ContainerIdentifiable) {
@@ -1686,6 +1712,19 @@ private extension SceneCoordinator {
}
}
private func saveExpandedTableToUserDefaults() {
let state = expandedTable.compactMap { containerID -> [String: String]? in
var stringDict = [String: String]()
for (key, value) in containerID.userInfo {
if let keyString = key as? String, let valueString = value as? String {
stringDict[keyString] = valueString
}
}
return stringDict.isEmpty ? nil : stringDict
}
AppDefaults.shared.containerExpandedWindowState = state
}
// MARK: Select Prev Unread
@discardableResult
@@ -2053,13 +2092,11 @@ private extension SceneCoordinator {
// MARK: NSUserActivity
func windowState() -> [AnyHashable: Any] {
let containerExpandedWindowState = expandedTable.map( { $0.userInfo })
var readArticlesFilterState = [[AnyHashable: AnyHashable]: Bool]()
for key in readFilterEnabledTable.keys {
readArticlesFilterState[key.userInfo] = readFilterEnabledTable[key]
}
return [
UserInfoKey.containerExpandedWindowState: containerExpandedWindowState,
UserInfoKey.readArticlesFilterState: readArticlesFilterState
]
}