0

I am learning MVP design for the Android , I am new to it so need your valuable time . I went to the basics of the MVP how it work , Now i got stuck so need your help , I have to post the data to the server , when i hard code the value in presenter then i get the response correct but i need the data that is in the LoginActivity view when user press then that value should pass to the presenter and presenter pass that value to the Retrofit and bring back the result . Here is my try :

My LoginActvity:

public class LoginActivity extends BaseActivity implements LoginView {

    @BindView(R.id.company_name)
    protected EditText companyName_et;

    @BindView(R.id.email)
    protected EditText email_et;

    @BindView(R.id.password)
    protected EditText password_et;

    @BindView(R.id.submit)
    protected Button submit_btn;

    @Inject
    LoginPresenter loginPresenter;


    @Override
    protected int getContentView() {
        return R.layout.login_activity;
    }

    @Override
    protected void onViewReady(Bundle savedInstanceState, Intent intent) {
        super.onViewReady(savedInstanceState, intent);
        submit_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String company=companyName_et.getText().toString();
                String username=email_et.getText().toString();
                String password=password_et.getText().toString();

                /*
                    I have to put the above String data
                    to my model class then i have to post the data to the server
                 */
                loginPresenter.passLoginDataToServer();
            }
        });

    }

    @Override
    public void onError(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSuccess(String s) {

    }

    @Override
    public void onResponse(Login login) {
        Log.e("-----",""+login.getUserId());
    }

    @Override
    protected void resolveDaggerDependency() {
        DaggerLoginComponent.builder().applicationComponent(getApplicationComponent()).loginModule(new LoginModule(this)).build().inject(this);
    }
}

Here is Login Presenter :

public class LoginPresenter extends BasePresenter<LoginView>  implements Observer<Login>{

    @Inject
    CreateApiService createApiService;

    @Inject
    public LoginPresenter(){};

    public void  passLoginDataToServer(){
       /*
        when i hard code the data , i get the successful response.Like :
          String user="raj";
          String check="true";
          Map<String,String> headers=new HashMap();
          headers,put("xyz","pqr");
          Login loginObject = new Login("xyzs", "pqr","Qtch","mvp");
        */
       /*
       But I need the data here from my LoginActivity ? Dunno how to pass the data from LoginActivity to presenter
        */

       Observable<Login> loginObservable=createApiService.loginUser(user, check, headers, loginObject);
       subscribeToLogin(loginObservable,this);
   }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(Login login) {
       getmView().onResponse(login);
    }

    @Override
    public void onError(Throwable e) {
        getmView().onError("Error "+e);
        Log.e("---",""+e);
    }

    @Override
    public void onComplete() {
        getmView().onSuccess("Successfully Loaded");
    }
}

Here is My Interface :

public interface LoginView extends BaseView {

    void onError(String s);

    void onSuccess(String s);

    void onResponse(Login login);
}
Barnali Bhattacharjee
  • 497
  • 2
  • 10
  • 24
  • What is happening when you try to pass the parameters via the method? Is the app crashing or do you have logs? – Caleb Feb 28 '18 at 22:00
  • actually i am lost after reading the documentation , i thought i have to pass the data from the interface to presenter . My mind always stuck there ,can't able to figure out i can pass the value in method to . – Barnali Bhattacharjee Feb 28 '18 at 22:05

1 Answers1

0

Could you add the parameters to the method?

In your activity:

 @Override public void onClick(View view) {
                String company=companyName_et.getText().toString();
                String username=email_et.getText().toString();
                String password=password_et.getText().toString();

            loginPresenter.passLoginDataToServer(compay, username, password);
}

In your presenter:

    public void  passLoginDataToServer(String company, String username, String password){

       // Now create your request from the dynamic parameters

       Observable<Login> loginObservable=createApiService.loginUser(username, check, headers, loginObject);
       subscribeToLogin(loginObservable,this);
   }
Caleb
  • 1,666
  • 13
  • 13
  • oh my god !! yeah right so silly me , is it the right way to do because i am still learning MVP ? – Barnali Bhattacharjee Feb 28 '18 at 22:03
  • 1
    Yeah, it's one way to do it. It's fine to pass values to the presenter but try not to pass Android specific objects (like Context) unless you have a good reason. Try to keep the presenter just for application logic. Of course there are slightly different ways of doing the MVP pattern. I'm sure someone will object. :D – Caleb Feb 28 '18 at 22:09
  • I need to ask , i will not wrap that string into my model class in the LoginActivity and then send to the presenter right. I have to make the object in the presenter itself and then send it to server ,i am right? – Barnali Bhattacharjee Feb 28 '18 at 22:09
  • You could wrap it in the Activity or in the Presenter. It would technically work either way. I would probably do it in the presenter but I haven't given it much thought yet. – Caleb Feb 28 '18 at 22:13
  • ok thanks for the help , i think it should wrap on presenter because in mvp , the view should not directly talk to the model . it's presenter who cares about the data coming from the view should go to model class or when model class receive any update then it go to the presenter and presenter send data to activity. – Barnali Bhattacharjee Feb 28 '18 at 22:20