Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put business logic when working with mvvm

I am making a user login screen using MVVM design pattern but I am stuck when its come to implement the logic for phone number validation. I read out Rules to follow when working with mvvm (Rule no. 4) that View should not have any logic in it, Not even a simple if condition. All logic for the view happens in ViewModel.

Here is my ViewModel class.

public class LoginViewModel extends AndroidViewModel {

    private LoginRepository loginRepository;
    private HashMap<String,String> mNumberParam;
    private MutableLiveData<Boolean> isValidated;

    public LoginViewModel(@NonNull Application application) {
        super(application);
        loginRepository=LoginRepository.getInstance();
        isValidated=new MutableLiveData<>();
    }


    public LiveData<List<OtpEnterModel.Data>> enterNumberApiHit(){
        return loginRepository.enterNumberApiHit(mNumberParam);
    }


    public void onSubmitClick(String number){

        //if mobile number not enter or wrong enter show message ,and tell the view to hide other view
        if (number==null) {
            Toast.makeText(getApplication(), "Invalid mobile number", Toast.LENGTH_SHORT).show();
            isValidated.setValue(false);

        } else {
            //otherwise save mobile number in hashMap ,and tell the view to work further
            isValidated.setValue(true);
            saveNumberParam(number);
        }
    }

    //to save the mobile number in hashMap with key i.e mobile_number.
    private void saveNumberParam(String mobileNumber) {

        //if hashMap null then initialize it
        if (mNumberParam ==null) {
            mNumberParam = new HashMap<>();
        }
        mNumberParam.put(MyConstant.MOBILE_NUMBER, mobileNumber);
    }

    public LiveData<Boolean> isValidated(){
      return isValidated;
    }
}

Here is my View class.

public class EnterNumber extends AppCompatActivity implements View.OnClickListener, FragmentManager.OnBackStackChangedListener {

    //dataType
    private Context context;
    private FragmentManager manager;
    private LoginViewModel loginViewModel;

    //views
    private EditText enterMobileEDT;
    private ProgressBar progressBar;
    private Button btnNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enter_number);

        manager=getSupportFragmentManager();
        context = EnterNumber.this;
        loginViewModel= ViewModelProviders.of(this).get(LoginViewModel.class);

        init();
        setListener();
    }

    //all views initialize here
    private void init() {
        enterMobileEDT = findViewById(R.id.enterMobileET);
        progressBar=findViewById(R.id.progressBar);
        btnNumber=findViewById(R.id.btn_number);
    }

    //listener for views
    private void setListener() {
        btnNumber.setOnClickListener(this);
        manager.addOnBackStackChangedListener(this);
    }

    //check for mobile number and send otp by hitting API
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_number) {
            loginViewModel.onSubmitClick(enterMobileEDT.getText().toString());
            numberValidation();
        }
    }

    //check for editText length
    public void numberValidation() {

        loginViewModel.isValidated().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {

                if(aBoolean){
                    loginApiHit();
                }
                hideShowView(aBoolean);
            }
        });
    }

    //hide and show the view based on condition
    public void hideShowView(boolean wantToShowProgress) {

        if(!wantToShowProgress){
            progressBar.setVisibility(View.INVISIBLE);
            btnNumber.setEnabled(true);

        }else {
            progressBar.setVisibility(View.VISIBLE);
            btnNumber.setEnabled(false);
        }
    }

}

How Can I move all if/else condition from View to ViewModel?

like image 397
Lokik Soni Avatar asked Sep 17 '19 02:09

Lokik Soni


1 Answers

Question

How Can I move all if/else condition from View to ViewModel?

  • It is recommended that you remove all business logic in the View.
  • View only has code for updates UI that coupled ViewModel data(LiveData), which can be reduced via ViewDataBininding library.
  • Finally, View just has code related to setup ViewDataBinding and ViewModel.

ViewModel

public class LoginViewModel extends AndroidViewModel {
    ...
    private MutableLiveData<String> _email = new MutableLiveData<>(); // is binded some UI such as EditText..

    LiveData<Boolean> emailValidate = Transformations.map(_email, this::emailValidate);

    private boolean emailValidate(String email) {
        return true; // implements email validation logic
    }
    ...
}

View

...
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    LoginViewModel loginViewModel= ViewModelProviders.of(this).get(LoginViewModel.class);
    subscribe(loginViewModel);
}

private void subscribe(LoginViewModel loginViewModel) {
    loginViewModel.emailValidate.observe(this, this::setEmailValidateLayout); 
    // You shouldn't implement observing in the onClick event. Overlapping observers problem.
}

private void setEmailValidateLayout(boolean validate) {
    progressBar.setVisibility(validate ? View.VISIBLE : View.INVISIBLE);
    btnNumber.setEnabled(validate);
}
...
like image 129
Ethan Choi Avatar answered Oct 10 '22 00:10

Ethan Choi