Taehyeon-Kim / SeSAC-MediaSsok

🎥 미디어에 쏙 빠졌다 (desc: TMDB Open API를 이용해 미디어 앱 구현하기)
Apache License 2.0
1 stars 0 forks source link

구조 개선 #13

Open Taehyeon-Kim opened 2 years ago

Taehyeon-Kim commented 2 years ago
Taehyeon-Kim commented 2 years ago

TabBar, TabBarController

Taehyeon-Kim commented 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 인자 추가

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)
    }
}