Skip to content

Commit

Permalink
Present new joke reader on already presented controller
Browse files Browse the repository at this point in the history
  • Loading branch information
balazs630 committed Feb 6, 2020
1 parent 412d12e commit 14ab6d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
7 changes: 3 additions & 4 deletions BadJokes/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
// MARK: Deeplinking
extension AppDelegate {
private func deeplinkToJokeReader(with jokeText: String) {
guard let jokeReaderViewController = JokeReaderViewController.instantiate(with: jokeText),
let navigationController = (window?.rootViewController as? UINavigationController) else {
return
guard let jokeReaderViewController = JokeReaderViewController.instantiate(with: jokeText) else {
return
}

navigationController.present(jokeReaderViewController, animated: true)
UIApplication.topMostViewController()?.present(jokeReaderViewController, animated: true)
}
}
17 changes: 16 additions & 1 deletion BadJokes/Extensions/UIApplicationExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@
import UIKit

extension UIApplication {
public static func openSettings() {
static func openSettings() {
guard let url = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(url) else {
debugPrint("Cannot open 'UIApplication.openSettingsURLString'")
return
}

UIApplication.shared.open(url)
}

// swiftlint:disable line_length
class func topMostViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = base as? UINavigationController {
return topMostViewController(base: navigationController.visibleViewController)

} else if let tabBarController = base as? UITabBarController, let selected = tabBarController.selectedViewController {
return topMostViewController(base: selected)

} else if let presented = base?.presentedViewController {
return topMostViewController(base: presented)
}

return base
}
}

0 comments on commit 14ab6d0

Please sign in to comment.