| | |
| | | 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 |
| | |
| | | */ |
| | | public class HealthCareActivityViewModel extends BaseViewModel<DataRepository> { |
| | | |
| | | private MutableLiveData<Boolean> typeLiveData;//true:编辑 false:查看 |
| | | private MutableLiveData<HealthCareBaseInfoBean> healthCareBaseInfoBeanLiveData;//健康保健基本信息 |
| | | private MutableLiveData<HealthCareLifeHabitsBean> healthCareLifeHabitsBeanLiveData;//健康保健生活习惯 |
| | | private MutableLiveData<HealthCareNursingExaminationBean> healthCareNursingExaminationBeanLiveData;//健康保健护理检查 |
| | | private MutableLiveData<HealthCareExistingProblemsBean> healthCareExistingProblemsBeanLiveData;//健康保健现存问题 |
| | | private MutableLiveData<HealthCareNotesContentBean> healthCareNotesContentBeanLiveData;//健康保健笔记内容 |
| | | private MutableLiveData<Boolean> isLifeHabitsExpendedLiveData; |
| | | private MutableLiveData<Boolean> isNursingExpendedLiveData; |
| | | private MutableLiveData<Boolean> isHealthExpendedLiveData; |
| | | |
| | | |
| | | |
| | | public HealthCareActivityViewModel(@NonNull Application application) { |
| | | super(application); |
| | |
| | | @Override |
| | | protected DataRepository initModel() { |
| | | return DataRepository.getInstance(); |
| | | } |
| | | |
| | | public <T> Observer<ResultData<T>> getObserver(MutableLiveData<T> resultData,T newBean) { |
| | | return new Observer<ResultData<T>>() { |
| | | @Override |
| | | public void onSubscribe(Disposable d) { |
| | | addSubscribe(d); |
| | | } |
| | | |
| | | @Override |
| | | public void onNext(ResultData<T> 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<String>, ResultData<String>, ResultData<String>, ResultData<String>, ResultData<String>, String>() { |
| | | @Override |
| | | public String apply(ResultData<String> stringResultData, ResultData<String> stringResultData2, ResultData<String> stringResultData3, ResultData<String> stringResultData4, ResultData<String> 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<String>() { |
| | | @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<Boolean> getIsLifeHabitsExpendedLiveData() { |
| | |
| | | public void setIsHealthExpendedLiveData(MutableLiveData<Boolean> isHealthExpendedLiveData) { |
| | | this.isHealthExpendedLiveData = isHealthExpendedLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<HealthCareBaseInfoBean> getHealthCareBaseInfoBeanLiveData() { |
| | | if (healthCareBaseInfoBeanLiveData == null){ |
| | | healthCareBaseInfoBeanLiveData = new MutableLiveData<>(); |
| | | } |
| | | return healthCareBaseInfoBeanLiveData; |
| | | } |
| | | |
| | | public void setHealthCareBaseInfoBeanLiveData(MutableLiveData<HealthCareBaseInfoBean> healthCareBaseInfoBeanLiveData) { |
| | | this.healthCareBaseInfoBeanLiveData = healthCareBaseInfoBeanLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<HealthCareLifeHabitsBean> getHealthCareLifeHabitsBeanLiveData() { |
| | | if (healthCareLifeHabitsBeanLiveData == null){ |
| | | healthCareLifeHabitsBeanLiveData = new MutableLiveData<>(); |
| | | } |
| | | return healthCareLifeHabitsBeanLiveData; |
| | | } |
| | | |
| | | public void setHealthCareLifeHabitsBeanLiveData(MutableLiveData<HealthCareLifeHabitsBean> healthCareLifeHabitsBeanLiveData) { |
| | | this.healthCareLifeHabitsBeanLiveData = healthCareLifeHabitsBeanLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<HealthCareNursingExaminationBean> getHealthCareNursingExaminationBeanLiveData() { |
| | | if (healthCareNursingExaminationBeanLiveData == null){ |
| | | healthCareNursingExaminationBeanLiveData = new MutableLiveData<>(); |
| | | } |
| | | return healthCareNursingExaminationBeanLiveData; |
| | | } |
| | | |
| | | public void setHealthCareNursingExaminationBeanLiveData(MutableLiveData<HealthCareNursingExaminationBean> healthCareNursingExaminationBeanLiveData) { |
| | | this.healthCareNursingExaminationBeanLiveData = healthCareNursingExaminationBeanLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<HealthCareExistingProblemsBean> getHealthCareExistingProblemsBeanLiveData() { |
| | | if (healthCareExistingProblemsBeanLiveData == null){ |
| | | healthCareExistingProblemsBeanLiveData = new MutableLiveData<>(); |
| | | } |
| | | return healthCareExistingProblemsBeanLiveData; |
| | | } |
| | | |
| | | public void setHealthCareExistingProblemsBeanLiveData(MutableLiveData<HealthCareExistingProblemsBean> healthCareExistingProblemsBeanLiveData) { |
| | | this.healthCareExistingProblemsBeanLiveData = healthCareExistingProblemsBeanLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<HealthCareNotesContentBean> getHealthCareNotesContentBeanLiveData() { |
| | | if (healthCareNotesContentBeanLiveData == null){ |
| | | healthCareNotesContentBeanLiveData = new MutableLiveData<>(); |
| | | } |
| | | return healthCareNotesContentBeanLiveData; |
| | | } |
| | | |
| | | public void setHealthCareNotesContentBeanLiveData(MutableLiveData<HealthCareNotesContentBean> healthCareNotesContentBeanLiveData) { |
| | | this.healthCareNotesContentBeanLiveData = healthCareNotesContentBeanLiveData; |
| | | } |
| | | |
| | | public MutableLiveData<Boolean> getTypeLiveData() { |
| | | if (typeLiveData == null){ |
| | | typeLiveData = new MutableLiveData<>(); |
| | | typeLiveData.setValue(false); |
| | | } |
| | | return typeLiveData; |
| | | } |
| | | |
| | | public void setTypeLiveData(MutableLiveData<Boolean> typeLiveData) { |
| | | this.typeLiveData = typeLiveData; |
| | | } |
| | | |
| | | public void setTypeLiveData(boolean type) { |
| | | if (typeLiveData == null){ |
| | | typeLiveData = new MutableLiveData<>(); |
| | | } |
| | | typeLiveData.setValue(type); |
| | | } |
| | | } |