Search code examples
javaandroidmvpandroid-mvp

MVP and multiple behaviours


I've been trying to make a simple notepad app (my first app), using MVP and dagger. I've understood most of the stuff but I'm a little bit lost if I have multiple behaviors. I'll explain.
My project hierarchy goes like this : Click here

  • MainActivity - The activity class, holds a listview.
  • MainPresenter - interface, holding a setup container method, and a refresh one
  • MainPresenterImpl - the implementation of the presenter
  • MainView - interface for the MainActivity (for mvp)
  • Note_Container - Contains the NoteHandler to load-save notes, creates/stores the adapter and can make changes
  • Ignore noteactivity its empty
  • di folder is for dependency injection
  • models has only a note class that holds two strings
  • NoteHandler - Contains the NoteRepository and handles exceptions whenever they're thrown.
  • NoteRepository - Saves and loads the notes from a file using gson library.

After explaining all of these you should (probably) have understood how the project works (feel free to criticize the hierarchy).
Now I want to add a button that creates a note. I'll just add the listener on the mainactivity and redirect it to the Presenter...
But will the presenter handle the code to add a new note (Start a new intent, etc) or should I make a new class that will independently just handle this stuff?
If I want to add more buttons in the future, for example remove-all-notes or launch a help dialog, should I make for each button a class that will handle the code?
How can I organize something like that?


Solution

  • Navigation is not formally addressed in the MVP pattern. Well, I think there are two good ways:

    1 - You can create a a Navigator/Router to handle the navigation for you. So the presenter address all this logic to the navigator or...

    2 - You can handle screen changes in the Activity. I really believe that the screen change is a View's responsibility, so all you the is to pass the navigation command to the View. Like:

    interface MainView{
        void navigateToAddCardScreen()
        void navigateToLoginScreen()
        void navigateSomeScreen()
    }
    

    I prefer the second option. The navigator makes the code too much granular for me...

    I hope I could help