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<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) {
|
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 {
|
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()));
|
}
|
|
/**
|
* 获取健康保健生活习惯
|
*/
|
public void getHealthCareLifeHabits() {
|
model.getHealthCareLifeHabits()
|
.compose(RxUtils.schedulersTransformer())
|
.doOnSubscribe(disposable -> changeStateView(StateViewEnum.DATA_LOADING))
|
.doFinally(() -> changeStateView(StateViewEnum.DATA_FINISH))
|
.subscribe(getObserver(getHealthCareLifeHabitsBeanLiveData()));
|
}
|
|
/**
|
* 获取健康保健护理查体
|
*/
|
public void getHealthCareNursingExamination() {
|
model.getHealthCareNursingExamination()
|
.compose(RxUtils.schedulersTransformer())
|
.doOnSubscribe(disposable -> changeStateView(StateViewEnum.DATA_LOADING))
|
.doFinally(() -> changeStateView(StateViewEnum.DATA_FINISH))
|
.subscribe(getObserver(getHealthCareNursingExaminationBeanLiveData()));
|
}
|
|
/**
|
* 获取健康保健现存问题
|
*/
|
public void getHealthCareExistingProblems() {
|
model.getHealthCareExistingProblems()
|
.compose(RxUtils.schedulersTransformer())
|
.doOnSubscribe(disposable -> changeStateView(StateViewEnum.DATA_LOADING))
|
.doFinally(() -> changeStateView(StateViewEnum.DATA_FINISH))
|
.subscribe(getObserver(getHealthCareExistingProblemsBeanLiveData()));
|
}
|
|
/**
|
* 获取健康保健笔记内容
|
*/
|
public void getHealthCareNotesContent() {
|
model.getHealthCareNotesContent()
|
.compose(RxUtils.schedulersTransformer())
|
.doOnSubscribe(disposable -> changeStateView(StateViewEnum.DATA_LOADING))
|
.doFinally(() -> changeStateView(StateViewEnum.DATA_FINISH))
|
.subscribe(getObserver(getHealthCareNotesContentBeanLiveData()));
|
}
|
|
/**
|
* 修改保存
|
*/
|
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() {
|
if (isLifeHabitsExpendedLiveData == null){
|
isLifeHabitsExpendedLiveData = new MutableLiveData<>();
|
isLifeHabitsExpendedLiveData.setValue(false);
|
}
|
return isLifeHabitsExpendedLiveData;
|
}
|
|
public void setIsLifeHabitsExpendedLiveData(MutableLiveData<Boolean> isLifeHabitsExpendedLiveData) {
|
this.isLifeHabitsExpendedLiveData = isLifeHabitsExpendedLiveData;
|
}
|
|
public MutableLiveData<Boolean> getIsNursingExpendedLiveData() {
|
if (isNursingExpendedLiveData == null){
|
isNursingExpendedLiveData = new MutableLiveData<>();
|
isNursingExpendedLiveData.setValue(false);
|
}
|
return isNursingExpendedLiveData;
|
}
|
|
public void setIsNursingExpendedLiveData(MutableLiveData<Boolean> isNursingExpendedLiveData) {
|
this.isNursingExpendedLiveData = isNursingExpendedLiveData;
|
}
|
|
public MutableLiveData<Boolean> getIsHealthExpendedLiveData() {
|
if (isHealthExpendedLiveData == null){
|
isHealthExpendedLiveData = new MutableLiveData<>();
|
isHealthExpendedLiveData.setValue(false);
|
}
|
return isHealthExpendedLiveData;
|
}
|
|
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);
|
}
|
}
|