亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號