Uber RIBs Architecture -Part 5

Integrating Builder, Interactor and Router

Mohit Sharma
3 min readJun 19, 2023

All parts in this series.

Part 1: Introduction to RIBs
Part 2:
Creation of View for logged out RIB

Part 3: Creation of Router and Interactor for logged out RIB

Part 4: Creation of Builder for logged out RIB

Part 5: Integrating Builder, Router and Interactor for logged out RIB

Now that we have our Router, View, Interactor and Builder ready for the logged out RIB, we will integrate it in this part.

All the RIBs attach and detach are done via routers.

So, we will add an instance of LoggedOutBuilder in RootRouter.

We will get the instance of LoggedOutBuilder in the RootRouter constructor. After updating the code in RootRouter, our code looks like this.

import com.uber.rib.core.ViewRouter;
import com.uber.rib.root.loggedout.LoggedOutBuilder;

/** Adds and removes children of {@link RootBuilder.RootScope}. */
public class RootRouter extends ViewRouter<RootView, RootInteractor> {

private LoggedOutBuilder loggedOutBuilder;

RootRouter(RootView view,
RootInteractor interactor,
RootBuilder.Component component,
LoggedOutBuilder loggedOutBuilder
) {
super(view, interactor, component);
this.loggedOutBuilder = loggedOutBuilder
}
}

As, we have updated, the RootRouter constructor. We will update its creation in the RootBuilder.

  @dagger.Module
public abstract static class Module {

@RootScope
@Binds
abstract RootInteractor.RootPresenter presenter(RootView view);

@RootScope
@Provides
static RootRouter router(Component component, RootView view, RootInteractor interactor) {
return new RootRouter(view, interactor, component);
}
}

In RootBuilder.Module, there is a method router which create the instance of RootRouter. Here, we will add the new instance of LoggedOutBuilder in the RootRouter Instance. We will need, dependency to build LoggedOutBuild, for which we will use component which is already present in router method. After update our code looks like this.

    @RootScope
@Provides
static RootRouter router(Component component, RootView view, RootInteractor interactor) {
return new RootRouter(view, interactor, component, new LoggedOutBuilder(component));
}

We will see a compilation error now, shown below.

To resolve this, we will extend RootBuilder.Component with LoggedOutBuilder.ParentComponent. After updating our RootBuilder.Component looks like this.

  @RootScope
@dagger.Component(modules = Module.class, dependencies = ParentComponent.class)
interface Component extends InteractorBaseComponent<RootInteractor>,
BuilderComponent, LoggedOutBuilder.ParentComponent {

@dagger.Component.Builder
interface Builder {
@BindsInstance
Builder interactor(RootInteractor interactor);

@BindsInstance
Builder view(RootView view);

Builder parentComponent(ParentComponent component);

Component build();
}
}

We will create a new method named attachLoggedOutRouter in Root Router to attach the logged out rib.

public class RootRouter extends ViewRouter<RootView, RootInteractor> {

private LoggedOutBuilder loggedOutBuilder;

RootRouter(RootView view,
RootInteractor interactor,
RootBuilder.Component component,
LoggedOutBuilder loggedOutBuilder
) {
super(view, interactor, component);
this.loggedOutBuilder = loggedOutBuilder;
}

void attachLoggedOutRouter() {
LoggedOutRouter loggedOutRouter = loggedOutBuilder.build(getView());
attachChild(loggedOutRouter);
getView().addView(loggedOutRouter.getView());
}
}

Now we will call attachLoggedOutRouter from RootInteractor:didBecomeActive method.

  @Override
protected void didBecomeActive(@Nullable Bundle savedInstanceState) {
super.didBecomeActive(savedInstanceState);
getRouter().attachLoggedOutRouter();
}

And we can now build the Tutorial1 App, and Voila!

Congratulations! You have created your 1st RIB and that too manually. ^_^

We will cover the communications between Interactor and Views in future parts.

All parts in this series.

Part 1: Introduction to RIBs
Part 2:
Creation of View for logged out RIB

Part 3: Creation of Router and Interactor for logged out RIB

Part 4: Creation of Builder for logged out RIB

Part 5: Integrating Builder, Router and Interactor for logged out RIB

--

--