使用 mvvm 时将业务逻辑放在哪里

我正在使用MVVM 设计模式制作用户登录屏幕,但当它实现电话号码验证逻辑时,我陷入了困境。我读出了使用 mvvm 时要遵循的规则 (规则 4),即视图中不应该有任何逻辑,甚至不应该是简单的 if 条件。视图的所有逻辑都发生在 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;

    }

}


温温酱
浏览 197回答 1
1回答

天涯尽头无女友

问题如何将所有 if/else 条件从 View 移至 ViewModel?建议您删除View中的所有业务逻辑。View只有用于更新 UI 的代码,该代码耦合了ViewModel数据(LiveData),可以通过ViewDataBininding库来减少。最后,View只有与 setupViewDataBinding和ViewModel.视图模型public class LoginViewModel extends AndroidViewModel {&nbsp; &nbsp; ...&nbsp; &nbsp; private MutableLiveData<String> _email = new MutableLiveData<>(); // is binded some UI such as EditText..&nbsp; &nbsp; LiveData<Boolean> emailValidate = Transformations.map(_email, this::emailValidate);&nbsp; &nbsp; private boolean emailValidate(String email) {&nbsp; &nbsp; &nbsp; &nbsp; return true; // implements email validation logic&nbsp; &nbsp; }&nbsp; &nbsp; ...}看法...@Overridepublic void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {&nbsp; &nbsp; super.onCreate(savedInstanceState, persistentState);&nbsp; &nbsp; LoginViewModel loginViewModel= ViewModelProviders.of(this).get(LoginViewModel.class);&nbsp; &nbsp; subscribe(loginViewModel);}private void subscribe(LoginViewModel loginViewModel) {&nbsp; &nbsp; loginViewModel.emailValidate.observe(this, this::setEmailValidateLayout);&nbsp;&nbsp; &nbsp; // You shouldn't implement observing in the onClick event. Overlapping observers problem.}private void setEmailValidateLayout(boolean validate) {&nbsp; &nbsp; progressBar.setVisibility(validate ? View.VISIBLE : View.INVISIBLE);&nbsp; &nbsp; btnNumber.setEnabled(validate);}...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java