lyft / scoop

:icecream: micro framework for building view based modular Android applications.
Apache License 2.0
1.03k stars 79 forks source link

How would you make a sub view/screen router? #82

Closed alexwhb closed 3 years ago

alexwhb commented 7 years ago

First off thank you guys for building out this framework. From my short experience with it, it's already way more simple than using Fragments/Activities. One thing I've not been able to quite figure out yet is if you can somehow nest screens and give them a different UiContainer, then lose that container when you go to another main screen i.e. scope the sub containers? So instead of switching to a whole new view each time you call router.goTo() you can keep some common elements?

So I have a login form in my app with multiple sub views, each share a common background that should stay fixed, but I'd love to be able to call goTo( new NextScreenName()). Originally I was solving this by having a common Activity and each form view would be it's own fragment. Is this possible with the current framework? Any advice on how to solve this would be greatly appreciated.

itspbj commented 7 years ago

There are multiple solutions we either have in this repository, or are currently exploring. One solution we have for sub view controllers is:

public class ScreenAController extends ViewController {

    @BindView(R2.id.screen_a_container)
    ScreensContainer screensContainer;

    private final SubScreenRouter subScreenRouter;

    private Subscription subscription;

    @Inject
    public ScreenAController(final SubScreenRouter subScreenRouter) {
        this.subScreenRouter = subScreenRouter;
    }

    @Override
    public void onAttach() {
        super.onAttach();
        subscription = subScreenRouter
                .observeRouteChange()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onScreenChanged);
        subScreenRouter.goTo(new SubScreenA());
    }

    @Override
    public void onDetach() {
        super.onDetach();
        subscription.unsubscribe();
    }

    @Override
    public int getLayoutId() {
        return R.layout.screen_a_view;
    }

    private Action1<RouteChange> onScreenChanged = new Action1<RouteChange>() {
        @Override
        public void call(RouteChange routeChange) {
            List<Screen> toPath = routeChange.getRoute();

            if (!toPath.isEmpty()) {
                Screen next = toPath.get(toPath.size() - 1);
            }

            Keyboard.hideKeyboard(screensContainer);

            screensContainer.goTo(routeChange);
        }
    };
}

This way, you can keep another backstack inside of your screen, similar to a sub view controller.

dschaller commented 3 years ago

Thank you for you contribution to this repository.

Closing this contribution as this repository is being archived.