stephanenicolas / robospice

Repo of the Open Source Android library : RoboSpice. RoboSpice is a modular android library that makes writing asynchronous long running tasks easy. It is specialized in network requests, supports caching and offers REST requests out-of-the box using extension modules.
Apache License 2.0
2.95k stars 545 forks source link

Best Practice for design to connect to multiple services #462

Open eternalBlast opened 7 years ago

eternalBlast commented 7 years ago

Hi @stephanenicolas this is a superb library. I want to have two service. Can you give me a guide? FYI, my whole activities extend BaseActivity (or below class). If i implement an interface to this class i must call the method on my each sub class activities.

public abstract class BaseActivity extends AppCompatActivity {
  private static final String TAG = BaseActivity.class.getSimpleName();

  private SpiceManager spiceManager;

  public SpiceManager getSpiceManager() {
    if(PrefUtils.getPrefCity(getBaseContext()).equals("ABC"))
      spiceManager = new SpiceManager(MySecondService.class);
    else
      spiceManager = new SpiceManager(MyFirstService.class);
    return spiceManager;
  }

  @Override
  protected void onStart() {
    super.onStart();
    spiceManager.start(this);
  }

  @Override
  protected void onStop() {
    spiceManager.shouldStop();
    super.onStop();
  }

}

Before i change to the above class, here is my BaseActivity class

public abstract class BaseActivity extends AppCompatActivity {
  private static final String TAG = BaseActivity.class.getSimpleName();

  private SpiceManager spiceManager = new SpiceManager(MyFirstService.class);

  public SpiceManager getSpiceManager() {
    return spiceManager;
  }

  @Override
  protected void onStart() {
    super.onStart();
    spiceManager.start(this);
  }

  @Override
  protected void onStop() {
    spiceManager.shouldStop();
    super.onStop();
  }

}

Is there any way that give best practices design a spicemanager connect two service? Or i must create two spice manager? Or is there any way i just check using if statement when getServerUrl() method is executed? Please help. Thanks in advanced.