Skip to content

Instantly share code, notes, and snippets.

@ramadana
Created June 21, 2017 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramadana/02e290fb8cfb49e92610a5f771bcd897 to your computer and use it in GitHub Desktop.
Save ramadana/02e290fb8cfb49e92610a5f771bcd897 to your computer and use it in GitHub Desktop.
//
// ManagerViewController.swift
// Skillshare
//
// Created by Stillalive on 5/29/17.
// Copyright © 2017 Stillalive. All rights reserved.
//
import UIKit
open class ManagerViewController: UIViewController {
fileprivate var cachedViewControllers = [String: UIViewController]()
fileprivate var activeViewControllerIdentifier = ""
fileprivate let CHILD_KEY = 1000000
fileprivate let CHILD_LOGIN_KEY = "NavLogin"
fileprivate let CHILD_MAIN_KEY = "MainTabBar"
// MARK: - View Lifecycles
override open func viewDidLoad() {
super.viewDidLoad()
print("[ Info ] Manager Screen Loaded")
if UserDefaults.standard.bool(forKey: SSPreferences.isLoggedIn) {
showMainScreen()
}else {
showLoginScreen()
}
(UIApplication.shared.delegate as! AppDelegate).managerVC = self
}
override open func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - View Controller Management Methods
func showLoginScreen() {
displayContentViewController(CHILD_LOGIN_KEY)
}
func showMainScreen() {
displayContentViewController(CHILD_MAIN_KEY)
}
func displayContentViewController(_ identifier: String) {
var vc: UIViewController?
if let cachedVC = cachedViewControllers[identifier] {
vc = cachedVC
}else if let vendorTabVC = storyboard?.instantiateViewController(withIdentifier: identifier) {
cachedViewControllers[identifier] = vendorTabVC
vc = vendorTabVC
}
self.addChildViewController(vc!)
let existingView = view.viewWithTag(CHILD_KEY)
if view.viewWithTag(CHILD_KEY) != nil {
vc?.view.alpha = 0
}
vc!.view.frame = view.bounds
view.addSubview(vc!.view)
if identifier == "Main" {
(vc as! UITabBarController).selectedIndex = 0
}
if existingView != nil {
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseIn, animations: {
existingView?.alpha = 0
}, completion: { finished in
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseOut, animations: {
vc?.view.alpha = 1
}, completion: { finished in
if finished {
existingView?.removeFromSuperview()
let firstVC = self.childViewControllers.first
firstVC?.removeFromParentViewController()
firstVC?.didMove(toParentViewController: nil)
}
})
})
}
vc!.view.tag = CHILD_KEY
vc!.view.setNeedsLayout()
vc!.view.layoutIfNeeded()
vc!.didMove(toParentViewController: self)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
public extension UIViewController {
public var managerViewController: ManagerViewController? {
return managerViewControllerForViewController(self)
}
fileprivate func managerViewControllerForViewController(_ controller : UIViewController) -> ManagerViewController?
{
if let managerController = controller as? ManagerViewController {
return managerController
}
if let parent = controller.parent {
return managerViewControllerForViewController(parent)
} else {
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment