greyhat49

adapter watergas

Jan 31st, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.70 KB | None | 0 0
  1. package id.tukang.adapter;
  2.  
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.os.Handler;
  6. import android.support.v4.app.FragmentActivity;
  7. import android.support.v4.content.ContextCompat;
  8. import android.support.v4.content.LocalBroadcastManager;
  9. import android.support.v7.widget.RecyclerView;
  10. import android.util.Log;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.ArrayAdapter;
  15. import android.widget.ImageButton;
  16. import android.widget.ImageView;
  17. import android.widget.LinearLayout;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. import com.rey.material.widget.Spinner;
  22.  
  23. import java.util.List;
  24.  
  25. import id.tukang.CustomApplication;
  26. import id.tukang.GlobalFunction;
  27. import id.tukang.R;
  28. import id.tukang.database.dao.WaterPricesDS;
  29. import id.tukang.helper.Logc;
  30. import id.tukang.item.BrandsLabel;
  31. import id.tukang.item.WaterGas;
  32. import id.tukang.item.WaterGasObject;
  33.  
  34. /***
  35.  * Created by Meidika on 8/4/2015.
  36.  */
  37. public class WaterGasAdapter extends RecyclerView.Adapter<WaterGasAdapter.WaterGasViewHolder> {
  38.  
  39.     private List<WaterGas> waterGases;
  40.     private FragmentActivity activity;
  41.  
  42.     private static int VIEW_TYPE_ROW = 0;
  43.     private static int VIEW_TYPE_FOOTER = 1;
  44.     private boolean isInit = true;
  45.  
  46.     public WaterGasAdapter(List<WaterGas> waterGases, FragmentActivity activity) {
  47.         this.waterGases = waterGases;
  48.         this.activity = activity;
  49.         isInit = true;
  50.     }
  51.  
  52.     class WaterGasViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  53.         android.widget.LinearLayout water_container;
  54.         ImageView imgWaterGas;
  55.         TextView txtPrice;
  56.         TextView txtCount;
  57.         com.rey.material.widget.Spinner sBrands;
  58.         private int viewType;
  59.  
  60.         ImageButton btnMinus;
  61.         ImageButton btnPlus;
  62.         ImageButton btnDel;
  63.  
  64.         View itemView;
  65.  
  66.         ImageButton btnAddWater;
  67.         TextView txtAddWater;
  68.  
  69.         int oldPosition = 0;
  70.  
  71.         private Boolean isUserAction = false;
  72.         String[] stringArray;
  73.  
  74.         WaterGasViewHolder(View itemView) {
  75.             super(itemView);
  76.             this.itemView = itemView;
  77.         }
  78.  
  79.         void setViewType(int viewType) {
  80.             this.viewType = viewType;
  81.         }
  82.  
  83.         void initialize() {
  84.             if (viewType == VIEW_TYPE_ROW) {
  85.                 initializeRow();
  86.             }
  87.             if (viewType == VIEW_TYPE_FOOTER) {
  88.                 initializeFooter();
  89.             }
  90.         }
  91.  
  92.         private void initializeRow() {
  93.             water_container = (android.widget.LinearLayout) itemView.findViewById(R.id.water_container);
  94.             imgWaterGas = (ImageView) itemView.findViewById(R.id.imgWaterGas);
  95.             txtPrice = (TextView) itemView.findViewById(R.id.txtPrice);
  96.  
  97.             txtCount = (TextView) itemView.findViewById(R.id.txtCount);
  98.             sBrands = (com.rey.material.widget.Spinner) itemView.findViewById(R.id.sBrands);
  99.             btnMinus = (ImageButton) itemView.findViewById(R.id.btnMinus);
  100.             btnPlus = (ImageButton) itemView.findViewById(R.id.btnPlus);
  101.             btnDel = (ImageButton) itemView.findViewById(R.id.btnDel);
  102.  
  103.             btnMinus.setOnClickListener(this);
  104.             btnPlus.setOnClickListener(this);
  105.             btnDel.setOnClickListener(this);
  106.  
  107.             initiateSpinner(itemView);
  108.  
  109.             sBrands.setOnItemSelectedListener(new com.rey.material.widget.Spinner.OnItemSelectedListener() {
  110.                 @Override
  111.                 public void onItemSelected(com.rey.material.widget.Spinner parent, View view, int position, long id) {
  112.  
  113.                     if (position < 0)
  114.                         return;
  115.  
  116.                     if (position == 0) {
  117.                         Logc.e("c215--", "1");
  118.                         waterGases.get(getAdapterPosition()).brandId = new BrandsLabel().getId(sBrands.getSelectedItem().toString());
  119.                         waterGases.get(getAdapterPosition()).resId = R.drawable.ic_fp_water;
  120.                         waterGases.get(getAdapterPosition()).price = 0;
  121.                         waterGases.get(getAdapterPosition()).count = 0;
  122.                         waterGases.get(getAdapterPosition()).brandName = CustomApplication.getContext().getString(R.string.brand);
  123.  
  124.                         toggleBtnMinusIcon(waterGases.get(getAdapterPosition()).count, btnMinus, 0);
  125.                         toggleTextPrice(true, txtPrice, sBrands);
  126.                     } else {
  127.                         Logc.e("c216--", "1");
  128.  
  129.                         //Septian: fill data item product from local db
  130.                         WaterPricesDS waterPricesDS = new WaterPricesDS(CustomApplication.getContext());
  131.                         waterPricesDS.open();
  132.  
  133.                         waterGases.get(getAdapterPosition()).brandId = waterPricesDS.getBrandIdOnName(sBrands.getSelectedItem().toString());
  134.                         waterGases.get(getAdapterPosition()).resId = R.drawable.ic_fp_water;
  135.                         waterGases.get(getAdapterPosition()).price = waterPricesDS.getPriceBrandOnName(sBrands.getSelectedItem().toString());
  136.                         waterGases.get(getAdapterPosition()).brandName = waterPricesDS.getNameBrandOnName(sBrands.getSelectedItem().toString());
  137.  
  138.                         waterPricesDS.close();
  139.  
  140.                         Log.e("dynamicProduct", "onItemSelected: id " + id
  141.                                 + "\nposition" + position
  142.                                 + "\nadapter position " + getAdapterPosition()
  143.                                 + "\nbrandId " + waterGases.get(getAdapterPosition()).brandId
  144.                                 + "\nprice " + waterGases.get(getAdapterPosition()).price
  145.                                 + "\nbrandName " + waterGases.get(getAdapterPosition()).brandName);
  146.  
  147.                         if (waterGases.get(getAdapterPosition()).count == 0)
  148.                             waterGases.get(getAdapterPosition()).count = 0;
  149.  
  150.                         toggleBtnMinusIcon(waterGases.get(getAdapterPosition()).count, btnMinus, 0);
  151.                         toggleTextPrice(false, txtPrice, sBrands);
  152.  
  153.                     }
  154.                     /*if (!waterGases.get(getAdapterPosition()).brandName.equals(CustomApplication.getContext().getString(R.string.brand))) {
  155.                         initializeRow();
  156.                     }*/
  157.  
  158.                     fillRow(getAdapterPosition(), imgWaterGas, txtPrice, sBrands, txtCount, false);
  159.  
  160.                     if (isUserAction || oldPosition == 0)
  161.                         updateTotalTransaction(true);
  162.  
  163.                     validateRow(Integer.valueOf(txtCount.getTag().toString())
  164.                             , sBrands.getSelectedItemPosition()
  165.                             , txtCount
  166.                             , sBrands);
  167.  
  168.                     isUserAction = true;
  169.                     oldPosition = position;
  170.                 }
  171.             });
  172.  
  173.             txtCount.setTextColor(ContextCompat.getColor(activity.getApplicationContext()
  174.                     , R.color.black));
  175.         }
  176.  
  177.         private void initializeFooter() {
  178.             btnAddWater = (ImageButton) itemView.findViewById(R.id.btnAddWater);
  179.             txtAddWater = (TextView) itemView.findViewById(R.id.tvAddWater);
  180.  
  181.             btnAddWater.setOnClickListener(new View.OnClickListener() {
  182.                 @Override
  183.                 public void onClick(View view) {
  184.                     onAddWaterGasClick(false);
  185.                 }
  186.             });
  187.  
  188.             txtAddWater.setOnClickListener(new View.OnClickListener() {
  189.                 @Override
  190.                 public void onClick(View view) {
  191.                     onAddWaterGasClick(false);
  192.                 }
  193.             });
  194.         }
  195.  
  196.         //set item default selection
  197.         void onAddWaterGasClick(boolean isInit) {
  198.             /*if (isInit) {
  199.                 waterGases.add(new WaterGas(0, 0));
  200.                 waterGases.add(new WaterGas(0, 8));
  201.                 notifyItemInserted(waterGases.size() - 1);
  202.             } else {
  203.                 waterGases.add(new WaterGas(0, -1));
  204.                 notifyItemInserted(waterGases.size() - 1);
  205.             }*/
  206.  
  207.             waterGases.add(new WaterGas(0, -1));
  208.             waterGases.add(new WaterGas(0, -1));
  209.             notifyItemInserted(waterGases.size() - 1);
  210.         }
  211.  
  212.         private void initiateSpinner(View view) {
  213.  
  214.             for (int i = 0; i < waterGases.size(); i++) {
  215.                 Log.e("dynamicProduct", "initiateSpinner: size " + waterGases.size()
  216.                         + "\nbrand name " + waterGases.get(i).brandName
  217.                         + "\nid " + waterGases.get(i).brandId);
  218.             }
  219.  
  220.             sBrands = (com.rey.material.widget.Spinner) view.findViewById(R.id.sBrands);
  221.  
  222.             for (int i=0;i<waterGases.size();i++) {
  223.                 if (waterGases.get(i).brandName.equals(CustomApplication.getContext().getString(R.string.brand))) {
  224.  
  225.                     Log.d("dynamicProduct", "initiateSpinner: #1");
  226.  
  227.                     stringArray = GlobalFunction.getWaterPriceBrandNameAvailableVendors();
  228.                     stringArray[0] = CustomApplication.getContext().getString(R.string.brand);
  229.  
  230.                     //Septian:default selection item product to brand 'Pilih Product'
  231.                     sBrands.setSelection(0);
  232.                     break;
  233.                 } else {
  234.                     Log.d("dynamicProduct", "initiateSpinner: #2");
  235.  
  236.                     stringArray = GlobalFunction.getWaterPriceBrandNameAvailableVendorsByRanking(sBrands.getSelectedItem().toString());
  237.                     stringArray[0] = CustomApplication.getContext().getString(R.string.brand);
  238.                     break;
  239.                 }
  240.             }
  241.  
  242.             ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(activity.getApplicationContext(),
  243.                     R.layout.simple_spinner_dropdown_item_custom_48, stringArray);
  244.  
  245.             sBrands.setAdapter(adapter);
  246.             adapter.notifyDataSetChanged();
  247.  
  248.             isUserAction = false;
  249.  
  250.             if(sBrands.getSelectedItemPosition() == 0){
  251.                 ((TextView) sBrands.getSelectedView()).setTextColor(Color.GRAY);
  252.             }
  253.  
  254.             toggleTextPrice(true, txtPrice, sBrands);
  255.         }
  256.  
  257.         @Override
  258.         public void onClick(View v) {
  259.             if (v.equals(btnMinus))
  260.                 onBtnMinusClick(getAdapterPosition(), txtCount, sBrands, btnMinus);
  261.             if (v.equals(btnPlus))
  262.                 onBtnPlusClick(getAdapterPosition(), txtCount, sBrands, btnMinus);
  263.             if (v.equals(btnDel))
  264.                 onBtnDelClick(getAdapterPosition());
  265.         }
  266.     }
  267.  
  268.     private void validateRow(Integer currentTotal
  269.             , int selectedItemPosition
  270.             , TextView txtCount
  271.             , com.rey.material.widget.Spinner sBrands) {
  272.  
  273.         txtCount.setTextColor(ContextCompat.getColor(activity.getApplicationContext(), R.color.black));
  274.  
  275.         if (selectedItemPosition == 0) {
  276.             ((TextView) sBrands.getSelectedView()).setTextColor(Color.GRAY);
  277.         } else {
  278.             ((TextView) sBrands.getSelectedView()).setTextColor(Color.BLACK);
  279.         }
  280.  
  281.         if (currentTotal > 0 && selectedItemPosition == 0) {
  282.             ((TextView) sBrands.getSelectedView())
  283.                     .setTextColor(ContextCompat.getColor(activity.getApplicationContext(), R.color.red));
  284.         } else {
  285.             if (selectedItemPosition == 0) {
  286.                 ((TextView) sBrands.getSelectedView()).setTextColor(Color.GRAY);
  287.             } else {
  288.                 ((TextView) sBrands.getSelectedView()).setTextColor(Color.BLACK);
  289.             }
  290.         }
  291.     }
  292.  
  293.     private void onBtnDelClick(int position) {
  294.         waterGases.remove(position);
  295.         notifyItemRemoved(position);
  296.         notifyItemRangeChanged(position, waterGases.size());
  297.     }
  298.  
  299.     private void onBtnMinusClick(int adapterPosition, TextView txtCount, Spinner sBrands, ImageButton btnMinus) {
  300.         if (waterGases.size() < adapterPosition + 1 || adapterPosition < 0)
  301.             return;
  302.  
  303.         int currentCount = getTxtCountTag(txtCount);
  304.  
  305.         WaterGasObject item = new WaterGasObject(waterGases.get(adapterPosition).brandId);
  306.  
  307.         int minimumCount;
  308.  
  309.         Logc.e("c212--", String.valueOf(item.getUnitId()));
  310.         Logc.e("c213--", "1");
  311.  
  312.         minimumCount = 0;
  313.         if (currentCount >= minimumCount)
  314.             currentCount--;
  315.  
  316.         txtCount.setTag(currentCount);
  317.         setTxtCountText(txtCount);
  318.         waterGases.get(adapterPosition).count = currentCount;
  319.  
  320.         validateRow(currentCount, sBrands.getSelectedItemPosition(), txtCount, sBrands);
  321.  
  322.         toggleBtnMinusIcon(currentCount, btnMinus, minimumCount);
  323.  
  324.         if (currentCount == minimumCount - 1)
  325.             onBtnDelClick(adapterPosition);
  326.  
  327.         updateTotalTransaction(true);
  328.     }
  329.  
  330.     private void updateTotalTransaction(boolean shouldUpdateText) {
  331.  
  332.         long total = 0;
  333.         for (int i = 0; i < waterGases.size(); i++) {
  334.             total += waterGases.get(i).count * waterGases.get(i).price;
  335.             Logc.e("dynamicProduct", "size " + String.valueOf(waterGases.size())
  336.                     + "\ni " + String.valueOf(i)
  337.                     + "\ncount " + String.valueOf(waterGases.get(i).count)
  338.                     + "\nprice " + String.valueOf(waterGases.get(i).price)
  339.                     + "\nbrandName " + waterGases.get(i).brandName
  340.                     + "\ntotal " + String.valueOf(total));
  341.         }
  342.  
  343.         Intent intent = new Intent("waterGasListUpdateIntent");
  344.         intent.putExtra("total", total);
  345.  
  346.         if (shouldUpdateText)
  347.             intent.putExtra("shouldUpdateText", shouldUpdateText);
  348.  
  349.         LocalBroadcastManager.getInstance(activity.getApplicationContext()).sendBroadcast(intent);
  350.     }
  351.  
  352.     private void onBtnPlusClick(int adapterPosition, TextView txtCount, Spinner sBrands, ImageButton btnMinus) {
  353.  
  354.         if (adapterPosition < 0)
  355.             return;
  356.  
  357.         int currentCount = getTxtCountTag(txtCount);
  358.  
  359.         /*Buka ini jika limit item sebanyak 6 item setiap order
  360.         if (currentCount < 6)
  361.             currentCount++;
  362.         else
  363.             Toast.makeText(CustomApplication.getContext(),
  364.                     CustomApplication.getContext().getString(R.string.max_order_is_six),Toast.LENGTH_SHORT).show();*/
  365.  
  366.         //Septian: getSelectedItemPosition will be return the position adapter in spinner
  367.         //5 gonna be adapter position of lpg 3kg
  368.         //when custom product been implemented, position of adapter cannot be predicted
  369.         if (String.valueOf(sBrands.getSelectedItem()).contains("3kg")) {
  370.             if (currentCount < 2) {
  371.                 currentCount++;
  372.             } else {
  373.                 Toast.makeText(CustomApplication.getContext(),
  374.                         CustomApplication.getContext().getString(R.string.max_order_lpg3kg_is_two_a_day), Toast.LENGTH_SHORT)
  375.                         .show();
  376.             }
  377.         } else {
  378.             currentCount++;
  379.         }
  380.  
  381.         Log.e("tagItemLimit", "onBtnPlusClick: " + sBrands.getSelectedItemPosition() + " " + sBrands.getSelectedItem());
  382.  
  383.         toggleBtnMinusIcon(currentCount, btnMinus, 0);
  384.  
  385.         txtCount.setTag(currentCount);
  386.         setTxtCountText(txtCount);
  387.         waterGases.get(adapterPosition).count = currentCount;
  388.  
  389.         validateRow(currentCount, sBrands.getSelectedItemPosition(), txtCount, sBrands);
  390.  
  391.         updateTotalTransaction(true);
  392.     }
  393.  
  394.     private void setTxtCountText(TextView txtCount) {
  395.         int count = Integer.valueOf(txtCount.getTag().toString());
  396.         if (count >= 0)
  397.             txtCount.setText(GlobalFunction.fromHtmlCustom("<b>" + String.valueOf(count) + "</b>"));
  398.     }
  399.  
  400.     private int getTxtCountTag(TextView txtCount) {
  401.         return Integer.valueOf(txtCount.getTag().toString());
  402.     }
  403.  
  404.     @Override
  405.     public int getItemCount() {
  406.         return waterGases.size() + 1; //for VIEW_TYPE_FOOTER
  407.     }
  408.  
  409.     public WaterGas getItem(int position) {
  410.         return waterGases.get(position);
  411.     }
  412.  
  413.     @Override
  414.     public WaterGasViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
  415.  
  416.         int res = R.layout.item_water_v2;
  417.         if (viewType == VIEW_TYPE_FOOTER)
  418.             //this gonna be layout representative of button add more item products
  419.             res = R.layout.item_water_add;
  420.  
  421.         View v = LayoutInflater.from(viewGroup.getContext()).inflate(res, /*viewGroup*/ null, false);
  422.         WaterGasViewHolder wvh = new WaterGasViewHolder(v);
  423.         wvh.setViewType(viewType);
  424.         wvh.initialize();
  425.  
  426.         return wvh;
  427.     }
  428.  
  429.     @Override
  430.     public void onBindViewHolder(final WaterGasViewHolder waterGasViewHolder, final int i) {
  431.         if (waterGasViewHolder.viewType == VIEW_TYPE_ROW) {
  432.             fillRow(i, waterGasViewHolder.imgWaterGas, waterGasViewHolder.txtPrice, waterGasViewHolder.sBrands
  433.                     , waterGasViewHolder.txtCount, true);
  434.  
  435.             toggleBtnMinusIcon(waterGases.get(i).count, waterGasViewHolder.btnMinus, 0);
  436.         }
  437.  
  438.         //noinspection StatementWithEmptyBody
  439.         if (waterGasViewHolder.viewType == VIEW_TYPE_FOOTER) {
  440.             Handler handler = new Handler();
  441.  
  442.             final Runnable r = new Runnable() {
  443.                 public void run() {
  444.                     Logc.e("c144--", "1");
  445.                     if (waterGases.size() == 0 && isInit) {
  446.                         Logc.e("c144.1--", "1");
  447.                         isInit = false;
  448.                         waterGasViewHolder.onAddWaterGasClick(true);
  449.                     }
  450.                 }
  451.             };
  452.  
  453.             handler.post(r);
  454.         }
  455.     }
  456.  
  457.     private void toggleTextPrice(boolean b, TextView txtPrice, Spinner sBrands) {
  458.         if (b) {
  459.             txtPrice.setVisibility(View.GONE);
  460.             sBrands.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
  461.         } else {
  462.             txtPrice.setVisibility(View.VISIBLE);
  463.             txtPrice.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  464.             sBrands.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100));
  465.         }
  466.         txtPrice.requestLayout();
  467.         sBrands.requestLayout();
  468.     }
  469.  
  470.     private void toggleBtnMinusIcon(int count, ImageButton btnMinus, int minimumCount) {
  471.         if (count <= minimumCount) {
  472.             btnMinus.setImageResource(R.drawable.ic_del_yellow);
  473.         } else {
  474.             btnMinus.setImageResource(R.drawable.ic_minus_yellow);
  475.         }
  476.     }
  477.  
  478.     private void fillRow(int adapterPosition
  479.             , ImageView imgWaterGas
  480.             , TextView txtPrice
  481.             , Spinner sBrands
  482.             , TextView txtCount
  483.             , boolean shouldSelectSBrands) {
  484.  
  485.         imgWaterGas.setImageResource(waterGases.get(adapterPosition).resId);
  486.  
  487.         long price = waterGases.get(adapterPosition).price;
  488.  
  489.         txtPrice.setText((price == 0) ? CustomApplication.getContext().getString(R.string.rp_zero)
  490.                 : GlobalFunction.formatTotal(price));
  491.  
  492.         /*if (shouldSelectSBrands)
  493.             sBrands.setSelection(((ArrayAdapter) sBrands.getAdapter()).getPosition(
  494.                     new BrandsLabel().getName(waterGases.get(adapterPosition).brandId)
  495.             ));
  496.  
  497.         txtCount.setTag(waterGases.get(adapterPosition).count);
  498.         setTxtCountText(txtCount);*/
  499.     }
  500.  
  501.     @Override
  502.     public int getItemViewType(int position) {
  503.         return (position == waterGases.size()) ? VIEW_TYPE_FOOTER : VIEW_TYPE_ROW;
  504.     }
  505. }
Advertisement
Add Comment
Please, Sign In to add comment