Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package id.tukang.adapter;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Handler;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.content.ContextCompat;
- import android.support.v4.content.LocalBroadcastManager;
- import android.support.v7.widget.RecyclerView;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ArrayAdapter;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.rey.material.widget.Spinner;
- import java.util.List;
- import id.tukang.CustomApplication;
- import id.tukang.GlobalFunction;
- import id.tukang.R;
- import id.tukang.database.dao.WaterPricesDS;
- import id.tukang.helper.Logc;
- import id.tukang.item.BrandsLabel;
- import id.tukang.item.WaterGas;
- import id.tukang.item.WaterGasObject;
- /***
- * Created by Meidika on 8/4/2015.
- */
- public class WaterGasAdapter extends RecyclerView.Adapter<WaterGasAdapter.WaterGasViewHolder> {
- private List<WaterGas> waterGases;
- private FragmentActivity activity;
- private static int VIEW_TYPE_ROW = 0;
- private static int VIEW_TYPE_FOOTER = 1;
- private boolean isInit = true;
- public WaterGasAdapter(List<WaterGas> waterGases, FragmentActivity activity) {
- this.waterGases = waterGases;
- this.activity = activity;
- isInit = true;
- }
- class WaterGasViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
- android.widget.LinearLayout water_container;
- ImageView imgWaterGas;
- TextView txtPrice;
- TextView txtCount;
- com.rey.material.widget.Spinner sBrands;
- private int viewType;
- ImageButton btnMinus;
- ImageButton btnPlus;
- ImageButton btnDel;
- View itemView;
- ImageButton btnAddWater;
- TextView txtAddWater;
- int oldPosition = 0;
- private Boolean isUserAction = false;
- String[] stringArray;
- WaterGasViewHolder(View itemView) {
- super(itemView);
- this.itemView = itemView;
- }
- void setViewType(int viewType) {
- this.viewType = viewType;
- }
- void initialize() {
- if (viewType == VIEW_TYPE_ROW) {
- initializeRow();
- }
- if (viewType == VIEW_TYPE_FOOTER) {
- initializeFooter();
- }
- }
- private void initializeRow() {
- water_container = (android.widget.LinearLayout) itemView.findViewById(R.id.water_container);
- imgWaterGas = (ImageView) itemView.findViewById(R.id.imgWaterGas);
- txtPrice = (TextView) itemView.findViewById(R.id.txtPrice);
- txtCount = (TextView) itemView.findViewById(R.id.txtCount);
- sBrands = (com.rey.material.widget.Spinner) itemView.findViewById(R.id.sBrands);
- btnMinus = (ImageButton) itemView.findViewById(R.id.btnMinus);
- btnPlus = (ImageButton) itemView.findViewById(R.id.btnPlus);
- btnDel = (ImageButton) itemView.findViewById(R.id.btnDel);
- btnMinus.setOnClickListener(this);
- btnPlus.setOnClickListener(this);
- btnDel.setOnClickListener(this);
- initiateSpinner(itemView);
- sBrands.setOnItemSelectedListener(new com.rey.material.widget.Spinner.OnItemSelectedListener() {
- @Override
- public void onItemSelected(com.rey.material.widget.Spinner parent, View view, int position, long id) {
- if (position < 0)
- return;
- if (position == 0) {
- Logc.e("c215--", "1");
- waterGases.get(getAdapterPosition()).brandId = new BrandsLabel().getId(sBrands.getSelectedItem().toString());
- waterGases.get(getAdapterPosition()).resId = R.drawable.ic_fp_water;
- waterGases.get(getAdapterPosition()).price = 0;
- waterGases.get(getAdapterPosition()).count = 0;
- waterGases.get(getAdapterPosition()).brandName = CustomApplication.getContext().getString(R.string.brand);
- toggleBtnMinusIcon(waterGases.get(getAdapterPosition()).count, btnMinus, 0);
- toggleTextPrice(true, txtPrice, sBrands);
- } else {
- Logc.e("c216--", "1");
- //Septian: fill data item product from local db
- WaterPricesDS waterPricesDS = new WaterPricesDS(CustomApplication.getContext());
- waterPricesDS.open();
- waterGases.get(getAdapterPosition()).brandId = waterPricesDS.getBrandIdOnName(sBrands.getSelectedItem().toString());
- waterGases.get(getAdapterPosition()).resId = R.drawable.ic_fp_water;
- waterGases.get(getAdapterPosition()).price = waterPricesDS.getPriceBrandOnName(sBrands.getSelectedItem().toString());
- waterGases.get(getAdapterPosition()).brandName = waterPricesDS.getNameBrandOnName(sBrands.getSelectedItem().toString());
- waterPricesDS.close();
- Log.e("dynamicProduct", "onItemSelected: id " + id
- + "\nposition" + position
- + "\nadapter position " + getAdapterPosition()
- + "\nbrandId " + waterGases.get(getAdapterPosition()).brandId
- + "\nprice " + waterGases.get(getAdapterPosition()).price
- + "\nbrandName " + waterGases.get(getAdapterPosition()).brandName);
- if (waterGases.get(getAdapterPosition()).count == 0)
- waterGases.get(getAdapterPosition()).count = 0;
- toggleBtnMinusIcon(waterGases.get(getAdapterPosition()).count, btnMinus, 0);
- toggleTextPrice(false, txtPrice, sBrands);
- }
- /*if (!waterGases.get(getAdapterPosition()).brandName.equals(CustomApplication.getContext().getString(R.string.brand))) {
- initializeRow();
- }*/
- fillRow(getAdapterPosition(), imgWaterGas, txtPrice, sBrands, txtCount, false);
- if (isUserAction || oldPosition == 0)
- updateTotalTransaction(true);
- validateRow(Integer.valueOf(txtCount.getTag().toString())
- , sBrands.getSelectedItemPosition()
- , txtCount
- , sBrands);
- isUserAction = true;
- oldPosition = position;
- }
- });
- txtCount.setTextColor(ContextCompat.getColor(activity.getApplicationContext()
- , R.color.black));
- }
- private void initializeFooter() {
- btnAddWater = (ImageButton) itemView.findViewById(R.id.btnAddWater);
- txtAddWater = (TextView) itemView.findViewById(R.id.tvAddWater);
- btnAddWater.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- onAddWaterGasClick(false);
- }
- });
- txtAddWater.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- onAddWaterGasClick(false);
- }
- });
- }
- //set item default selection
- void onAddWaterGasClick(boolean isInit) {
- /*if (isInit) {
- waterGases.add(new WaterGas(0, 0));
- waterGases.add(new WaterGas(0, 8));
- notifyItemInserted(waterGases.size() - 1);
- } else {
- waterGases.add(new WaterGas(0, -1));
- notifyItemInserted(waterGases.size() - 1);
- }*/
- waterGases.add(new WaterGas(0, -1));
- waterGases.add(new WaterGas(0, -1));
- notifyItemInserted(waterGases.size() - 1);
- }
- private void initiateSpinner(View view) {
- for (int i = 0; i < waterGases.size(); i++) {
- Log.e("dynamicProduct", "initiateSpinner: size " + waterGases.size()
- + "\nbrand name " + waterGases.get(i).brandName
- + "\nid " + waterGases.get(i).brandId);
- }
- sBrands = (com.rey.material.widget.Spinner) view.findViewById(R.id.sBrands);
- for (int i=0;i<waterGases.size();i++) {
- if (waterGases.get(i).brandName.equals(CustomApplication.getContext().getString(R.string.brand))) {
- Log.d("dynamicProduct", "initiateSpinner: #1");
- stringArray = GlobalFunction.getWaterPriceBrandNameAvailableVendors();
- stringArray[0] = CustomApplication.getContext().getString(R.string.brand);
- //Septian:default selection item product to brand 'Pilih Product'
- sBrands.setSelection(0);
- break;
- } else {
- Log.d("dynamicProduct", "initiateSpinner: #2");
- stringArray = GlobalFunction.getWaterPriceBrandNameAvailableVendorsByRanking(sBrands.getSelectedItem().toString());
- stringArray[0] = CustomApplication.getContext().getString(R.string.brand);
- break;
- }
- }
- ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(activity.getApplicationContext(),
- R.layout.simple_spinner_dropdown_item_custom_48, stringArray);
- sBrands.setAdapter(adapter);
- adapter.notifyDataSetChanged();
- isUserAction = false;
- if(sBrands.getSelectedItemPosition() == 0){
- ((TextView) sBrands.getSelectedView()).setTextColor(Color.GRAY);
- }
- toggleTextPrice(true, txtPrice, sBrands);
- }
- @Override
- public void onClick(View v) {
- if (v.equals(btnMinus))
- onBtnMinusClick(getAdapterPosition(), txtCount, sBrands, btnMinus);
- if (v.equals(btnPlus))
- onBtnPlusClick(getAdapterPosition(), txtCount, sBrands, btnMinus);
- if (v.equals(btnDel))
- onBtnDelClick(getAdapterPosition());
- }
- }
- private void validateRow(Integer currentTotal
- , int selectedItemPosition
- , TextView txtCount
- , com.rey.material.widget.Spinner sBrands) {
- txtCount.setTextColor(ContextCompat.getColor(activity.getApplicationContext(), R.color.black));
- if (selectedItemPosition == 0) {
- ((TextView) sBrands.getSelectedView()).setTextColor(Color.GRAY);
- } else {
- ((TextView) sBrands.getSelectedView()).setTextColor(Color.BLACK);
- }
- if (currentTotal > 0 && selectedItemPosition == 0) {
- ((TextView) sBrands.getSelectedView())
- .setTextColor(ContextCompat.getColor(activity.getApplicationContext(), R.color.red));
- } else {
- if (selectedItemPosition == 0) {
- ((TextView) sBrands.getSelectedView()).setTextColor(Color.GRAY);
- } else {
- ((TextView) sBrands.getSelectedView()).setTextColor(Color.BLACK);
- }
- }
- }
- private void onBtnDelClick(int position) {
- waterGases.remove(position);
- notifyItemRemoved(position);
- notifyItemRangeChanged(position, waterGases.size());
- }
- private void onBtnMinusClick(int adapterPosition, TextView txtCount, Spinner sBrands, ImageButton btnMinus) {
- if (waterGases.size() < adapterPosition + 1 || adapterPosition < 0)
- return;
- int currentCount = getTxtCountTag(txtCount);
- WaterGasObject item = new WaterGasObject(waterGases.get(adapterPosition).brandId);
- int minimumCount;
- Logc.e("c212--", String.valueOf(item.getUnitId()));
- Logc.e("c213--", "1");
- minimumCount = 0;
- if (currentCount >= minimumCount)
- currentCount--;
- txtCount.setTag(currentCount);
- setTxtCountText(txtCount);
- waterGases.get(adapterPosition).count = currentCount;
- validateRow(currentCount, sBrands.getSelectedItemPosition(), txtCount, sBrands);
- toggleBtnMinusIcon(currentCount, btnMinus, minimumCount);
- if (currentCount == minimumCount - 1)
- onBtnDelClick(adapterPosition);
- updateTotalTransaction(true);
- }
- private void updateTotalTransaction(boolean shouldUpdateText) {
- long total = 0;
- for (int i = 0; i < waterGases.size(); i++) {
- total += waterGases.get(i).count * waterGases.get(i).price;
- Logc.e("dynamicProduct", "size " + String.valueOf(waterGases.size())
- + "\ni " + String.valueOf(i)
- + "\ncount " + String.valueOf(waterGases.get(i).count)
- + "\nprice " + String.valueOf(waterGases.get(i).price)
- + "\nbrandName " + waterGases.get(i).brandName
- + "\ntotal " + String.valueOf(total));
- }
- Intent intent = new Intent("waterGasListUpdateIntent");
- intent.putExtra("total", total);
- if (shouldUpdateText)
- intent.putExtra("shouldUpdateText", shouldUpdateText);
- LocalBroadcastManager.getInstance(activity.getApplicationContext()).sendBroadcast(intent);
- }
- private void onBtnPlusClick(int adapterPosition, TextView txtCount, Spinner sBrands, ImageButton btnMinus) {
- if (adapterPosition < 0)
- return;
- int currentCount = getTxtCountTag(txtCount);
- /*Buka ini jika limit item sebanyak 6 item setiap order
- if (currentCount < 6)
- currentCount++;
- else
- Toast.makeText(CustomApplication.getContext(),
- CustomApplication.getContext().getString(R.string.max_order_is_six),Toast.LENGTH_SHORT).show();*/
- //Septian: getSelectedItemPosition will be return the position adapter in spinner
- //5 gonna be adapter position of lpg 3kg
- //when custom product been implemented, position of adapter cannot be predicted
- if (String.valueOf(sBrands.getSelectedItem()).contains("3kg")) {
- if (currentCount < 2) {
- currentCount++;
- } else {
- Toast.makeText(CustomApplication.getContext(),
- CustomApplication.getContext().getString(R.string.max_order_lpg3kg_is_two_a_day), Toast.LENGTH_SHORT)
- .show();
- }
- } else {
- currentCount++;
- }
- Log.e("tagItemLimit", "onBtnPlusClick: " + sBrands.getSelectedItemPosition() + " " + sBrands.getSelectedItem());
- toggleBtnMinusIcon(currentCount, btnMinus, 0);
- txtCount.setTag(currentCount);
- setTxtCountText(txtCount);
- waterGases.get(adapterPosition).count = currentCount;
- validateRow(currentCount, sBrands.getSelectedItemPosition(), txtCount, sBrands);
- updateTotalTransaction(true);
- }
- private void setTxtCountText(TextView txtCount) {
- int count = Integer.valueOf(txtCount.getTag().toString());
- if (count >= 0)
- txtCount.setText(GlobalFunction.fromHtmlCustom("<b>" + String.valueOf(count) + "</b>"));
- }
- private int getTxtCountTag(TextView txtCount) {
- return Integer.valueOf(txtCount.getTag().toString());
- }
- @Override
- public int getItemCount() {
- return waterGases.size() + 1; //for VIEW_TYPE_FOOTER
- }
- public WaterGas getItem(int position) {
- return waterGases.get(position);
- }
- @Override
- public WaterGasViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
- int res = R.layout.item_water_v2;
- if (viewType == VIEW_TYPE_FOOTER)
- //this gonna be layout representative of button add more item products
- res = R.layout.item_water_add;
- View v = LayoutInflater.from(viewGroup.getContext()).inflate(res, /*viewGroup*/ null, false);
- WaterGasViewHolder wvh = new WaterGasViewHolder(v);
- wvh.setViewType(viewType);
- wvh.initialize();
- return wvh;
- }
- @Override
- public void onBindViewHolder(final WaterGasViewHolder waterGasViewHolder, final int i) {
- if (waterGasViewHolder.viewType == VIEW_TYPE_ROW) {
- fillRow(i, waterGasViewHolder.imgWaterGas, waterGasViewHolder.txtPrice, waterGasViewHolder.sBrands
- , waterGasViewHolder.txtCount, true);
- toggleBtnMinusIcon(waterGases.get(i).count, waterGasViewHolder.btnMinus, 0);
- }
- //noinspection StatementWithEmptyBody
- if (waterGasViewHolder.viewType == VIEW_TYPE_FOOTER) {
- Handler handler = new Handler();
- final Runnable r = new Runnable() {
- public void run() {
- Logc.e("c144--", "1");
- if (waterGases.size() == 0 && isInit) {
- Logc.e("c144.1--", "1");
- isInit = false;
- waterGasViewHolder.onAddWaterGasClick(true);
- }
- }
- };
- handler.post(r);
- }
- }
- private void toggleTextPrice(boolean b, TextView txtPrice, Spinner sBrands) {
- if (b) {
- txtPrice.setVisibility(View.GONE);
- sBrands.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
- } else {
- txtPrice.setVisibility(View.VISIBLE);
- txtPrice.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
- sBrands.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100));
- }
- txtPrice.requestLayout();
- sBrands.requestLayout();
- }
- private void toggleBtnMinusIcon(int count, ImageButton btnMinus, int minimumCount) {
- if (count <= minimumCount) {
- btnMinus.setImageResource(R.drawable.ic_del_yellow);
- } else {
- btnMinus.setImageResource(R.drawable.ic_minus_yellow);
- }
- }
- private void fillRow(int adapterPosition
- , ImageView imgWaterGas
- , TextView txtPrice
- , Spinner sBrands
- , TextView txtCount
- , boolean shouldSelectSBrands) {
- imgWaterGas.setImageResource(waterGases.get(adapterPosition).resId);
- long price = waterGases.get(adapterPosition).price;
- txtPrice.setText((price == 0) ? CustomApplication.getContext().getString(R.string.rp_zero)
- : GlobalFunction.formatTotal(price));
- /*if (shouldSelectSBrands)
- sBrands.setSelection(((ArrayAdapter) sBrands.getAdapter()).getPosition(
- new BrandsLabel().getName(waterGases.get(adapterPosition).brandId)
- ));
- txtCount.setTag(waterGases.get(adapterPosition).count);
- setTxtCountText(txtCount);*/
- }
- @Override
- public int getItemViewType(int position) {
- return (position == waterGases.size()) ? VIEW_TYPE_FOOTER : VIEW_TYPE_ROW;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment