cl.smartcities.isci.transportinspector.adapters.dialogAdapters.BusSelectionAdapter.java Source code

Java tutorial

Introduction

Here is the source code for cl.smartcities.isci.transportinspector.adapters.dialogAdapters.BusSelectionAdapter.java

Source

package cl.smartcities.isci.transportinspector.adapters.dialogAdapters;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cl.smartcities.isci.transportinspector.R;
import cl.smartcities.isci.transportinspector.backend.Bus;
import cl.smartcities.isci.transportinspector.drawables.DropDownButton;

public class BusSelectionAdapter extends ArrayAdapter<String> {
    private LayoutInflater inflater;
    private List<String> serviceList;
    private HashMap<String, ArrayList<Bus>> busMap;
    private DropDownButton activeDropdown;
    private DropDownButton inactiveDropdown;
    private int activeHolderPosition = -1;
    private ListViewAdapterListener listener;
    private ViewHolder activeHolder = null;

    public BusSelectionAdapter(Context context, ArrayList<String> serviceList,
            HashMap<String, ArrayList<Bus>> busMap, ListViewAdapterListener listener) {
        super(context, R.layout.bus_selection_dialog_bus_row, serviceList);
        this.serviceList = serviceList;
        this.listener = listener;
        this.busMap = busMap;
        this.activeDropdown = new DropDownButton(getContext());
        this.activeDropdown.setActive();
        this.inactiveDropdown = new DropDownButton(getContext());
        this.inactiveDropdown.setInactive(R.color.even_row, R.color.odd_row);
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return serviceList.size();
    }

    /* set the view for the adapter */
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        /* set view */
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.bus_selection_dialog_bus_row, parent, false);
            holder = new ViewHolder();

            /* initialize holder */
            holder.busRoute = (TextView) convertView.findViewById(R.id.bus_route);
            holder.licensePlate1 = (TextView) convertView.findViewById(R.id.license_plate_1);
            holder.licencePlate2 = (TextView) convertView.findViewById(R.id.license_plate_2);
            holder.doNotKnowIcon = (TextView) convertView.findViewById(R.id.do_not_know_icon);
            holder.doNotKnow = (LinearLayout) convertView.findViewById(R.id.do_not_know_layout);
            holder.busImage = (ImageView) convertView.findViewById(R.id.bus_image);
            holder.dropDownButton = (ImageButton) convertView.findViewById(R.id.button);
            holder.platesLayout = convertView.findViewById(R.id.bus_options);
            holder.headerLayout = convertView.findViewById(R.id.route_info);

            /* get and set typefaces */
            Typeface iconFont = Typeface.createFromAsset(this.getContext().getAssets(),
                    this.getContext().getString(R.string.icon_font));
            Typeface robotoBold = Typeface.createFromAsset(this.getContext().getAssets(),
                    this.getContext().getString(R.string.roboto_bold_font));
            holder.doNotKnowIcon.setTypeface(iconFont);
            holder.setHolderTypefaceBold(robotoBold);
            holder.position = position;

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            if (activeHolder != null && holder.position == activeHolder.position) {
                activeHolder = null;
            }
        }

        holder.platesLayout.setVisibility(View.GONE);

        setUpNormalRow(holder, position);

        setBackgroundColor(holder, position);

        return convertView;

    }

    private void setUpNormalRow(final ViewHolder holder, final int position) {
        final String service = this.serviceList.get(position);
        final ArrayList<Bus> sameServiceBusList = this.busMap.get(service);
        final Drawable busImage = ContextCompat.getDrawable(this.getContext(),
                sameServiceBusList.get(0).getBusIcon());

        holder.optionsLayout = holder.platesLayout;

        holder.busImage.setVisibility(View.VISIBLE);
        holder.busRoute.setVisibility(View.VISIBLE);
        holder.dropDownButton.setVisibility(View.VISIBLE);

        holder.busRoute.setText(service);
        holder.busImage.setImageDrawable(busImage);

        /* case: only one bus of this service is coming */
        if (sameServiceBusList.size() == 1) {
            holder.dropDownButton.setVisibility(View.GONE);
            holder.busRoute.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bus bus = sameServiceBusList.get(0);
                    bus.addPassenger();
                    Log.d("Notification dialog", "Added me to the passenger number");
                    listener.onPositiveClick(bus);
                }
            });
        } else { /* case: two buses of this service are coming */
            String plate1 = sameServiceBusList.get(0).getFormattedPlate();
            String plate2 = sameServiceBusList.get(1).getFormattedPlate();

            holder.licensePlate1.setText(plate1);
            holder.licencePlate2.setText(plate2);

            setUpBusOptionsLayout(holder, position);

            /* set every option to send its own plate */
            holder.licensePlate1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Add 1 to the passenger number
                    Bus bus = sameServiceBusList.get(0);
                    bus.addPassenger();
                    Log.d("Notification dialog", "Added me to the passenger number");
                    listener.onPositiveClick(bus);
                }
            });
            holder.licencePlate2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Add 1 to the passenger number
                    Bus bus = sameServiceBusList.get(1);
                    bus.addPassenger();
                    Log.d("Notification dialog", "Added me to the passenger number");

                    listener.onPositiveClick(bus);
                }
            });
            /* if the plate is not known raise false alarm */
            holder.doNotKnow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onPositiveClick(new Bus(sameServiceBusList.get(0).getService(), ""));
                }
            });
        }
    }

    private void setBackgroundColor(final ViewHolder holder, final int position) {
        int backGroundColor;
        int dropDownColor;
        if (position % 2 == 1) {
            backGroundColor = ContextCompat.getColor(this.getContext(), R.color.odd_row);
            dropDownColor = ContextCompat.getColor(this.getContext(), R.color.odd_drop_down);
        } else {
            backGroundColor = ContextCompat.getColor(this.getContext(), R.color.even_row);
            dropDownColor = ContextCompat.getColor(this.getContext(), R.color.odd_drop_down);
        }

        holder.headerLayout.setBackgroundColor(backGroundColor);
        holder.optionsLayout.setBackgroundColor(dropDownColor);
    }

    private void setUpBusOptionsLayout(final ViewHolder holder, final int position) {
        holder.dropDownButton.setVisibility(View.VISIBLE);
        holder.dropDownButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.optionsLayout.getVisibility() == View.VISIBLE) {
                    holder.optionsLayout.setVisibility(View.GONE);
                    activeHolderPosition = -1;
                    activeHolder = null;
                    ((ImageButton) v).setImageDrawable(activeDropdown);
                } else {
                    holder.optionsLayout.setVisibility(View.VISIBLE);
                    ((ImageButton) v).setImageDrawable(inactiveDropdown);
                    if (activeHolder != null) {
                        hideOptionsLayout(activeHolder);
                    }
                    activeHolderPosition = position;
                    activeHolder = holder;
                }
            }
        });

        if (!isShown(position)) {
            holder.dropDownButton.setImageDrawable(this.activeDropdown);
            holder.platesLayout.setVisibility(View.GONE);
        } else {
            holder.dropDownButton.setImageDrawable(this.inactiveDropdown);
            holder.optionsLayout.setVisibility(View.VISIBLE);
        }
    }

    private boolean isShown(int position) {
        return activeHolderPosition == position;
    }

    private void hideOptionsLayout(ViewHolder holder) {
        holder.platesLayout.setVisibility(View.GONE);
        holder.dropDownButton.setImageDrawable(activeDropdown);
    }

    /* ViewHolder to hold parameters */
    private static class ViewHolder extends EditableViewHolder {
        TextView busRoute;
        ImageView busImage;
        ImageButton dropDownButton;
        TextView licensePlate1;
        TextView licencePlate2;
        LinearLayout doNotKnow;
        TextView doNotKnowIcon;
        View optionsLayout;
        View headerLayout;
        View platesLayout;
        int position;

        @Override
        public void setHolderTypefaceRegular(Typeface font) {

        }

        @Override
        public void setHolderTypefaceBold(Typeface font) {
            setFont(font, licensePlate1, licencePlate2, busRoute);
        }

        @Override
        public void setHolderIconFont(Typeface font) {

        }
    }

    public interface ListViewAdapterListener {
        void onPositiveClick(Bus bus);

        void onNegativeClick();
    }
}