Skip to content

Commit

Permalink
购物车更新 优化了购物车的界面和主要数据逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangqian666 committed Jun 12, 2018
1 parent 41af7c8 commit 454270d
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 314 deletions.
12 changes: 6 additions & 6 deletions app/src/main/java/com/zack/shop/mvp/contract/CartContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.jess.arms.mvp.IView;
import com.zack.shop.mvp.http.entity.BaseResponse;
import com.zack.shop.mvp.http.entity.cart.CartBean;
import com.zack.shop.mvp.http.entity.cart.StoreBean;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

Expand All @@ -18,18 +18,18 @@
public interface CartContract {
interface Model extends IModel {

Observable<BaseResponse<Map<Integer, List<CartBean>>>> list();
Observable<BaseResponse<List<StoreBean>>> list();

Observable<BaseResponse> addProduct(Integer productId,
Integer count);

Observable<BaseResponse> deleteProduct(String productIds);

Observable<BaseResponse<Map<Integer, List<CartBean>>>> selectProduct(
Observable<BaseResponse<List<StoreBean>>> selectProduct(
Integer productId,
Integer checked);

Observable<BaseResponse<Map<Integer, List<CartBean>>>> updateProductCount(
Observable<BaseResponse<CartBean>> updateProductCount(
Integer productId,
Integer count

Expand All @@ -38,10 +38,10 @@ Observable<BaseResponse<Map<Integer, List<CartBean>>>> updateProductCount(
}

interface View extends IView {
void cartList(List<CartBean> data);
void cartList(List<StoreBean> data);

void deleteProductIdsSuccess();

void updateProductCount(List<CartBean> baseResponse);
void updateProductCount(CartBean baseResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import com.jess.arms.mvp.IModel;
import com.jess.arms.mvp.IView;
import com.zack.shop.mvp.http.entity.BaseResponse;
import com.zack.shop.mvp.http.entity.cart.CartBean;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

Expand All @@ -20,12 +16,6 @@ interface View extends IView {
}

interface Model extends IModel {
Observable<BaseResponse> addProduct(Integer productId, Integer count);

Observable<BaseResponse> deleteProduct(String productIds);

Observable<BaseResponse<Map<Integer, List<CartBean>>>> selectProduct(Integer productId, Integer checked);

Observable<BaseResponse<Map<Integer, List<CartBean>>>> updateProductCount(Integer productId, Integer count);
Observable<BaseResponse> addCart(Integer productId, Integer count);
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/zack/shop/mvp/http/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
*/
public interface Api {
// String APP_DOMAIN = "http://192.168.199.115:8080";
String APP_DOMAIN = "http://192.168.4.174:8080";
// String APP_DOMAIN = "http://192.168.4.174:8080";
// String APP_DOMAIN = "http://192.168.199.115";
// String APP_DOMAIN = "http://192.168.199.115:8080";
String APP_DOMAIN = "http://192.168.199.115:8080";
// String APP_DOMAIN = "http://172.20.10.4:8080";
// String APP_DOMAIN = "http://10.0.0.1:8080";
// String APP_DOMAIN = "http://39.106.46.79";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.zack.shop.mvp.http.entity.BaseResponse;
import com.zack.shop.mvp.http.entity.cart.CartBean;
import com.zack.shop.mvp.http.entity.cart.StoreBean;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import retrofit2.http.Field;
Expand All @@ -20,7 +20,7 @@
public interface CartService {

@GET("/user/cart/list")
Observable<BaseResponse<Map<Integer, List<CartBean>>>> list(
Observable<BaseResponse<List<StoreBean>>> list(
);

@FormUrlEncoded
Expand All @@ -38,15 +38,15 @@ Observable<BaseResponse> deleteProduct(

@FormUrlEncoded
@POST("/user/cart/select")
Observable<BaseResponse<Map<Integer, List<CartBean>>>> selectProduct(
Observable<BaseResponse<List<StoreBean>>> selectProduct(
@Field("productId") Integer productId,
@Field("checked") Integer checked

);

@FormUrlEncoded
@POST("/user/cart/update/count")
Observable<BaseResponse<Map<Integer, List<CartBean>>>> updateProductCount(
Observable<BaseResponse<CartBean>> updateProductCount(
@Field("productId") Integer productId,
@Field("count") Integer count

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ public class CartBean implements MultiItemEntity {
private Integer userId;
private Integer productId;
private Integer quantity;
private Integer checked;
// private Integer checked;
private Product productVo;

private boolean isChecked;

final public static int ITEM = 1;
final public static int ITEM_WITH_HEADER = 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.zack.shop.mvp.http.entity.cart;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

/**
* @Author 张迁-zhangqian
* @Data 2018/6/12 下午4:28
* @Package com.zack.shop.mvp.http.entity.cart
**/
@Getter
@Setter
public class StoreBean {
private Integer userId;
private String username;
private List<CartBean> cartVos;

private boolean isChecked;
}
8 changes: 4 additions & 4 deletions app/src/main/java/com/zack/shop/mvp/model/CartModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import com.zack.shop.mvp.http.api.service.CartService;
import com.zack.shop.mvp.http.entity.BaseResponse;
import com.zack.shop.mvp.http.entity.cart.CartBean;
import com.zack.shop.mvp.http.entity.cart.StoreBean;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;

Expand All @@ -23,7 +23,7 @@ public CartModel(IRepositoryManager repositoryManager) {
super(repositoryManager);
}

public Observable<BaseResponse<Map<Integer, List<CartBean>>>> list() {
public Observable<BaseResponse<List<StoreBean>>> list() {
return mRepositoryManager.obtainRetrofitService(CartService.class)
.list();
}
Expand All @@ -39,14 +39,14 @@ public Observable<BaseResponse> deleteProduct(String productIds) {
.deleteProduct(productIds);
}

public Observable<BaseResponse<Map<Integer, List<CartBean>>>> selectProduct(
public Observable<BaseResponse<List<StoreBean>>> selectProduct(
Integer productId,
Integer checked) {
return mRepositoryManager.obtainRetrofitService(CartService.class)
.selectProduct(productId, checked);
}

public Observable<BaseResponse<Map<Integer, List<CartBean>>>> updateProductCount(
public Observable<BaseResponse<CartBean>> updateProductCount(
Integer productId,
Integer count

Expand Down
23 changes: 1 addition & 22 deletions app/src/main/java/com/zack/shop/mvp/model/ProductModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import com.zack.shop.mvp.http.api.service.ProductService;
import com.zack.shop.mvp.http.api.service.UploadService;
import com.zack.shop.mvp.http.entity.BaseResponse;
import com.zack.shop.mvp.http.entity.cart.CartBean;
import com.zack.shop.mvp.http.entity.product.RecommendBean;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MultipartBody;
Expand All @@ -36,31 +34,12 @@ public Observable<BaseResponse<RecommendBean>> getRecommend() {
.getRecommendedProducts(0, 0, "");
}

public Observable<BaseResponse> addProduct(Integer productId, Integer count) {
public Observable<BaseResponse> addCart(Integer productId, Integer count) {
return mRepositoryManager
.obtainRetrofitService(CartService.class)
.addProduct(productId, count);
}

public Observable<BaseResponse> deleteProduct(String productIds) {
return mRepositoryManager
.obtainRetrofitService(CartService.class)
.deleteProduct(productIds);
}

public Observable<BaseResponse<Map<Integer, List<CartBean>>>> selectProduct(Integer productId, Integer checked) {
return mRepositoryManager
.obtainRetrofitService(CartService.class)
.selectProduct(productId, checked);
}

public Observable<BaseResponse<Map<Integer, List<CartBean>>>> updateProductCount(Integer productId, Integer count) {
return mRepositoryManager
.obtainRetrofitService(CartService.class)
.updateProductCount(productId, count);
}


public Observable<BaseResponse<String>> upLoadImage(MultipartBody.Part upload_file) {
return mRepositoryManager.obtainRetrofitService(UploadService.class)
.uploadImage(upload_file);
Expand Down
75 changes: 26 additions & 49 deletions app/src/main/java/com/zack/shop/mvp/presenter/CartPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import com.zack.shop.mvp.contract.CartContract;
import com.zack.shop.mvp.http.entity.BaseResponse;
import com.zack.shop.mvp.http.entity.cart.CartBean;
import com.zack.shop.mvp.http.entity.cart.StoreBean;
import com.zack.shop.mvp.ui.widget.AmountView;
import com.zack.shop.mvp.utils.RxUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

Expand All @@ -36,30 +36,19 @@ public CartPresenter(CartContract.Model model, CartContract.View rootView, RxErr


public void list() {
mModel.list().subscribeOn(Schedulers.io())
.map(mapBaseResponse -> {
if (mapBaseResponse.isSuccess()) {
mRootView.showMessage(mapBaseResponse.getMsg());
}
List<CartBean> cartBeans = new ArrayList<>();
Map<Integer, List<CartBean>> data = mapBaseResponse.getData();
for (Map.Entry<Integer, List<CartBean>> entry : data.entrySet()) {
cartBeans.addAll(entry.getValue());
}

return cartBeans;
})
mModel.list()
.subscribeOn(Schedulers.io())
.doOnSubscribe(disposable -> {
})
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.doFinally(() -> {
mRootView.hideLoading();//隐藏进度条
}).compose(RxLifecycleUtils.bindToLifecycle(mRootView))
.subscribe(new ErrorHandleSubscriber<List<CartBean>>(rxErrorHandler) {
.subscribe(new ErrorHandleSubscriber<BaseResponse<List<StoreBean>>>(rxErrorHandler) {
@Override
public void onNext(List<CartBean> baseResponse) {
mRootView.cartList(baseResponse);
public void onNext(BaseResponse<List<StoreBean>> baseResponse) {
mRootView.cartList(baseResponse.getData());

}
});
Expand All @@ -70,48 +59,36 @@ public void onNext(List<CartBean> baseResponse) {
public void selectProduct(Integer productId,
Integer checked) {
mModel.selectProduct(productId, checked)
.map(mapBaseResponse -> {
if (mapBaseResponse.isSuccess()) {
mRootView.showMessage(mapBaseResponse.getMsg());
}
List<CartBean> cartBeans = new ArrayList<>();
Map<Integer, List<CartBean>> data = mapBaseResponse.getData();
for (Map.Entry<Integer, List<CartBean>> entry : data.entrySet()) {
cartBeans.addAll(entry.getValue());
}

return cartBeans;
})
.compose(RxUtils.applySchedulers(mRootView))
.subscribe(new ErrorHandleSubscriber<List<CartBean>>(rxErrorHandler) {
.subscribe(new ErrorHandleSubscriber<BaseResponse<List<StoreBean>>>(rxErrorHandler) {
@Override
public void onNext(List<CartBean> baseResponse) {
mRootView.cartList(baseResponse);
public void onNext(BaseResponse<List<StoreBean>> baseResponse) {
mRootView.cartList(baseResponse.getData());

}
});
}

public void updateProductCount(Integer productId,
public void updateProductCount(AmountView view, CartBean cartBean,
Integer count) {
mModel.updateProductCount(productId, count)
.map(mapBaseResponse -> {
if (mapBaseResponse.isSuccess()) {
mRootView.showMessage(mapBaseResponse.getMsg());
}
List<CartBean> cartBeans = new ArrayList<>();
Map<Integer, List<CartBean>> data = mapBaseResponse.getData();
for (Map.Entry<Integer, List<CartBean>> entry : data.entrySet()) {
cartBeans.addAll(entry.getValue());
mModel.updateProductCount(cartBean.getProductId(), count)
.compose(RxUtils.applySchedulers(mRootView))
.subscribe(new ErrorHandleSubscriber<BaseResponse<CartBean>>(rxErrorHandler) {
@Override
public void onNext(BaseResponse<CartBean> cartBeanBaseResponse) {
if (cartBeanBaseResponse.isSuccess()) {
cartBean.setQuantity(cartBeanBaseResponse.getData().getQuantity());
mRootView.updateProductCount(cartBeanBaseResponse.getData());
} else {
view.setCurrentAmount(cartBean.getQuantity());
mRootView.showMessage(cartBeanBaseResponse.getMsg());
}
}

return cartBeans;
})
.compose(RxUtils.applySchedulers(mRootView))
.subscribe(new ErrorHandleSubscriber<List<CartBean>>(rxErrorHandler) {
@Override
public void onNext(List<CartBean> baseResponse) {
mRootView.updateProductCount(baseResponse);
public void onError(Throwable t) {
super.onError(t);
view.setCurrentAmount(cartBean.getQuantity());

}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ProductDetailsPresenter(ProductDetailsContract.Model model, ProductDetail
}

public void addProduct(Integer productId, Integer count) {
mModel.addProduct(productId, count)
mModel.addCart(productId, count)
.compose(RxUtils.applySchedulers(mRootView))
.subscribe(new ErrorHandleSubscriber<BaseResponse>(rxErrorHandler) {
@Override
Expand All @@ -43,27 +43,4 @@ public void onNext(BaseResponse baseResponse) {

}

public void deleteProduct(String productIds) {
mModel.deleteProduct(productIds)
.compose(RxUtils.applySchedulers(mRootView))
.subscribe(new ErrorHandleSubscriber<BaseResponse>(rxErrorHandler) {
@Override
public void onNext(BaseResponse baseResponse) {

}
});

}

public void updateProductCount(Integer productId, Integer count) {
mModel.updateProductCount(productId, count)
.compose(RxUtils.applySchedulers(mRootView))
.subscribe(new ErrorHandleSubscriber<BaseResponse>(rxErrorHandler) {
@Override
public void onNext(BaseResponse baseResponse) {

}
});

}
}
Loading

0 comments on commit 454270d

Please sign in to comment.