From ea6698a2d8e34b50cf1686b71e2d0c02db715719 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 28 Nov 2022 16:33:19 -0500 Subject: [PATCH] State restoration for non-home timeline pages --- Tusker/Screens/Main/MainSplitViewController.swift | 3 +-- Tusker/Screens/Main/MainTabBarViewController.swift | 10 ++++------ .../Screens/Timeline/TimelineViewController.swift | 6 ++++-- .../Timeline/TimelinesPageViewController.swift | 14 ++++++++++---- .../Utilities/SegmentedPageViewController.swift | 7 ++++++- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Tusker/Screens/Main/MainSplitViewController.swift b/Tusker/Screens/Main/MainSplitViewController.swift index c69c6f458b..54f95580d6 100644 --- a/Tusker/Screens/Main/MainSplitViewController.swift +++ b/Tusker/Screens/Main/MainSplitViewController.swift @@ -391,8 +391,7 @@ extension MainSplitViewController: TuskerRootViewController { return tabBarViewController.stateRestorationActivity() } else { if let timelinePages = navigationStackFor(item: .tab(.timelines))?.first as? TimelinesPageViewController { - let timeline = timelinePages.pageControllers[timelinePages.currentIndex] as! TimelineViewController - return timeline.stateRestorationActivity() + return timelinePages.stateRestorationActivity() } else { stateRestorationLogger.fault("MainSplitViewController: Unable to create state restoration activity") return nil diff --git a/Tusker/Screens/Main/MainTabBarViewController.swift b/Tusker/Screens/Main/MainTabBarViewController.swift index 58b81f3cb0..d5ac1a9a31 100644 --- a/Tusker/Screens/Main/MainTabBarViewController.swift +++ b/Tusker/Screens/Main/MainTabBarViewController.swift @@ -244,9 +244,8 @@ extension MainTabBarViewController: TuskerRootViewController { func stateRestorationActivity() -> NSUserActivity? { let nav = viewController(for: .timelines) as! UINavigationController var activity: NSUserActivity? - if let timelinePages = nav.viewControllers.first as? TimelinesPageViewController, - let timelineVC = timelinePages.pageControllers[timelinePages.currentIndex] as? TimelineViewController { - activity = timelineVC.stateRestorationActivity() + if let timelinePages = nav.viewControllers.first as? TimelinesPageViewController { + activity = timelinePages.stateRestorationActivity() } else { stateRestorationLogger.fault("MainTabBarViewController: Unable to create state restoration activity, couldn't find timeline/page VC") } @@ -268,12 +267,11 @@ extension MainTabBarViewController: TuskerRootViewController { if activity.activityType == UserActivityType.showTimeline.rawValue { let nav = viewController(for: .timelines) as! UINavigationController - guard let timelinePages = nav.viewControllers.first as? TimelinesPageViewController, - let timelineVC = timelinePages.pageControllers[timelinePages.currentIndex] as? TimelineViewController else { + guard let timelinePages = nav.viewControllers.first as? TimelinesPageViewController else { stateRestorationLogger.fault("MainTabBarViewController: Unable to restore timeline activity, couldn't find VC") return } - timelineVC.restoreActivity(activity) + timelinePages.restoreActivity(activity) restoreEditedDraft() } else if activity.activityType == UserActivityType.newPost.rawValue { restoreEditedDraft() diff --git a/Tusker/Screens/Timeline/TimelineViewController.swift b/Tusker/Screens/Timeline/TimelineViewController.swift index 50281e0be8..58dea58f66 100644 --- a/Tusker/Screens/Timeline/TimelineViewController.swift +++ b/Tusker/Screens/Timeline/TimelineViewController.swift @@ -252,8 +252,10 @@ class TimelineViewController: UIViewController, TimelineLikeCollectionViewContro } private func doRestore() -> Bool { - guard let activity = activityToRestore, - let statusIDs = activity.userInfo?["statusIDs"] as? [String] else { + guard let activity = activityToRestore else { + return false + } + guard let statusIDs = activity.userInfo?["statusIDs"] as? [String] else { stateRestorationLogger.fault("TimelineViewController: activity missing statusIDs") return false } diff --git a/Tusker/Screens/Timeline/TimelinesPageViewController.swift b/Tusker/Screens/Timeline/TimelinesPageViewController.swift index e8ba32ef03..4219eccf3d 100644 --- a/Tusker/Screens/Timeline/TimelinesPageViewController.swift +++ b/Tusker/Screens/Timeline/TimelinesPageViewController.swift @@ -46,21 +46,27 @@ class TimelinesPageViewController: SegmentedPageViewController { fatalError("init(coder:) has not been implemented") } + func stateRestorationActivity() -> NSUserActivity? { + return (pageControllers[currentIndex] as! TimelineViewController).stateRestorationActivity() + } + func restoreActivity(_ activity: NSUserActivity) { guard let timeline = UserActivityManager.getTimeline(from: activity) else { return } + let index: Int switch timeline { case .home: - selectPage(at: 0, animated: false) + index = 0 case .public(local: false): - selectPage(at: 1, animated: false) + index = 1 case .public(local: true): - selectPage(at: 2, animated: false) + index = 2 default: return } - let timelineVC = pageControllers[currentIndex] as! TimelineViewController + selectPage(at: index, animated: false) + let timelineVC = pageControllers[index] as! TimelineViewController timelineVC.restoreActivity(activity) } diff --git a/Tusker/Screens/Utilities/SegmentedPageViewController.swift b/Tusker/Screens/Utilities/SegmentedPageViewController.swift index 0e19512e68..ff3e8fdbf1 100644 --- a/Tusker/Screens/Utilities/SegmentedPageViewController.swift +++ b/Tusker/Screens/Utilities/SegmentedPageViewController.swift @@ -13,6 +13,7 @@ class SegmentedPageViewController: UIPageViewController, UIPageViewControllerDel let titles: [String] let pageControllers: [UIViewController] + private var initialIndex = 0 private(set) var currentIndex = 0 var segmentedControl: UISegmentedControl! @@ -43,7 +44,7 @@ class SegmentedPageViewController: UIPageViewController, UIPageViewControllerDel view.backgroundColor = .systemBackground - selectPage(at: 0, animated: false) + selectPage(at: initialIndex, animated: false) addKeyCommand(MenuController.prevSubTabCommand) addKeyCommand(MenuController.nextSubTabCommand) @@ -57,6 +58,10 @@ class SegmentedPageViewController: UIPageViewController, UIPageViewControllerDel } func selectPage(at index: Int, animated: Bool) { + guard isViewLoaded else { + initialIndex = index + return + } let direction: UIPageViewController.NavigationDirection = index - currentIndex > 0 ? .forward : .reverse setViewControllers([pageControllers[index]], direction: direction, animated: animated) navigationItem.title = pageControllers[index].title