Last active
June 16, 2021 01:18
-
-
Save ellyheetov/0cadc9099c8b04ec2ea136fb554934bf to your computer and use it in GitHub Desktop.
Coordinator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BuyCoordinator: Coordinator { | |
var childCoordinators = [Coordinator]() | |
weak var parentCoordinator: MainCoordinator? | |
var navigationController: UINavigationController | |
init(navigationController: UINavigationController) { | |
self.navigationController = navigationController | |
} | |
func start(){ | |
let vc = BuyViewController.instantiate() | |
vc.coordinator = self | |
navigationController.pushViewController(vc, animated: true) | |
} | |
func didFinishBuying(){ | |
parentCoordinator?.childDidFinish(self) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BuyViewController : UIViewController, Storyboarded { | |
weak var coordinator: BuyCoordinator? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .cyan | |
} | |
override func viewDidDisappear(_ animated: Bool) { | |
super.viewDidDisappear(animated) | |
coordinator?.didFinishBuying() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
protocol Coordinator: AnyObject { | |
var childCoordinators: [Coordinator] { get set } | |
var navigationController: UINavigationController { get set } | |
func start() | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class MainCoordinator: Coordinator { | |
var childCoordinators = [Coordinator]() | |
var navigationController: UINavigationController | |
init(navigationController: UINavigationController) { | |
self.navigationController = navigationController | |
} | |
func start(){ | |
let vc = ViewController.instantiate() | |
vc.coordinator = self | |
navigationController.pushViewController(vc, animated: false) | |
} | |
func buySubscription(){ | |
let child = BuyCoordinator(navigationController: navigationController) | |
child.parentCoordinator = self | |
childCoordinators.append(child) | |
child.start() | |
} | |
func childDidFinish(_ child: Coordinator?){ | |
for (index, coordinator) in childCoordinators.enumerated() { | |
if coordinator === child { | |
childCoordinators.remove(at: index) | |
break | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class SceneDelegate: UIResponder, UIWindowSceneDelegate { | |
var window: UIWindow? | |
var coordinator: MainCoordinator? | |
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { | |
guard let windowScene = (scene as? UIWindowScene) else { return } | |
let navController = UINavigationController() | |
coordinator = MainCoordinator(navigationController: navController) | |
coordinator?.start() | |
let appWindow = UIWindow(frame: windowScene.coordinateSpace.bounds) | |
appWindow.windowScene = windowScene | |
appWindow.rootViewController = navController | |
appWindow.makeKeyAndVisible() | |
window = appWindow | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
protocol Storyboarded { | |
static func instantiate() -> Self | |
} | |
extension Storyboarded where Self: UIViewController { | |
static func instantiate() -> Self { | |
let fullName = NSStringFromClass(self) | |
let className = fullName.components(separatedBy: ".")[1] | |
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) | |
return storyboard.instantiateViewController(withIdentifier: className) as! Self | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ViewController: UIViewController, Storyboarded { | |
weak var coordinator: MainCoordinator? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
@IBAction func didTapNextButton(_ sender: Any) { | |
coordinator?.buySubscription() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment