State restoration for non-home timeline pages

This commit is contained in:
Shadowfacts 2022-11-28 16:33:19 -05:00
parent 1e950b5ccb
commit ea6698a2d8
5 changed files with 25 additions and 15 deletions

View File

@ -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

View File

@ -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()

View File

@ -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
}

View File

@ -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)
}

View File

@ -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