//
//  ExtensionAppDelegate.swift
//  goodTree
//
//  Created by Taeyong An on 11/12/2018.
//  Copyright © 2018 Taeyong An. All rights reserved.
//

import Foundation
import UIKit

extension AppDelegate {
    
    var visibleViewController: UIViewController? {
        
        guard let rootViewController = window?.rootViewController else {
            return nil
        }
        
        return getVisibleViewController(rootViewController)
    }
    
    private func getVisibleViewController(_ rootViewController: UIViewController) -> UIViewController? {
        
        if let presentedViewController = rootViewController.presentedViewController {
            return getVisibleViewController(presentedViewController)
        }
        
        if let navigationController = rootViewController as? UINavigationController {
            return navigationController.visibleViewController
        }
        
        if let tabBarController = rootViewController as? UITabBarController {
            return tabBarController.selectedViewController
        }
        
        return rootViewController
    }
    
    
}
