Search code examples
androidgoogle-apimvpgoogle-location-servicesandroid-mvp

MVP in Android - where to place Google API calls for location services?


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?


Solution

  • 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();