为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

温温酱 2023-08-04 19:02:15
我正在使用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;    }}
查看完整描述

1 回答

?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

问题

如何将所有 if/else 条件从 View 移至 ViewModel?

  • 建议您删除View中的所有业务逻辑

  • View只有用于更新 UI 的代码,该代码耦合了ViewModel数据(LiveData),可以通过ViewDataBininding库来减少。

  • 最后,View只有与 setupViewDataBindingViewModel.

视图模型

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

    }

    ...

}

看法

...

@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);

}

...


查看完整回答
反对 回复 2023-08-04
  • 1 回答
  • 0 关注
  • 195 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号