From 82e57df230ecb744af6c8865f80870ba03c86d89 Mon Sep 17 00:00:00 2001
From: Linjiajia <319408893@qq.com>
Date: 星期二, 25 七月 2023 22:18:27 +0800
Subject: [PATCH] 基本功能完成

---
 app/src/main/java/com/application/zhangshi_app_android/ui/function/HealthCareActivityViewModel.java |  237 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 235 insertions(+), 2 deletions(-)

diff --git a/app/src/main/java/com/application/zhangshi_app_android/ui/function/HealthCareActivityViewModel.java b/app/src/main/java/com/application/zhangshi_app_android/ui/function/HealthCareActivityViewModel.java
index bf13299..8d4e957 100644
--- a/app/src/main/java/com/application/zhangshi_app_android/ui/function/HealthCareActivityViewModel.java
+++ b/app/src/main/java/com/application/zhangshi_app_android/ui/function/HealthCareActivityViewModel.java
@@ -1,12 +1,31 @@
 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
@@ -15,11 +34,15 @@
  */
 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);
@@ -28,6 +51,142 @@
     @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() {
@@ -65,4 +224,78 @@
     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);
+    }
 }

--
Gitblit v1.9.1