11

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

BlackCursor
  • 1,492
  • 1
  • 17
  • 29

2 Answers2

7

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);
   }
}
AutonomousApps
  • 4,229
  • 4
  • 32
  • 42
codebringer
  • 411
  • 3
  • 7
  • 3
    I don't think the answer is right. The interactor should not know anything about specific frameworks and should be Android agnostic if you follow the guidelines from the Clean Architecture [link](https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html). Therefore it should be completely free of anything related to Android including the Context class. – koufa Jun 27 '16 at 10:43
  • @koufa you have to put the `Context` _somewhere_. How else will you interact with a `Service`? What would you recommend? – AutonomousApps Sep 26 '16 at 14:36
  • 2
    @AutonomousApps If you divide your app in layers then the Interactors would reside in the Domain Layer. This Layer should not have any framework specific dependencies that means no Android dependency. The `Service` initiation should go to the DataLayer where you wrap this code `Intent intent = new Intent(context, SomeService.class); context.startService(intent);` in a class that implements an interface from the Domain Layer. Then the Interactor will only know about this interface and not about the concrete implementation of it. – koufa Sep 29 '16 at 06:42
0

I am also searching for your first question. However, I have the answer of the second question.

The answer is Dagger2. (http://google.github.io/dagger/) You can easily inject GoogleApiClient object by using Dagger2.

Efe Budak
  • 659
  • 5
  • 27
  • 7
    How do you handle the callbacks `GoogleApiClient.ConnectionCallbacks` and `GoogleApiClient.OnConnectionFailedListener` when using DI? An example would be helpful. – sidecarcat Mar 12 '16 at 17:33