3

I am developing my first android app and since my code is quickly becoming a huge mess I decided to follow the MVP pattern.
My app is divided in Views (Fragments), Presenters, and Contracts (interfaces with the methods a view and it's presenter will use to comunicate).
My problem is that I have a form which has a field with the user's location, which I retrieve using the Google location APIs. I have a LocationHelper class which is responsible for checking/asking permissions, building the google API client, getting the location etc.
However, I do not know where to put the code which uses this class: my first approach was instantiating it in the presenter, since it is more business logic than UI stuff, however many methods need the caller activity as a parameter. For example to build the Google API client:

mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) current_activity)
            .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) current_activity)
            .addApi(LocationServices.API).build();

Or the location settings request:

status.startResolutionForResult(current_activity,REQUEST_CHECK_SETTINGS);

However, from what I've understood in MVP the presenter should have no context/android code. What would be the best practice in this case?

MaggieD
  • 35
  • 5
  • Why would a presenter necessarily have no Android code? That's a pretty arbitrary decision. – Gabe Sechan Oct 30 '17 at 13:40
  • @Gabe Sechan it defeats the purpose for testing. Your presenter shouldn't have any Android code because now you have to include it in the instrumentation test, as opposed to being able to test it without the Android dependency. – Rafa Dec 01 '17 at 01:14
  • @Rafa No, an MVP should have a model, a view, and a presenter. That's the only requirements. If you want to add other requirements you aren't talking about MVP, you're talking about something else that may be a subset of MVP – Gabe Sechan Dec 01 '17 at 01:15
  • You're right I misspoke, when I said "your presenter shouldn't have any android code" what I really should have said was, that in the context of @MaggieD 's question, it seemed like she was trying to avoid putting and SDK code inside of her presenter in order to keep a passive view and facilitate her testing. I was not implying that the one way mvp works was obviously to keep the context out, but it was not an arbitrary decision. – Rafa Dec 01 '17 at 02:06

1 Answers1

1

I'd suggest you to read this link. You can use Dagger to inject the context without having to pass it to the presenter but I don't think is a bad practice at all.

In my projects I often user this line in my BasePresenter to get all contexts instead of getting from the Activity;

        this.mContext = CorretorApplication.getInstance().getApplicationContext();