천원의 개발

Swift RIBs RootRIB 설정하기 본문

iOS&Swift🍎/Swift

Swift RIBs RootRIB 설정하기

천 원 2023. 9. 9. 11:31

안녕하세요. 천원입니다. RootRIB 설정하는 부분은 공식문서에 없어서 간단하게 정리해두고자 포스팅합니다.

 

먼저 기존 RIB들과 동일하게 생성해줍니다.

참고로 Owns corresponding view 체크하면 view없이 동작하는 RIB 만들 수 있습니다.

SeneDelegate에서 launchRouter로 RootRIB 붙여줍니다.

private var launchRouter: LaunchRouting?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            self.window = window

            let launchRouter = RootBuilder(dependency: AppComponent()).build()
            self.launchRouter = launchRouter
            launchRouter.launch(from: window)
        }
    }

코드를 보면 RootBuilder에 dependency파라미터가 있는데 여기 AppComponent를 넣어줍니다. 그런데 AppComponent가 없으니 만들어 줍시다.

class AppComponent: Component<EmptyDependency>, RootDependency {

    init() {
        super.init(dependency: EmptyComponent())
    }
}

 

그러면 build 함수에 withListener 파라미터가 없다고 하는데 여기서 Listener 프로토콜은 상위의 RIB과 통신할 때 사용하는데 RootRIB은 상위 RIB이 없으니까 필요없겠죠? 그러니 우리는 지워두고 return을 RootRIB에서 사용하라고 만들어 둔 LaunchRouting으로 수정해 줍시다.

protocol RootBuildable: Buildable {
    func build() -> LaunchRouting
}

final class RootBuilder: Builder<RootDependency>, RootBuildable {

    override init(dependency: RootDependency) {
        super.init(dependency: dependency)
    }

    func build() -> LaunchRouting {
        let component = RootComponent(dependency: dependency)
        let viewController = RootViewController()
        let interactor = RootInteractor(presenter: viewController)
        return RootRouter(interactor: interactor, viewController: viewController)
    }
}

 

이제 보면 RootRouter가 리턴타입인 LaunchRouting만 만족하게 해주면 설정 끝입니다. RootRouter를 아래와 같이 수정해 줍니다.

final class RootRouter: LaunchRouter<RootInteractable, RootViewControllable>, RootRouting {

    // TODO: Constructor inject child builder protocols to allow building children.
    override init(interactor: RootInteractable, viewController: RootViewControllable) {
        super.init(interactor: interactor, viewController: viewController)
        interactor.router = self
    }
}

 

진짜 마음대로 정리한 RIBs Tutorial: https://keen-name-701.notion.site/RIBs-857c2bf8e9ae427caa6958d8380b1bdb?pvs=4 

'iOS&Swift🍎 > Swift' 카테고리의 다른 글

Swift Clean Architecture 정리  (1) 2023.11.23
iOS Tuist 프로젝트에 적용하기  (0) 2023.10.22
Swift RIBs 튜토리얼 (with StoryBorad)  (0) 2023.08.26
Swift Adapter  (0) 2023.08.11
Swift RIBs  (0) 2023.07.27