cart screen
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package ch.pizzalink.android.adapter.recycler;
|
||||
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import ch.pizzalink.android.R;
|
||||
import ch.pizzalink.android.interfaces.RecyclerItemClickListener;
|
||||
import ch.pizzalink.android.model.CartProductModel;
|
||||
|
||||
/**
|
||||
* Created by cimenmus on 05/10/2017.
|
||||
*/
|
||||
|
||||
public class CartRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
|
||||
|
||||
private final int HOLDER_CART_PRODUCT = 0;
|
||||
private final int HOLDER_SPACE = 1;
|
||||
|
||||
private ArrayList<CartProductModel> cartProductList = new ArrayList<>();
|
||||
private RecyclerItemClickListener recyclerItemClickListener;
|
||||
|
||||
public static class CartProductViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
@BindView(R.id.cartProductNameTotalTextView) TextView cartProductNameTotalTextView;
|
||||
@BindView(R.id.cartProductInfoTextView) TextView cartProductInfoTextView;
|
||||
@BindView(R.id.cartProductTotalTextView) TextView cartProductTotalTextView;
|
||||
@BindView(R.id.removeProductFromCartImageView) ImageView removeProductFromCartImageView;
|
||||
|
||||
public CartProductViewHolder(final View view, final RecyclerItemClickListener recyclerItemClickListener) {
|
||||
super(view);
|
||||
ButterKnife.bind(this, view);
|
||||
removeProductFromCartImageView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if(recyclerItemClickListener != null)
|
||||
recyclerItemClickListener.onItemClick(removeProductFromCartImageView, getAdapterPosition());
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class SpaceViewHolder extends RecyclerView.ViewHolder{
|
||||
public SpaceViewHolder(final View view) {
|
||||
super(view);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
if(position == cartProductList.size())
|
||||
return HOLDER_SPACE;
|
||||
return HOLDER_CART_PRODUCT;
|
||||
}
|
||||
|
||||
public CartRecyclerAdapter(ArrayList<CartProductModel> cartProductList,
|
||||
RecyclerItemClickListener recyclerItemClickListener){
|
||||
this.cartProductList = cartProductList;
|
||||
this.recyclerItemClickListener = recyclerItemClickListener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
|
||||
|
||||
RecyclerView.ViewHolder viewHolder;
|
||||
View view;
|
||||
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
|
||||
|
||||
switch (viewType){
|
||||
|
||||
case HOLDER_CART_PRODUCT:
|
||||
view = inflater.inflate(R.layout.row_cart, viewGroup, false);
|
||||
viewHolder = new CartProductViewHolder(view, recyclerItemClickListener);
|
||||
break;
|
||||
|
||||
case HOLDER_SPACE:
|
||||
view = inflater.inflate(R.layout.row_space, viewGroup, false);
|
||||
viewHolder = new SpaceViewHolder(view);
|
||||
break;
|
||||
|
||||
default:
|
||||
view = inflater.inflate(R.layout.row_cart, viewGroup, false);
|
||||
viewHolder = new CartProductViewHolder(view, recyclerItemClickListener);
|
||||
break;
|
||||
}
|
||||
|
||||
return viewHolder;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
|
||||
|
||||
switch (holder.getItemViewType()){
|
||||
case HOLDER_CART_PRODUCT :
|
||||
CartProductViewHolder cartProductViewHolder = (CartProductViewHolder) holder;
|
||||
cartProductViewHolder.cartProductNameTotalTextView.setText(cartProductList.get(position).getName());
|
||||
cartProductViewHolder.cartProductTotalTextView.setText(cartProductList.get(position).getTotal());
|
||||
if(cartProductList.get(position).getOption() != null &&
|
||||
cartProductList.get(position).getOption().size() > 0){
|
||||
cartProductViewHolder.cartProductInfoTextView.setText(cartProductList.get(position).getOption().get(0).getValue());
|
||||
}
|
||||
break;
|
||||
|
||||
case HOLDER_SPACE :
|
||||
SpaceViewHolder spaceViewHolder = (SpaceViewHolder) holder;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return cartProductList.size() + 1 ;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,5 +10,7 @@ public class ApiEndPoints {
|
||||
public static final String API_LOGIN = PREFIX + "login" + SUFFIX;
|
||||
public static final String API_LOGOUT = PREFIX + "logout" + SUFFIX;
|
||||
public static final String API_GET_ORDER_HISTORY = PREFIX + "getOrders" + SUFFIX;
|
||||
public static final String API_GET_CART_PRODUCTS = PREFIX + "getBasketProducts" + SUFFIX;
|
||||
public static final String API_GET_CLEAR_CART = PREFIX + "clearBasket" + SUFFIX;
|
||||
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ public class ApiErrorUtils {
|
||||
try {
|
||||
error = converter.convert(response.errorBody());
|
||||
DialogHelper.showDialogWithPositiveButton(BaseActivity.currentActivity, error.getMessage());
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
return error;
|
||||
}
|
||||
return error;
|
||||
|
||||
@@ -3,6 +3,7 @@ package ch.pizzalink.android.api;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import ch.pizzalink.android.model.CartInfoModel;
|
||||
import ch.pizzalink.android.model.CategoryModel;
|
||||
import ch.pizzalink.android.model.OrderModel;
|
||||
import ch.pizzalink.android.model.UserModel;
|
||||
@@ -49,6 +50,12 @@ public interface ApiInterface {
|
||||
@GET(ApiEndPoints.API_GET_ORDER_HISTORY)
|
||||
Call<ResponseArray<OrderModel>> getOrderHistory(@Query("token") String token);
|
||||
|
||||
@GET(ApiEndPoints.API_GET_CART_PRODUCTS)
|
||||
Call<ResponseObject<CartInfoModel>> getCartProducts(@Query("token") String token);
|
||||
|
||||
@GET(ApiEndPoints.API_GET_CLEAR_CART)
|
||||
Call<ResponseObject> clearCart(@Query("token") String token);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,13 +1,39 @@
|
||||
package ch.pizzalink.android.fragment;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import butterknife.BindColor;
|
||||
import butterknife.BindString;
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
import ch.pizzalink.android.R;
|
||||
import ch.pizzalink.android.activity.BaseActivity;
|
||||
import ch.pizzalink.android.adapter.recycler.CartRecyclerAdapter;
|
||||
import ch.pizzalink.android.api.ApiErrorUtils;
|
||||
import ch.pizzalink.android.api.ApiService;
|
||||
import ch.pizzalink.android.api.ResponseObject;
|
||||
import ch.pizzalink.android.helper.DialogHelper;
|
||||
import ch.pizzalink.android.helper.SessionHelper;
|
||||
import ch.pizzalink.android.interfaces.RecyclerItemClickListener;
|
||||
import ch.pizzalink.android.model.CartInfoModel;
|
||||
import ch.pizzalink.android.model.CartProductModel;
|
||||
import ch.pizzalink.android.model.CartTotalModel;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
/**
|
||||
* Created by cimenmus on 18/09/2017.
|
||||
@@ -15,10 +41,25 @@ import ch.pizzalink.android.R;
|
||||
|
||||
public class CartFragment extends BaseFragment {
|
||||
|
||||
@BindView(R.id.cartFragmentMainLayout) RelativeLayout cartFragmentMainLayout;
|
||||
@BindView(R.id.noProductsOnCartTextView) TextView noProductsOnCartTextView;
|
||||
@BindView(R.id.cartRecyclerView) RecyclerView cartRecyclerView;
|
||||
@BindView(R.id.cartInfoLayout) LinearLayout cartInfoLayout;
|
||||
@BindView(R.id.cartTotalLabelTextView) TextView cartTotalLabelTextView;
|
||||
@BindView(R.id.cartProductTotalTextView) TextView cartProductTotalTextView;
|
||||
@BindView(R.id.continueCartButton) Button continueCartButton;
|
||||
@BindView(R.id.clearCartButton) Button clearCartButton;
|
||||
|
||||
@BindString(R.string.bottom_nav_menu_item_cart) String fragmentTitle;
|
||||
|
||||
@BindColor(R.color.actvity_default_background_color_1) int grayColor;
|
||||
@BindColor(R.color.white) int whiteColor;
|
||||
|
||||
public static final java.lang.String FRAGMENT_NAME = "cartFragment";
|
||||
|
||||
private ArrayList<CartProductModel> cartProductList = new ArrayList<>();
|
||||
private CartRecyclerAdapter cartRecyclerAdapter;
|
||||
|
||||
public CartFragment() {}
|
||||
|
||||
public static CartFragment newInstance() {
|
||||
@@ -35,10 +76,115 @@ public class CartFragment extends BaseFragment {
|
||||
View view = inflater.inflate(R.layout.fragment_cart, container, false);
|
||||
ButterKnife.bind(this, view);
|
||||
initViews();
|
||||
getCartProducts();
|
||||
return view;
|
||||
}
|
||||
|
||||
@OnClick({R.id.clearCartButton, R.id.continueCartButton})
|
||||
protected void onClick(View view){
|
||||
switch (view.getId()){
|
||||
case R.id.clearCartButton:
|
||||
clearCart();
|
||||
break;
|
||||
case R.id.continueCartButton:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void initViews(){
|
||||
setPizzalinkToolbarFields(false, fragmentTitle);
|
||||
initRecyclerView();
|
||||
}
|
||||
|
||||
private void getCartProducts(){
|
||||
DialogHelper.showLoadingDialog();
|
||||
Call<ResponseObject<CartInfoModel>> call = ApiService.apiInterface.getCartProducts(
|
||||
SessionHelper.getCustomerToken().getToken());
|
||||
call.enqueue(new Callback<ResponseObject<CartInfoModel>>() {
|
||||
@Override
|
||||
public void onResponse(Call<ResponseObject<CartInfoModel>> call, Response<ResponseObject<CartInfoModel>> response) {
|
||||
DialogHelper.hideLoadingDialog();
|
||||
if(response.isSuccessful() &&
|
||||
response.body().getData() != null &&
|
||||
response.body().isSuccess()){
|
||||
setCartTotalFields(response.body().getData().getTotals().get(0));
|
||||
fillAndNotifyAdapter(response.body().getData().getProducts());
|
||||
}
|
||||
else
|
||||
ApiErrorUtils.parseError(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<ResponseObject<CartInfoModel>> call, Throwable t) {
|
||||
DialogHelper.hideLoadingDialog();
|
||||
DialogHelper.showFailedDialog();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void clearCart(){
|
||||
DialogHelper.showLoadingDialog();
|
||||
Call<ResponseObject> call = ApiService.apiInterface.clearCart(
|
||||
SessionHelper.getCustomerToken().getToken());
|
||||
call.enqueue(new Callback<ResponseObject>() {
|
||||
@Override
|
||||
public void onResponse(Call<ResponseObject> call, Response<ResponseObject> response) {
|
||||
DialogHelper.hideLoadingDialog();
|
||||
if(response.isSuccessful() && response.body().isSuccess()){
|
||||
cartProductList.clear();
|
||||
cartRecyclerAdapter.notifyDataSetChanged();
|
||||
setCartLayoutsVisibility();
|
||||
}
|
||||
|
||||
else
|
||||
ApiErrorUtils.parseError(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<ResponseObject> call, Throwable t) {
|
||||
DialogHelper.hideLoadingDialog();
|
||||
DialogHelper.showFailedDialog();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void fillAndNotifyAdapter(ArrayList<CartProductModel> cartList){
|
||||
CartProductModel.checkNull(cartList);
|
||||
cartProductList.clear();
|
||||
cartProductList.addAll(cartList);
|
||||
cartRecyclerAdapter.notifyDataSetChanged();
|
||||
setCartLayoutsVisibility();
|
||||
}
|
||||
|
||||
private void setCartTotalFields(CartTotalModel cartTotalModel){
|
||||
cartTotalLabelTextView.setText(cartTotalModel.getTitle());
|
||||
cartProductTotalTextView.setText(cartTotalModel.getText());
|
||||
}
|
||||
|
||||
private void initRecyclerView(){
|
||||
cartRecyclerAdapter = new CartRecyclerAdapter(cartProductList, new RecyclerItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(View view, int position) {
|
||||
|
||||
}
|
||||
});
|
||||
LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.currentActivity);
|
||||
cartRecyclerView.setLayoutManager(layoutManager);
|
||||
cartRecyclerView.setAdapter(cartRecyclerAdapter);
|
||||
}
|
||||
|
||||
private void setCartLayoutsVisibility(){
|
||||
if(cartProductList.size() > 0){
|
||||
noProductsOnCartTextView.setVisibility(View.GONE);
|
||||
cartFragmentMainLayout.setBackgroundColor(grayColor);
|
||||
cartRecyclerView.setVisibility(View.VISIBLE);
|
||||
cartInfoLayout.setVisibility(View.VISIBLE);
|
||||
}
|
||||
else {
|
||||
cartRecyclerView.setVisibility(View.GONE);
|
||||
cartInfoLayout.setVisibility(View.GONE);
|
||||
cartFragmentMainLayout.setBackgroundColor(whiteColor);
|
||||
noProductsOnCartTextView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package ch.pizzalink.android.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by cimenmus on 05/10/2017.
|
||||
*/
|
||||
|
||||
public class CartInfoModel {
|
||||
|
||||
private ArrayList<CartProductModel> products;
|
||||
private ArrayList<CartTotalModel> totals;
|
||||
|
||||
public ArrayList<CartProductModel> getProducts() {
|
||||
return products;
|
||||
}
|
||||
|
||||
public void setProducts(ArrayList<CartProductModel> products) {
|
||||
this.products = products;
|
||||
}
|
||||
|
||||
public ArrayList<CartTotalModel> getTotals() {
|
||||
return totals;
|
||||
}
|
||||
|
||||
public void setTotals(ArrayList<CartTotalModel> totals) {
|
||||
this.totals = totals;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package ch.pizzalink.android.model;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by cimenmus on 05/10/2017.
|
||||
*/
|
||||
|
||||
public class CartProductModel {
|
||||
|
||||
@Expose @SerializedName("cart_id") private String cartId;
|
||||
@Expose @SerializedName("product_id") private String productId;
|
||||
|
||||
private String name;
|
||||
private String model;
|
||||
private String quantity;
|
||||
private String shipping;
|
||||
private String price;
|
||||
private String total;
|
||||
private boolean stock;
|
||||
private int reward;
|
||||
private ArrayList<ProductOptionModel> option;
|
||||
|
||||
private void checkNull(){
|
||||
|
||||
if(cartId == null)
|
||||
cartId = "";
|
||||
|
||||
if(productId == null)
|
||||
productId = "";
|
||||
|
||||
if(name == null)
|
||||
name = "";
|
||||
|
||||
if(model == null)
|
||||
model = "";
|
||||
|
||||
if(quantity == null)
|
||||
quantity = "";
|
||||
|
||||
if(shipping == null)
|
||||
shipping = "";
|
||||
|
||||
if(price == null)
|
||||
price = "";
|
||||
|
||||
if(total == null)
|
||||
total = "";
|
||||
|
||||
if(option != null){
|
||||
for(ProductOptionModel productOptionModel : option){
|
||||
productOptionModel.checkNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkNull(ArrayList<CartProductModel> cartProductList){
|
||||
for(CartProductModel cartProductModel : cartProductList){
|
||||
cartProductModel.checkNull();
|
||||
}
|
||||
}
|
||||
|
||||
public String getCartId() {
|
||||
return cartId;
|
||||
}
|
||||
|
||||
public void setCartId(String cartId) {
|
||||
this.cartId = cartId;
|
||||
}
|
||||
|
||||
public String getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(String productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(String quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public String getShipping() {
|
||||
return shipping;
|
||||
}
|
||||
|
||||
public void setShipping(String shipping) {
|
||||
this.shipping = shipping;
|
||||
}
|
||||
|
||||
public String getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(String price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(String total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public boolean isStock() {
|
||||
return stock;
|
||||
}
|
||||
|
||||
public void setStock(boolean stock) {
|
||||
this.stock = stock;
|
||||
}
|
||||
|
||||
public int getReward() {
|
||||
return reward;
|
||||
}
|
||||
|
||||
public void setReward(int reward) {
|
||||
this.reward = reward;
|
||||
}
|
||||
|
||||
public ArrayList<ProductOptionModel> getOption() {
|
||||
return option;
|
||||
}
|
||||
|
||||
public void setOption(ArrayList<ProductOptionModel> option) {
|
||||
this.option = option;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package ch.pizzalink.android.model;
|
||||
|
||||
/**
|
||||
* Created by cimenmus on 05/10/2017.
|
||||
*/
|
||||
|
||||
public class CartTotalModel {
|
||||
|
||||
private String title;
|
||||
private String text;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package ch.pizzalink.android.model;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
/**
|
||||
* Created by cimenmus on 05/10/2017.
|
||||
*/
|
||||
|
||||
public class ProductOptionModel {
|
||||
|
||||
@Expose @SerializedName("product_option_id") private String id;
|
||||
@Expose @SerializedName("product_option_value_id") private String valueId;
|
||||
|
||||
private String name;
|
||||
private String value;
|
||||
private String type;
|
||||
|
||||
public void checkNull(){
|
||||
|
||||
if(id == null)
|
||||
id = "";
|
||||
|
||||
if(valueId == null)
|
||||
valueId = "";
|
||||
|
||||
if(name == null)
|
||||
name = "";
|
||||
|
||||
if(value == null)
|
||||
value = "";
|
||||
|
||||
if(type == null)
|
||||
type = "";
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getValueId() {
|
||||
return valueId;
|
||||
}
|
||||
|
||||
public void setValueId(String valueId) {
|
||||
this.valueId = valueId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user