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?
Aucun commentaire:
Enregistrer un commentaire