Skip to content

Commit

Permalink
Crash when opening audio channel view from media player view (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyby authored Mar 15, 2024
1 parent bc68418 commit b792ea4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ extension RadioChannelsViewController: PlayApplicationNavigation {
func open(_ applicationSectionInfo: ApplicationSectionInfo) -> Bool {
guard let radioChannel = applicationSectionInfo.radioChannel else { return false }

if let radioChannelViewController = viewControllers.first(where: { ($0 as? PageViewController)?.radioChannel == radioChannel }) as? UIViewController & PlayApplicationNavigation {
let pageIndex = viewControllers.firstIndex(of: radioChannelViewController)!
if let radioChannelViewController = viewControllers.first(where: { ($0 as? PageViewController)?.radioChannel == radioChannel }) as? UIViewController & PlayApplicationNavigation,
let pageIndex = viewControllers.firstIndex(of: radioChannelViewController) {
_ = self.switchToIndex(pageIndex, animated: false)

return radioChannelViewController.open(applicationSectionInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import SRGAppearance

class PageContainerViewController: UIViewController {
let viewControllers: [UIViewController]
let initialPage: Int

private(set) var initialPage: Int

private var pageViewController: UIPageViewController

Expand Down Expand Up @@ -137,7 +138,7 @@ class PageContainerViewController: UIViewController {

coordinator.animate(alongsideTransition: { _ in
// viewWillTransition(to:with:) could be called before controller view is loaded.
if self.tabBar != nil {
if self.isViewLoaded {
// Force a refresh of the tab bar so that the alignment is correct after rotation
self.tabBar.alignment = .leading
self.tabBar.alignment = .center
Expand All @@ -162,28 +163,44 @@ class PageContainerViewController: UIViewController {
}

func switchToIndex(_ index: Int, animated: Bool) -> Bool {
guard displayPage(at: index, animated: animated) else { return false }
guard index < viewControllers.count else { return false }

tabBar.setSelectedItem(tabBar.items[index], animated: animated)
return true
if self.isViewLoaded {
guard displayPage(at: index, animated: animated) else { return false }

tabBar.setSelectedItem(tabBar.items[index], animated: animated)
return true
}
else {
initialPage = index
return true
}
}

func displayPage(at index: Int, animated: Bool) -> Bool {
private func displayPage(at index: Int, animated: Bool) -> Bool {
guard index < viewControllers.count else { return false }

let currentViewController = pageViewController.viewControllers!.first!
let currentIndex = viewControllers.firstIndex(of: currentViewController)!
let direction: UIPageViewController.NavigationDirection = index > currentIndex ? .forward : .reverse

let newViewController = viewControllers[index]
pageViewController.setViewControllers([newViewController], direction: direction, animated: animated)
self.play_setNeedsScrollableViewUpdate()

didDisplayViewController(newViewController, animated: animated)
return true
if self.isViewLoaded {
var direction: UIPageViewController.NavigationDirection = .forward
if let currentViewController = pageViewController.viewControllers?.first {
let currentIndex = viewControllers.firstIndex(of: currentViewController)!
direction = index > currentIndex ? .forward : .reverse
}

let newViewController = viewControllers[index]
pageViewController.setViewControllers([newViewController], direction: direction, animated: animated)
self.play_setNeedsScrollableViewUpdate()

didDisplayViewController(newViewController, animated: animated)
return true
}
else {
initialPage = index
return true
}
}

func updateFonts() {
private func updateFonts() {
let tabBarFont = SRGFont.font(.body) as UIFont
tabBar.unselectedItemTitleFont = tabBarFont
tabBar.selectedItemTitleFont = tabBarFont
Expand Down

0 comments on commit b792ea4

Please sign in to comment.