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
package com.application.zhangshi_app_android.ui.personal_center;
 
import static com.android.app_base.base.BaseConfig.CODE_SUCCESS;
 
import android.app.Application;
 
import androidx.annotation.NonNull;
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.manager.UserManager;
import com.android.app_base.utils.RxUtils;
import com.android.app_base.utils.rxbus.MessageEvent;
import com.android.app_base.utils.rxbus.RxBus;
import com.application.zhangshi_app_android.bean.BannerBean;
import com.application.zhangshi_app_android.bean.GrowthExperienceInformationBean;
import com.application.zhangshi_app_android.bean.UploadFileResponseBean;
import com.application.zhangshi_app_android.data.DataRepository;
 
import java.io.File;
 
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
 
/**
 * @author Ljj
 * @date 2023.03.15. 20:18
 * @desc 个人中心 Fragment的 ViewModel
 */
public class PersonalCenterFragmentViewModel extends BaseViewModel<DataRepository> {
    private MutableLiveData<GrowthExperienceInformationBean> infoLiveData;//个人信息
    public PersonalCenterFragmentViewModel(@NonNull Application application) {
        super(application);
    }
 
    @Override
    protected DataRepository initModel() {
        return DataRepository.getInstance();
    }
 
    /**
     * 获取个人信息
     */
    public void getInfo(){
        model.getGrowthExperienceInformation()
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new Observer<ResultData<GrowthExperienceInformationBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
 
                    @Override
                    public void onNext(ResultData<GrowthExperienceInformationBean> data) {
                        if (data.getCode() == CODE_SUCCESS){
                            getInfoLiveData().postValue(data.getData());
                            GrowthExperienceInformationBean informationBean = data.getData();
                            if(informationBean != null){
                                UserManager.getInstance().setUserName(informationBean.getNickName());
                                UserManager.getInstance().setUserAvatar(informationBean.getImg());
                                RxBus.getInstance().post(new MessageEvent(MessageEvent.EVENT_UPDATE_USER_INFO,informationBean));
                            }
                        }else {
                            messageLiveData.postValue(data.getMsg());
                        }
                    }
 
 
                    @Override
                    public void onError(Throwable e) {
                        messageLiveData.postValue(e.getMessage());
                    }
 
                    @Override
                    public void onComplete() {
 
                    }
                });
    }
 
    /**
     * 修改个人信息(头像)
     */
    public void updateInfo(String realPath) {
        File file = new File(realPath);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("uploadFile", file.getName(), requestBody);
        model.uploadFile(body)
                .flatMap(new Function<ResultData<UploadFileResponseBean>, ObservableSource<ResultData<String>>>() {
                    @Override
                    public ObservableSource<ResultData<String>> apply(ResultData<UploadFileResponseBean> resultData) throws Exception {
                        GrowthExperienceInformationBean bean = getInfoLiveData().getValue();
                        if (bean == null){
                            bean = new GrowthExperienceInformationBean();
                        }
                        bean.setImg(resultData.getData().getUrl());
                        infoLiveData.postValue(bean);
                        return model.updateGrowthExperienceInformation(bean);
                    }
                })
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        changeStateView(StateViewEnum.DIALOG_LOADING);
                    }
                })
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        changeStateView(StateViewEnum.DIALOG_DISMISS);
                    }
                })
                .subscribe(new Observer<ResultData<String>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addSubscribe(d);
                    }
 
                    @Override
                    public void onNext(ResultData<String> data) {
                        if (data.getCode() == CODE_SUCCESS){
                            messageLiveData.postValue("头像已上传");
                            GrowthExperienceInformationBean informationBean = getInfoLiveData().getValue();
                            if(informationBean != null){
                                UserManager.getInstance().setUserName(informationBean.getNickName());
                                UserManager.getInstance().setUserAvatar(informationBean.getUrl());
                                RxBus.getInstance().post(new MessageEvent(MessageEvent.EVENT_UPDATE_USER_INFO,informationBean));
                            }
                        }else {
                            messageLiveData.postValue(data.getMsg());
                        }
                    }
 
                    @Override
                    public void onError(Throwable e) {
                        messageLiveData.postValue(e.getMessage());
                    }
 
                    @Override
                    public void onComplete() {
 
                    }
                });
    }
 
 
    public MutableLiveData<GrowthExperienceInformationBean> getInfoLiveData() {
        if (infoLiveData == null){
            infoLiveData = new MutableLiveData<>();
        }
        return infoLiveData;
    }
 
    public void setInfoLiveData(GrowthExperienceInformationBean infoBean) {
        if (infoLiveData == null){
            infoLiveData = new MutableLiveData<>();
        }
        infoLiveData.setValue(infoBean);
    }
}