cart screen
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user