CartItemViewModel.java
8.03 KB
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
package com.naji.xiaotao.cart.vm;
import android.content.Context;
import android.databinding.ObservableField;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.naji.xiaotao.cart.entity.CartCookbookDetail;
import com.naji.xiaotao.cookbook.bean.ResultEntity;
import com.naji.xiaotao.cookbook.service.CookbookApiService;
import com.naji.xiaotao.http.RetrofitClient;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
public class CartItemViewModel extends BaseViewModel{
private String orderid;
private String fromSource;
private boolean isWaitingSure;//待确认订单
public CartCookbookDetail entity;
public ObservableField<String> title=new ObservableField<>();//标题栏显示
public ObservableField<String> count=new ObservableField<>();
private Disposable mSubscription;
public CartItemViewModel(Context context,CartCookbookDetail entity,String orderid,String fromSource,boolean isWaitingSure){
super(context);
this.entity=entity;
this.orderid=orderid;
this.fromSource=fromSource;
this.isWaitingSure=isWaitingSure;
count.set(entity.getCount());
if(entity.getTitleName().equals("其它")){
title.set(entity.getClass_name());
if(entity.getName()!=null&&entity.getName().equals("餐位费")){
mSubscription=RxBus.getDefault().toObservable(Integer.class).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
count.set(integer+"");
KLog.d("count:"+count.get().toString());
}
});
RxSubscriptions.add(mSubscription);
}
}else{
title.set(entity.getClass_name()+"("+entity.getSize()+")");
}
}
@Override
public void onDestroy() {
super.onDestroy();
if(entity.getName().equals("餐位费")){
RxSubscriptions.remove(mSubscription);
}
}
//减菜按钮
public BindingCommand onReduceClickCommand=new BindingCommand(new BindingAction() {
@Override
public void call() {
KLog.d("------------------");
reduceCookFromCart();
}
});
//加菜按钮
public BindingCommand onAddClickCommand=new BindingCommand(new BindingAction() {
@Override
public void call() {
KLog.d("+++++++++++++++++");
addCookToCart();
}
});
//添加菜品到购物车
private void addCookToCart(){
Map<String,String> params=new HashMap<>();
params.put("orderid",orderid);
params.put("openid",entity.getOpenid());
params.put("cookbookid",entity.getCookbookid());
params.put("identity","2");
if(TextUtils.isEmpty(entity.getSpecs())){
params.put("price",entity.getPrice());
}else{
params.put("price",entity.getSpec_price());
}
if(!TextUtils.isEmpty(entity.getSpecid())){
params.put("specid",entity.getSpecid());
}
if(!TextUtils.isEmpty(entity.getPracticeid())){
params.put("practiceid",entity.getPracticeid());
}
if(!TextUtils.isEmpty(fromSource)){
params.put("fromSoucre",fromSource);
}
RetrofitClient.getInstance().create(CookbookApiService.class)
.addCookToCart(params)
.compose(RxUtils.schedulersTransformer())
.compose(RxUtils.exceptionTransformer())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
}
})
.subscribe(new Consumer<BaseResponse<List<ResultEntity>>>() {
@Override
public void accept(BaseResponse<List<ResultEntity>> baseResponse) throws Exception {
if(baseResponse.getRet()==200){
count.set((Integer.parseInt(count.get())+1)+"");
int countTotal=baseResponse.getData().get(0).getCountTotal();
float countPrice=baseResponse.getData().get(0).getCountPirce();
String title="已点"+countTotal+"个菜,共计¥"+countPrice;
RxBus.getDefault().post(title);
}else{
ToastUtils.showShort(baseResponse.getMsg());
}
}
}, new Consumer<ResponseThrowable>() {
@Override
public void accept(ResponseThrowable throwable) throws Exception {
ToastUtils.showShort(throwable.msg);
throwable.printStackTrace();
}
});
}
//删减菜品从购物车
private void reduceCookFromCart(){
Map<String,String> params=new HashMap<>();
params.put("orderid",orderid);
params.put("openid",entity.getOpenid());
params.put("cookbookid",entity.getCookbookid());
params.put("identity","2");
if(!TextUtils.isEmpty(entity.getSpecid())){
params.put("specid",entity.getSpecid());
}
if(!TextUtils.isEmpty(entity.getPracticeid())){
params.put("practiceid",entity.getPracticeid());
}
if(!TextUtils.isEmpty(fromSource)){
params.put("fromSoucre",fromSource);
}
if(isWaitingSure){
params.put("request_uri","1");
}
RetrofitClient.getInstance().create(CookbookApiService.class)
.reduceCookFromCart(params)
.compose(RxUtils.schedulersTransformer())
.compose(RxUtils.exceptionTransformer())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
}
})
.subscribe(new Consumer<BaseResponse<List<ResultEntity>>>() {
@Override
public void accept(BaseResponse<List<ResultEntity>> baseResponse) throws Exception {
if(baseResponse.getRet()==200){
count.set((Integer.parseInt(count.get())-1)+"");
int countTotal=baseResponse.getData().get(0).getCountTotal();
float countPrice=baseResponse.getData().get(0).getCountPirce();
String title="已点"+countTotal+"个菜,共计¥"+countPrice;
RxBus.getDefault().post(title);
if(Integer.parseInt(count.get())==0){
Messenger.getDefault().send(CartItemViewModel.this,CartViewModel.TOKEN_DELETE_ITEM);
}
}else{
ToastUtils.showShort(baseResponse.getMsg());
}
}
}, new Consumer<ResponseThrowable>() {
@Override
public void accept(ResponseThrowable throwable) throws Exception {
ToastUtils.showShort(throwable.msg);
throwable.printStackTrace();
}
});
}
}