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.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.BannerBean;
|
import com.application.zhangshi_app_android.bean.UploadFileResponseBean;
|
import com.application.zhangshi_app_android.data.DataRepository;
|
|
import java.io.File;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
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.07.30. 8:52
|
* @desc
|
*/
|
public class ImageSelectActivityViewModel extends BaseViewModel<DataRepository> {
|
private MutableLiveData<Integer> flagLiveData;//0 图片选择 1 视频选择
|
private MutableLiveData<Boolean> typeLiveData;//true 编辑 false 查看
|
private MutableLiveData<List<BannerBean>> dataListLiveData = new MutableLiveData<>();
|
public ImageSelectActivityViewModel(@NonNull Application application) {
|
super(application);
|
}
|
|
@Override
|
protected DataRepository initModel() {
|
return DataRepository.getInstance();
|
}
|
|
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 setType(boolean type) {
|
if (typeLiveData == null){
|
typeLiveData = new MutableLiveData<>();
|
}
|
typeLiveData.setValue(type);
|
}
|
public boolean getType(){
|
if (typeLiveData == null){
|
typeLiveData = new MutableLiveData<>();
|
typeLiveData.setValue(false);
|
}
|
return Boolean.TRUE.equals(typeLiveData.getValue());
|
}
|
|
public MutableLiveData<Integer> getFlagLiveData() {
|
if (flagLiveData == null){
|
flagLiveData = new MutableLiveData<>();
|
flagLiveData.setValue(0);
|
}
|
return flagLiveData;
|
}
|
public int getFlag() {
|
if (flagLiveData.getValue()!=null){
|
return flagLiveData.getValue();
|
}
|
return 0;
|
}
|
|
public void setFlagLiveData(MutableLiveData<Integer> flagLiveData) {
|
this.flagLiveData = flagLiveData;
|
}
|
|
public void setFlag(int flag) {
|
if (flagLiveData == null){
|
flagLiveData = new MutableLiveData<>();
|
}
|
flagLiveData.setValue(flag);
|
}
|
|
public MutableLiveData<List<BannerBean>> getListLiveData() {
|
if (dataListLiveData == null){
|
dataListLiveData = new MutableLiveData<>();
|
}
|
return dataListLiveData;
|
}
|
|
public void setList(List<BannerBean> listMutableLiveData) {
|
if (this.dataListLiveData == null){
|
this.dataListLiveData = new MutableLiveData<>();
|
}
|
this.dataListLiveData.setValue(listMutableLiveData);
|
}
|
|
/**
|
* 添加 Banner 图片 或 视频
|
*/
|
public void add(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 {
|
BannerBean bannerBean = new BannerBean();
|
bannerBean.setUrl(resultData.getData().getUrl());
|
if (flagLiveData.getValue()!=null){
|
bannerBean.setFlag(flagLiveData.getValue());
|
}
|
return model.addBannerData(bannerBean);
|
}
|
})
|
.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("添加成功");
|
getBannerData();
|
}else {
|
messageLiveData.postValue(data.getMsg());
|
}
|
}
|
|
@Override
|
public void onError(Throwable e) {
|
messageLiveData.postValue(e.getMessage());
|
}
|
|
@Override
|
public void onComplete() {
|
|
}
|
});
|
}
|
|
/**
|
* 删除 Banner 图片 或 视频
|
*/
|
public void delete(String ids) {
|
model.deleteBannerData(ids)
|
.compose(RxUtils.schedulersTransformer())
|
.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("删除成功");
|
getBannerData();
|
} else {
|
messageLiveData.postValue(data.getMsg());
|
}
|
}
|
|
@Override
|
public void onError(Throwable e) {
|
messageLiveData.postValue(e.getMessage());
|
}
|
|
@Override
|
public void onComplete() {
|
|
}
|
});
|
}
|
|
/**
|
* 修改 Banner 图片 或 视频
|
*/
|
public void update(BannerBean bannerBean) {
|
model.updateBannerData(bannerBean)
|
.compose(RxUtils.schedulersTransformer())
|
.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){
|
getBannerData();
|
}else {
|
messageLiveData.postValue(data.getMsg());
|
}
|
}
|
|
@Override
|
public void onError(Throwable e) {
|
messageLiveData.postValue(e.getMessage());
|
}
|
|
@Override
|
public void onComplete() {
|
|
}
|
});
|
}
|
|
/**
|
* 获取banner 轮播图数据
|
*/
|
public void getBannerData() {
|
model.getBannerData()
|
.compose(RxUtils.schedulersTransformer())
|
.subscribe(new Observer<ResultData<List<BannerBean>>>() {
|
@Override
|
public void onSubscribe(Disposable d) {
|
addSubscribe(d);
|
}
|
|
@Override
|
public void onNext(ResultData<List<BannerBean>> data) {
|
if (data.getCode() == CODE_SUCCESS){
|
if (data.getData()!=null && data.getData().size()>0){
|
List<BannerBean> list = new ArrayList<>();
|
for (BannerBean datum : data.getData()) {
|
if (flagLiveData.getValue()!=null){
|
if (datum.getFlag() == flagLiveData.getValue()){
|
list.add(datum);
|
}
|
}
|
}
|
dataListLiveData.postValue(list);
|
}
|
}else {
|
messageLiveData.postValue(data.getMsg());
|
}
|
}
|
|
@Override
|
public void onError(Throwable e) {
|
messageLiveData.postValue(e.getMessage());
|
}
|
|
@Override
|
public void onComplete() {
|
|
}
|
});
|
}
|
|
}
|