greyhat49

adapter watergas

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