Hello everyone I have just started in MVP clean architecture and I have a question about the implementation I made of an Asynctask and a class that returns me to locate it using only gas, if someone knows how to improve the code and if I am completely wrong I appreciate your comments! I leave a part of the implementation and the repo on GitHub as well. PD: I return the data of the GpsProvider Class to the interactor by an interface and in the same way of the offender when presenting and there is my doubt, is it a good way to do it?
The GpsProvider class currently works well but it takes too long to get the location any suggestions to improve this.
MapPresenterImpl
public class MapPresenterImpl implements MapPresenter,MapInteractorImpl.onDataChanged{
private MapView view;
private MapInteractor interactor;
public MapPresenterImpl(MapView view) {
this.view = view;
this.interactor = new MapInteractorImpl(this);
}
@Override
public void setInfo(LocationManager manager,Context mContext) {
if (view != null) {
view.showProgress(true);
interactor.getCurrentInfMaps(manager,mContext);
}
}
@Override
public void notifyPresenterDataChanged(Location location) {
view.showProgress(false);
if (null != location){
view.setInfo(location);
}
}
@Override
public void notifyPresenterGpsDisabled(boolean isDisabledGps) {
if (isDisabledGps){
view.showProgress(false);
view.showMessage("The gps is disabled please enable first");
}
}
}
MapInteractorImpl
public class MapInteractorImpl implements MapInteractor,GpsProvider.listener{
private final String TAG = getClass().getSimpleName();
private onDataChanged listener;
public MapInteractorImpl(onDataChanged listener){
this.listener = listener;
}
@Override
public void getCurrentInfMaps(LocationManager manager, Context context) {
new GpsProvider(manager,context,this);
}
@Override
public void dataChanged(Location location) {
if (location!=null){
listener.notifyPresenterDataChanged(location);
Log.i("MapInteractorImpl() ",String.format("Lat: %s, Long: %s",String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude())));
} else{
listener.notifyPresenterGpsDisabled(true);
}
}
public interface onDataChanged{
void notifyPresenterDataChanged(Location location);
void notifyPresenterGpsDisabled(boolean isDisabledGps);
}
}
GpsProvider
public class GpsProvider implements android.location.LocationListener{
private LocationManager manager;
private Context context;
private listener listener;
public GpsProvider(LocationManager manager,Context context,listener listener){
this.manager = manager;
this.context = context;
this.listener = listener;
init();
}
public interface listener{
void dataChanged(Location location);
}
private final String TAG = getClass().getSimpleName();
@Override
public void onLocationChanged(Location location) {
if (location!=null){
Log.i(TAG,String.format("Lat: %s, Long: %s",String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude())));
listener.dataChanged(location);
} else{
Log.i(TAG,"Location is null");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
Log.i(TAG,"CurrentProvider enable: "+provider);
}
@Override
public void onProviderDisabled(String provider) {
Log.i(TAG,"CurrentProvider disable: "+provider);
}
@SuppressLint("MissingPermission")
public void init(){
manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (isGpsEnabled()){
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000,
10, this);
} else{
Log.i(TAG,"The gps is disabled");
listener.dataChanged(null);
}
}
public boolean isGpsEnabled(){
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
return true;
return false;
}
}