Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusing common views in u2020-MVP #29

Open
charleschenster opened this issue Apr 8, 2016 · 2 comments
Open

Reusing common views in u2020-MVP #29

charleschenster opened this issue Apr 8, 2016 · 2 comments

Comments

@charleschenster
Copy link

This isn't so much of an issue but more of a design question. How would you go about reusing common views in u2020-mvp?

For example, I'd like to create a common LoginView which can be shown in different activities/fragments. The associated LoginComponent, LoginPresenter, and LoginModule all use @LoginScope. The idea here is to have all the functionality of logging-in encapsulated in LoginView which I can use at anytime.

I tried to accomplish this in few ways and ran into issues. For the examples below, let's assume we want to put LoginView into our MainActivity.

  • Include LoginModule in MainComponent as follows. I ran into issues with Dagger complaining about @LoginScope being used in @MainScope.
@MainScope
@Component(
        dependencies = ApplicationComponent.class,
        modules = {
            MainModule.class,
            LoginModule.class
       }
)
  • Defining LoginComponent as the "parent" component and MainComponent as a subcomponent. This solution is not desirable either because the common component acts as the "parent" component. Also you run into the issue of not being able to use multiple common components.
@MainScope
@Subcomponent(
    modules = MainModule.class)
@LoginScope
@Component(
        dependencies = ApplicationComponent.class,
        modules = LoginModule.class
)

Is there no proper way to use common views in u2020-mvp?

The other option is to include the common view as part of a Fragment however there isn't an example here of mvp with Dagger injection of Fragments, views and its presenters.

@cosic
Copy link
Collaborator

cosic commented Apr 12, 2016

@charleschenster The u2020-mvp just a sample. As you can notice it doesn't have a right mpv implementation. For example Mosby has more clearly one. But u2020-mvp still can cover your goals.
I might offer you the next way:

  1. Create different presenters for each place where you will use this view.
  2. Create PresenterImpl interface . The our view will work with it.
  3. Create ViewImpl interface. Our view will implement it.
  4. Move the main code to ParentPresenter that implements PresenterImpl interface. ParentPresenter will work with ViewImpl.
  5. For each using of View we must create a new Presenter class that is extended from ParentPresenter, new Scope and Module, of course.
  6. And now each presenter creates through DI. For with way you should invoke presenter constructor clearly and provide it through appropriate module.

Such way is requires a more code, but it works.

For Fragments you can use our new realization of ComponentFinder class that supports of them:

public class ComponentFinder {
    private ComponentFinder(){}

    @SuppressWarnings("unchecked")
    public static <C> C findActivityComponent(Context context) {
        return ((HasComponent<C>) context).getComponent();
    }

    @SuppressWarnings({"unchecked", "ResourceType"})
    public static <C> C findFragmentComponent(Context context) {
        final HasPanel hasPanel = (HasPanel) context;
        PanelScreenService panelScreenService = (PanelScreenService) context.getSystemService(PanelScreenService.NAME);
        final String fragmentName = panelScreenService.getScreenName();
        final Fragment fragment = hasPanel.getSupportFragmentManager().findFragmentByTag(fragmentName);
        if (fragment == null) {
            throw new IllegalStateException("No fragments added");
        }
        return ((HasComponent<C>) fragment).getComponent();
    }
}

@charleschenster
Copy link
Author

Thanks @cosic for the detailed response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants