Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MVP. Who cares about launching and getting data from dialogs and external apps?

I like MVP concept, it helps me a lot to organize a code, but every time when i need to get a result from external app, from dialog, or request permission, I confront the same question - which component of architecture should care about this?

For example, we need to implement following behaviour:

  • click on the button starts Google voice recognition intent
  • if we have more than one result - show a list dialog and let user to choose right variant
  • do smth with it

So, we can do smth like this (also decide that Fragment implements View, contains link to Presenter and sends UI event to it):

interface View {

    void showRecognizer();//2

    void showChooseDialog(List<String> items);//4

}

interface Presenter {

    void onButtonClick();//1

    void onRecognizerResult(List<String> results);//3

    void onResultChosen(String result);//5

}

Ok, it will work, but we have some problems:

  • Algorithm is not fully encapsulated - if you want to change it, you would have to change view and presenter interfaces
  • If you have a few algorithms like this on one screen, your V & P interfaces will be huge and difficult to read and understand
  • View becames a little bit clever than it should be (both V & P knows about 1, 2, 3, 4, 5 sequence), Presenter knows about details (dialog is an implementation detail, but it asks View to launch it)
  • Algorithm is not reusable! How to use it on other screen?

So, I want to know your opinion about this, how it can be done better (or maybe you think this is ok)?

And generally speaking: starting dialogs and activities, requesting permissions, handling results, whose duty it is from the point of MVP and clean architecture?

Any thoughts or suggestions are encouraged. Please help me to clarify this, thanks.

like image 544
j2esu Avatar asked Oct 19 '22 07:10

j2esu


1 Answers

My 5 cents about MVP:

Some things are just not avoidable in View part, like starting and listening to some services (example: google maps or places).

When doing something like that, I try to leave as little logic in View and notify Presenter of any interaction (even if not useful at that moment) and then returning the action that is needed to View.

My listeners usually trigger presenter and nothing else. Then presenter triggers the view to do some function. This may be an overkill but I don't want my presenters to feel left out.

In your case, View would start a dialog and/or permission request but presenter would order it to do so. Then when the results come back, give them to Presenter to decide what to do with it (as long as you don't pass Android stuff like views).

Hope this helps.

like image 186
Marijan Avatar answered Oct 21 '22 06:10

Marijan