Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing GoogleApiClient on Android mvp using dagger?

there is a couple of question I have,

first, as I read some of the articles, I should implement LocationListener, ConnectionCallback,OnConnectionFailedListener interfaces in the activity,

is it right to seperate the implementation of these classes in different files?

like below?

public class LocationListener implements 
           com.google.android.gms.location.LocationListener {
@Inject
Location mLastLocation;
@Override
public void onLocationChanged(Location location) {
    // Assign the new location
    mLastLocation = location;
    // Displaying the new location on UI
 }
}

is it right in my activity I handle Showing the mLastLocation properties?

//Fields
@Inject
GoogleApiClient client;
Location mLastLocation;
//Fields
  mLastLocation = LocationServices.FusedLocationApi.getLastLocation(client);

second, How should I write the provider method for it? , my guess will be like this, what will you guys recommend?

//Constructor
public LocationModule(Context context, GoogleApiClient.ConnectionCallbacks callback, GoogleApiClient.OnConnectionFailedListener listener) {
    this.context = context;
    this.callback = callback;
    this.listener = listener;
}
@Provides
@Singleton
GoogleApiClient providesGoogleApi() {
    return new GoogleApiClient.Builder(context)
            .addOnConnectionFailedListener(listener)
            .addConnectionCallbacks(callback)
            .addApi(LocationServices.API)
            .build();
}

and finally, where should I handle the permissions for android 6 and above devices?, is it on the view, or on the presenter?

I heard that View must be so stupid that you don't need to test it, How should I keep this principle?

If anyone can give me a reference, or github sample code, which matches my case that would be so great.

like image 404
Shaheen Zahedi Avatar asked Oct 04 '16 19:10

Shaheen Zahedi


1 Answers

Last thing first, you can think of MVP view layer as the pure Android module, which means any communication with the Android operating system like requesting permission must handled using this layer and the result goes back to the presenter which decide what to do next.

About separating implementation of those class, I by myself like to separate classes for a cleaner visualisation when I am looking for some code of class! I don't think anyone can suggest a best practice because it depends on your module and implementation. According to Clean Code book, in this type of decision making situations you have to think more about your code readability.

Finally, about the LocationModule, it is totally correct, but if I was in your shoes I would even ask for Context in a higher level Component (for instance ApplicationComponent) and remove it from LocationModule constructor.

//Constructor
public LocationModule(GoogleApiClient.ConnectionCallbacks callback, GoogleApiClient.OnConnectionFailedListener listener) {
    this.callback = callback;
    this.listener = listener;
}

@Provides
@Singleton
GoogleApiClient providesGoogleApi(Context context) {
    return new GoogleApiClient.Builder(context)
            .addOnConnectionFailedListener(listener)
            .addConnectionCallbacks(callback)
            .addApi(LocationServices.API)
            .build();
}

the Context could be provide using it's relevant provider in a higher module.

Here is a sample repo which could really help you in this regard:

https://github.com/mmirhoseini/fyber_mobile_offers

like image 63
Mohsen Mirhoseini Avatar answered Oct 22 '22 10:10

Mohsen Mirhoseini