Linjiajia
2023-10-07 31b7700b976a46901f67c5d7a00281ca4745fc9f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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,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() {
        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);
    }
}