Code Monkey home page Code Monkey logo

Comments (4)

jreehuis avatar jreehuis commented on June 4, 2024

It is a good practice in the MVP pattern to leave the Presenter plain old Java as much as possible. It makes it much easier to test the presenter without the need of Android Tests or Robolectric for example.

If you just want to initialize the presenter with the data, you could read the intent by getIntent() in the providePresenter() of the Activity and pass the data into the Presenter's Constructor.
If you want to "update" the presenter data by Intent data, provide a method in the presenter for it and call it with the data e.g. in onCreate().

from thirtyinch.

passsy avatar passsy commented on June 4, 2024

This is from the sample project:

    @NonNull
    @Override
    public AddEditTaskPresenter providePresenter() {
        final String taskId = getIntent()
                .getStringExtra(AddEditTaskActivity.ARGUMENT_EDIT_TASK_ID);
        final TasksRepository tasksRepository = Injection
                .provideTasksRepository(getApplicationContext());

        return new AddEditTaskPresenter(taskId, tasksRepository);
    }

from thirtyinch.

dave08 avatar dave08 commented on June 4, 2024

Thank you, that is a good idea, but since I'm using dagger2 for presenter dependencies, I had to use:

@Inject Provider<MyPresenter> presenterFactory

then I used the graph to inject it in providePresenter method. The dependecies are passed by injecting the presenter's constructor for ease of testing. So in the end I had to make a setter method on the presenter to set the state on the activity's onCreate. Am I doing this right? I thought it was wrong for the activity to do any actions, it should only be a 'passive' view displayer?

Thanks for all your answers!

from thirtyinch.

passsy avatar passsy commented on June 4, 2024

I never use dagger to inject the presenter. I don't think it's useful. You never exchange the presenter with a mock implementation when doing tests. So my Activities and Presenters are hardcoded glued together.
On the other hand using Dagger to inject dependnecies inside you presenter could be useful. If you're starting a new project consider building your app with constructor injection instead of dagger. It's often easier and has a good separation; inject android dependencies in your Activity and mock dependencies in your tests.

On android the passive view is often not possible. It is possible for your view implementation but the Activity has functionality which goes beyond the functionality of a view. Initialising the presenter, starting other Activities and handle the ActivityResult are things which aren't view related. But there is no other way to do it on Android 😞.

Don't over engineer and get things done in a testable way where it is testable, that's probably my best tip ✌️

from thirtyinch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.