我正在使用MVVM设计模式制作用户登录界面,但在实现电话号码验证的逻辑时遇到了困难。我阅读了在使用MVVM时需要遵循的规则(第4条规则),即视图不应该有任何逻辑,甚至没有简单的if条件语句。所有与视图相关的逻辑都发生在ViewModel中。
以下是我的ViewModel类。
以下是我的ViewModel类。
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;
}
}
这是我的View类。
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);
}
}
}
如何将所有if/else条件从View移动到ViewModel中?