package com.application.zhangshi_app_android.ui.function; import static com.android.app_base.base.BaseConfig.CODE_SUCCESS; import android.app.Application; import androidx.annotation.NonNull; import androidx.databinding.Bindable; import androidx.databinding.InverseBindingAdapter; import androidx.lifecycle.MutableLiveData; import com.android.app_base.base.StateViewEnum; import com.android.app_base.base.viewmodel.BaseViewModel; import com.android.app_base.http.ResultData; import com.android.app_base.utils.RxUtils; import com.application.zhangshi_app_android.bean.HealthCareBaseInfoBean; import com.application.zhangshi_app_android.bean.HealthCareExistingProblemsBean; import com.application.zhangshi_app_android.bean.HealthCareLifeHabitsBean; import com.application.zhangshi_app_android.bean.HealthCareNotesContentBean; import com.application.zhangshi_app_android.bean.HealthCareNursingExaminationBean; import com.application.zhangshi_app_android.bean.HomeDevicesBean; import com.application.zhangshi_app_android.bean.PageResponseBean; import com.application.zhangshi_app_android.data.DataRepository; import io.reactivex.Observable; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import io.reactivex.functions.Function5; /** * @author Ljj * @date 2023.04.28. 13:53 * @desc 健康保健 ViewModel */ public class HealthCareActivityViewModel extends BaseViewModel { private MutableLiveData typeLiveData;//true:编辑 false:查看 private MutableLiveData healthCareBaseInfoBeanLiveData;//健康保健基本信息 private MutableLiveData healthCareLifeHabitsBeanLiveData;//健康保健生活习惯 private MutableLiveData healthCareNursingExaminationBeanLiveData;//健康保健护理检查 private MutableLiveData healthCareExistingProblemsBeanLiveData;//健康保健现存问题 private MutableLiveData healthCareNotesContentBeanLiveData;//健康保健笔记内容 private MutableLiveData isLifeHabitsExpendedLiveData; private MutableLiveData isNursingExpendedLiveData; private MutableLiveData isHealthExpendedLiveData; public HealthCareActivityViewModel(@NonNull Application application) { super(application); } @Override protected DataRepository initModel() { return DataRepository.getInstance(); } public Observer> getObserver(MutableLiveData resultData,T newBean) { return new Observer>() { @Override public void onSubscribe(Disposable d) { addSubscribe(d); } @Override public void onNext(ResultData data) { if (data.getCode() == CODE_SUCCESS) { if (data.getData() == null) { changeStateView(StateViewEnum.DATA_NULL); } else { changeStateView(StateViewEnum.HIDE); resultData.postValue(data.getData()); } } else if (data.getCode() == 500) {//用户还没有填写健康保健信息,直接给个新的对象展示供用户修改 resultData.postValue(newBean); } else { messageLiveData.postValue(data.getMsg()); } } @Override public void onError(Throwable e) { messageLiveData.postValue(e.getMessage()); } @Override public void onComplete() { } }; } /** * 获取健康保健基本信息 */ public void getHealthCareBaseInfo() { model.getHealthCareBaseInfo() .compose(RxUtils.schedulersTransformer()) .doOnSubscribe(disposable -> changeStateView(StateViewEnum.DATA_LOADING)) .doFinally(() -> changeStateView(StateViewEnum.DATA_FINISH)) .subscribe(getObserver(getHealthCareBaseInfoBeanLiveData(),new HealthCareBaseInfoBean())); } /** * 获取健康保健生活习惯 */ public void getHealthCareLifeHabits() { model.getHealthCareLifeHabits() .compose(RxUtils.schedulersTransformer()) .doOnSubscribe(disposable -> changeStateView(StateViewEnum.DATA_LOADING)) .doFinally(() -> changeStateView(StateViewEnum.DATA_FINISH)) .subscribe(getObserver(getHealthCareLifeHabitsBeanLiveData(),new HealthCareLifeHabitsBean())); } /** * 获取健康保健护理查体 */ public void getHealthCareNursingExamination() { model.getHealthCareNursingExamination() .compose(RxUtils.schedulersTransformer()) .doOnSubscribe(disposable -> changeStateView(StateViewEnum.DATA_LOADING)) .doFinally(() -> changeStateView(StateViewEnum.DATA_FINISH)) .subscribe(getObserver(getHealthCareNursingExaminationBeanLiveData(),new HealthCareNursingExaminationBean())); } /** * 获取健康保健现存问题 */ public void getHealthCareExistingProblems() { model.getHealthCareExistingProblems() .compose(RxUtils.schedulersTransformer()) .doOnSubscribe(disposable -> changeStateView(StateViewEnum.DATA_LOADING)) .doFinally(() -> changeStateView(StateViewEnum.DATA_FINISH)) .subscribe(getObserver(getHealthCareExistingProblemsBeanLiveData(),new HealthCareExistingProblemsBean())); } /** * 获取健康保健笔记内容 */ public void getHealthCareNotesContent() { model.getHealthCareNotesContent() .compose(RxUtils.schedulersTransformer()) .doOnSubscribe(disposable -> changeStateView(StateViewEnum.DATA_LOADING)) .doFinally(() -> changeStateView(StateViewEnum.DATA_FINISH)) .subscribe(getObserver(getHealthCareNotesContentBeanLiveData(),new HealthCareNotesContentBean())); } /** * 修改保存 */ public void save(){ Observable.zip( model.updateHealthCareBaseInfo(getHealthCareBaseInfoBeanLiveData().getValue()), model.updateHealthCareLifeHabits(getHealthCareLifeHabitsBeanLiveData().getValue()), model.updateHealthCareNursingExamination(getHealthCareNursingExaminationBeanLiveData().getValue()), model.updateHealthCareExistingProblems(getHealthCareExistingProblemsBeanLiveData().getValue()), model.updateHealthCareNotesContent(getHealthCareNotesContentBeanLiveData().getValue()), new Function5, ResultData, ResultData, ResultData, ResultData, String>() { @Override public String apply(ResultData stringResultData, ResultData stringResultData2, ResultData stringResultData3, ResultData stringResultData4, ResultData stringResultData5) throws Exception { if (stringResultData.getCode() == CODE_SUCCESS && stringResultData2.getCode() == CODE_SUCCESS && stringResultData3.getCode() == CODE_SUCCESS && stringResultData4.getCode() == CODE_SUCCESS && stringResultData5.getCode() == CODE_SUCCESS) { return "保存成功"; } else { throw new Exception("保存失败"); } } } ).compose(RxUtils.schedulersTransformer()).subscribe(new Observer() { @Override public void onSubscribe(Disposable d) { addSubscribe(d); changeStateView(StateViewEnum.DATA_LOADING); } @Override public void onNext(String s) { messageLiveData.postValue(s); } @Override public void onError(Throwable e) { messageLiveData.postValue(e.getMessage()); } @Override public void onComplete() { changeStateView(StateViewEnum.DATA_FINISH); } }); } public MutableLiveData getIsLifeHabitsExpendedLiveData() { if (isLifeHabitsExpendedLiveData == null){ isLifeHabitsExpendedLiveData = new MutableLiveData<>(); isLifeHabitsExpendedLiveData.setValue(false); } return isLifeHabitsExpendedLiveData; } public void setIsLifeHabitsExpendedLiveData(MutableLiveData isLifeHabitsExpendedLiveData) { this.isLifeHabitsExpendedLiveData = isLifeHabitsExpendedLiveData; } public MutableLiveData getIsNursingExpendedLiveData() { if (isNursingExpendedLiveData == null){ isNursingExpendedLiveData = new MutableLiveData<>(); isNursingExpendedLiveData.setValue(false); } return isNursingExpendedLiveData; } public void setIsNursingExpendedLiveData(MutableLiveData isNursingExpendedLiveData) { this.isNursingExpendedLiveData = isNursingExpendedLiveData; } public MutableLiveData getIsHealthExpendedLiveData() { if (isHealthExpendedLiveData == null){ isHealthExpendedLiveData = new MutableLiveData<>(); isHealthExpendedLiveData.setValue(false); } return isHealthExpendedLiveData; } public void setIsHealthExpendedLiveData(MutableLiveData isHealthExpendedLiveData) { this.isHealthExpendedLiveData = isHealthExpendedLiveData; } public MutableLiveData getHealthCareBaseInfoBeanLiveData() { if (healthCareBaseInfoBeanLiveData == null){ healthCareBaseInfoBeanLiveData = new MutableLiveData<>(); } return healthCareBaseInfoBeanLiveData; } public void setHealthCareBaseInfoBeanLiveData(MutableLiveData healthCareBaseInfoBeanLiveData) { this.healthCareBaseInfoBeanLiveData = healthCareBaseInfoBeanLiveData; } public MutableLiveData getHealthCareLifeHabitsBeanLiveData() { if (healthCareLifeHabitsBeanLiveData == null){ healthCareLifeHabitsBeanLiveData = new MutableLiveData<>(); } return healthCareLifeHabitsBeanLiveData; } public void setHealthCareLifeHabitsBeanLiveData(MutableLiveData healthCareLifeHabitsBeanLiveData) { this.healthCareLifeHabitsBeanLiveData = healthCareLifeHabitsBeanLiveData; } public MutableLiveData getHealthCareNursingExaminationBeanLiveData() { if (healthCareNursingExaminationBeanLiveData == null){ healthCareNursingExaminationBeanLiveData = new MutableLiveData<>(); } return healthCareNursingExaminationBeanLiveData; } public void setHealthCareNursingExaminationBeanLiveData(MutableLiveData healthCareNursingExaminationBeanLiveData) { this.healthCareNursingExaminationBeanLiveData = healthCareNursingExaminationBeanLiveData; } public MutableLiveData getHealthCareExistingProblemsBeanLiveData() { if (healthCareExistingProblemsBeanLiveData == null){ healthCareExistingProblemsBeanLiveData = new MutableLiveData<>(); } return healthCareExistingProblemsBeanLiveData; } public void setHealthCareExistingProblemsBeanLiveData(MutableLiveData healthCareExistingProblemsBeanLiveData) { this.healthCareExistingProblemsBeanLiveData = healthCareExistingProblemsBeanLiveData; } public MutableLiveData getHealthCareNotesContentBeanLiveData() { if (healthCareNotesContentBeanLiveData == null){ healthCareNotesContentBeanLiveData = new MutableLiveData<>(); } return healthCareNotesContentBeanLiveData; } public void setHealthCareNotesContentBeanLiveData(MutableLiveData healthCareNotesContentBeanLiveData) { this.healthCareNotesContentBeanLiveData = healthCareNotesContentBeanLiveData; } public MutableLiveData getTypeLiveData() { if (typeLiveData == null){ typeLiveData = new MutableLiveData<>(); typeLiveData.setValue(false); } return typeLiveData; } public void setTypeLiveData(MutableLiveData typeLiveData) { this.typeLiveData = typeLiveData; } public void setTypeLiveData(boolean type) { if (typeLiveData == null){ typeLiveData = new MutableLiveData<>(); } typeLiveData.setValue(type); } }