Open Taehyeon-Kim opened 2 years ago
import UIKit
enum Scene {
static let detail = UIStoryboard(name: "DetailViewController", bundle: nil).instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController
static let video = UIStoryboard(name: "VideoViewController", bundle: nil).instantiateViewController(withIdentifier: "VideoViewController") as! VideoViewController
}
enum TransitionStyle {
case present
case push
}
protocol SceneManagerType: AnyObject {
func transition(to scene: UIViewController, using Style: TransitionStyle, animated: Bool)
}
final class SceneManager: SceneManagerType {
private var currentViewController: UIViewController
required init(controller currentViewController: UIViewController) {
self.currentViewController = currentViewController
}
func transition(to scene: UIViewController, using Style: TransitionStyle, animated: Bool) {
switch Style {
case .present:
currentViewController.present(scene, animated: true)
case .push:
guard let nav = currentViewController.navigationController else { return }
nav.pushViewController(scene, animated: true)
}
}
}
completion이 없으니까 다음 뷰 컨트롤러에 인자를 넘겨준다거나 할 때 가독성이 너무 안 좋아져서 클로저 형태를 이용하기로 했다.
func transition<ViewController: UIViewController>(to scene: ViewController, using Style: TransitionStyle, animated: Bool, completion: ((ViewController) -> ())? = nil) {
completion?(scene)
switch Style {
case .present:
currentViewController.present(scene, animated: true)
case .push:
guard let nav = currentViewController.navigationController else { return }
nav.pushViewController(scene, animated: true)
}
}