Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should the android service calls and calls to GoogleAPIClient be written while using MVP pattern in android?

I am trying to implement MVP pattern in my android project by referring to this link : https://github.com/jpotts18/android-mvp

I have successfully implemented the view / presenter / interactor classes. I am not clear on

  • Where to put the service call code?

Since i cannot get the context inside the presenter or interactor class, I am not able to put the service call there

  • Where to implement the GoogleApiClient class?

Since GoogleApiClient also requires context to run, it also cannot be implemented inside the presenter or interactor without a context

like image 603
BlackCursor Avatar asked Jan 13 '16 15:01

BlackCursor


People also ask

What is MVP pattern in Android?

MVP (Model — View — Presenter) architecture is one of the most popular architecture patterns and is valid in organizing the project. MVP (Model — View — Presenter) comes into the picture as an alternative to the traditional MVC (Model — View — Controller) architecture pattern.

What is MVP architecture pattern?

MVP is a user interface architectural pattern engineered to facilitate automated unit testing and improve the separation of concerns in presentation logic: The model is an interface defining the data to be displayed or otherwise acted upon in the user interface.

What is MVP in Kotlin?

by Antonio Leiva | Blog, Development, Kotlin | 174 comments. MVP (Model View Presenter) pattern is a derivative from the well known MVC (Model View Controller), and one of the most popular patterns to organize the presentation layer in Android Applications.


1 Answers

Using dagger makes it easier to inject the Interactor on your Presenter. Try this link (https://github.com/spengilley/AndroidMVPService)

I'm trying to achieve it without dagger. But this seems violates the MVP architecture.

From the Activity, I created an instance of Interactor. Then create instance of Presenter with the Interactor as one of the parameter.

Activity

public class SomeActivity extends Activity implements SomeView {
   private SomePresenter presenter;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      SomeInteractor interactor = new SomeInteractorImpl(SomeActivity.this);
      presenter = new SomePresenterImpl(interactor,this);
   }

   @Override
   protected void onStart() {
     super.onStart();
     presenter.startServiceFunction();
   }

Presenter

public interface SomePresenter {
   public void startServiceFunction();
}

Presenter Implementation

public class SomePresenterImpl implements SomePresenter {
   private SomeInteractor interactor;
   private SomeView view;
   public SomePresenterImpl(SomeInteractor interactor,SomeView view){
      this.interactor = interactor;
      this.view = view;
   }
   @Override
   public void startServiceFunction() {
      interactor.startServiceFunction();
   }
}

Interactor

public interface SomeInteractor {
   public void startServiceFunction();
}

Interactor Implementation

public class SomeInteractorImpl implements SomeInteractor {
   private Context context;

   public SomeInteractorImpl(Context context) {
      this.context = context;
   }

   @Override
   public void startServiceFunction() {
      Intent intent = new Intent(context, SomeService.class);
      context.startService(intent);
   }
}
like image 199
codebringer Avatar answered Sep 28 '22 10:09

codebringer