From 72e5915fb83f2135bf1d0049424e23ebb8c5b2b3 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 3 Mar 2020 10:54:37 -0800 Subject: [PATCH] Refactor state restoration so that the MainWindowController has control of state restoration order. --- Mac/MainWindow/MainWindowController.swift | 16 +++++++++++++--- .../Sidebar/SidebarViewController.swift | 10 ++++++---- .../TimelineContainerViewController.swift | 15 +++++++++++++++ .../Timeline/TimelineViewController.swift | 11 +++++++++++ 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index 547200583..0e99a09f2 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -447,13 +447,15 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { extension MainWindowController: NSWindowDelegate { - func window(_ window: NSWindow, willEncodeRestorableState state: NSCoder) { + func window(_ window: NSWindow, willEncodeRestorableState coder: NSCoder) { + sidebarViewController?.encodeState(with: coder) // saveSplitViewState(to: state) } - func window(_ window: NSWindow, didDecodeRestorableState state: NSCoder) { - + func window(_ window: NSWindow, didDecodeRestorableState coder: NSCoder) { + sidebarViewController?.decodeState(with: coder) + // restoreSplitViewState(from: state) // // // Make sure the timeline view is first responder if possible, to start out viewing @@ -494,6 +496,10 @@ extension MainWindowController: SidebarDelegate { return timelineViewController.unreadCount } + func sidebarInvalidatedRestorationState(_: SidebarViewController) { + invalidateRestorableState() + } + } // MARK: - TimelineContainerViewControllerDelegate @@ -532,6 +538,10 @@ extension MainWindowController: TimelineContainerViewControllerDelegate { sidebarViewController?.selectFeed(webFeed) } + func timelineInvalidatedRestorationState(_: TimelineContainerViewController) { + invalidateRestorableState() + } + } // MARK: - NSSearchFieldDelegate diff --git a/Mac/MainWindow/Sidebar/SidebarViewController.swift b/Mac/MainWindow/Sidebar/SidebarViewController.swift index 684316041..397f12889 100644 --- a/Mac/MainWindow/Sidebar/SidebarViewController.swift +++ b/Mac/MainWindow/Sidebar/SidebarViewController.swift @@ -15,6 +15,7 @@ import RSCore protocol SidebarDelegate: class { func sidebarSelectionDidChange(_: SidebarViewController, selectedObjects: [AnyObject]?) func unreadCount(for: AnyObject) -> Int + func sidebarInvalidatedRestorationState(_: SidebarViewController) } @objc class SidebarViewController: NSViewController, NSOutlineViewDelegate, NSOutlineViewDataSource, NSMenuDelegate, UndoableCommandRunner { @@ -38,8 +39,7 @@ protocol SidebarDelegate: class { } set { treeControllerDelegate.isReadFiltered = newValue - invalidateRestorableState() - rebuildTreeAndRestoreSelection() + delegate?.sidebarInvalidatedRestorationState(self) } } @@ -93,12 +93,13 @@ protocol SidebarDelegate: class { // MARK: State Restoration - override func encodeRestorableState(with coder: NSCoder) { + func encodeState(with coder: NSCoder) { coder.encode(isReadFiltered, forKey: UserInfoKey.readFeedsFilterState) } - override func restoreState(with coder: NSCoder) { + func decodeState(with coder: NSCoder) { isReadFiltered = coder.decodeBool(forKey: UserInfoKey.readFeedsFilterState) + rebuildTreeAndRestoreSelection() } // MARK: - Notifications @@ -377,6 +378,7 @@ protocol SidebarDelegate: class { } else { isReadFiltered = true } + rebuildTreeAndRestoreSelection() } } diff --git a/Mac/MainWindow/Timeline/TimelineContainerViewController.swift b/Mac/MainWindow/Timeline/TimelineContainerViewController.swift index 7f4962581..ac77dda57 100644 --- a/Mac/MainWindow/Timeline/TimelineContainerViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineContainerViewController.swift @@ -13,6 +13,8 @@ import Articles protocol TimelineContainerViewControllerDelegate: class { func timelineSelectionDidChange(_: TimelineContainerViewController, articles: [Article]?, mode: TimelineSourceMode) func timelineRequestedWebFeedSelection(_: TimelineContainerViewController, webFeed: WebFeed) + func timelineInvalidatedRestorationState(_: TimelineContainerViewController) + } final class TimelineContainerViewController: NSViewController { @@ -91,6 +93,15 @@ final class TimelineContainerViewController: NSViewController { regularTimelineViewController.toggleReadFilter() } + // MARK: State Restoration + + func encodeState(with coder: NSCoder) { + regularTimelineViewController.encodeState(with: coder) + } + + func decodeState(with coder: NSCoder) { + regularTimelineViewController.decodeState(with: coder) + } } extension TimelineContainerViewController: TimelineDelegate { @@ -103,6 +114,10 @@ extension TimelineContainerViewController: TimelineDelegate { delegate?.timelineRequestedWebFeedSelection(self, webFeed: webFeed) } + func timelineInvalidatedRestorationState(_: TimelineViewController) { + delegate?.timelineInvalidatedRestorationState(self) + } + } private extension TimelineContainerViewController { diff --git a/Mac/MainWindow/Timeline/TimelineViewController.swift b/Mac/MainWindow/Timeline/TimelineViewController.swift index 422ef9ff2..6a977a919 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController.swift @@ -15,6 +15,7 @@ import os.log protocol TimelineDelegate: class { func timelineSelectionDidChange(_: TimelineViewController, selectedArticles: [Article]?) func timelineRequestedWebFeedSelection(_: TimelineViewController, webFeed: WebFeed) + func timelineInvalidatedRestorationState(_: TimelineViewController) } final class TimelineViewController: NSViewController, UndoableCommandRunner, UnreadCountProvider { @@ -243,6 +244,16 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr } } + // MARK: State Restoration + + func encodeState(with coder: NSCoder) { + + } + + func decodeState(with coder: NSCoder) { + + } + // MARK: - Actions @objc func openArticleInBrowser(_ sender: Any?) {