import UIKit

public extension UIApplication {
    var topViewController: UIViewController? {
        UIApplication.shared.connectedScenes
            .filter { $0.activationState == .foregroundActive }
            .first(where: { $0 is UIWindowScene })
            .flatMap { $0 as? UIWindowScene }?
            .windows
            .first(where: \.isKeyWindow)?
            .rootViewController?
            .topMostViewController()
    }
}
