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

Java tutorial

Introduction

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

Source

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

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cl.smartcities.isci.transportinspector.R;
import cl.smartcities.isci.transportinspector.TranSappApplication;
import cl.smartcities.isci.transportinspector.backend.Event;
import cl.smartcities.isci.transportinspector.serverConnection.ServerConnectionController;
import cl.smartcities.isci.transportinspector.serverConnection.ServerResponseListener;
import cl.smartcities.isci.transportinspector.serverConnection.requests.pushRequests.BusEventRequest;
import cl.smartcities.isci.transportinspector.serverConnection.requests.pushRequests.BusStopEventRequest;
import cl.smartcities.isci.transportinspector.serverConnection.requests.pushRequests.EventRequest;

/**
 * Created by David on 28-10-2015.
 * Modified bt aantoine on 14-06-2016
 */
public class EventAdapter extends MultiStateArrayAdapter<Event, EventAdapter.State>
        implements ServerResponseListener<ArrayList<Event>> {

    AlertDialog alertDialog;
    Context context;
    List<Event> events;
    String code;
    String title;
    String type;
    LayoutInflater mInflater;

    private final String TAG = "EventAdapter";

    @Override
    protected State getDefaultState() {
        return State.BOTH;
    }

    public enum State {
        ONLY_CONFIRM, ONLY_DECLINE, BOTH
    }

    /**
     * Constructor for the event adapter
     *
     * @param pContext the context
     * @param pCode    The code to be sent to the server, if a Bus, the code is the license plate
     *                 if a BusStop, the code is the busStop code
     * @param pTitle   The title to be sent to the server, if a Bus, it's the service of the bus.
     *                 BusStop does not use it
     * @param pType    Parameter to see if the events are from a bus or from a busStop
     * @param pEvents  List of Events to show
     */
    public EventAdapter(Context pContext, String pCode, String pTitle, String pType, List<Event> pEvents) {
        super(pContext, R.layout.event_layout_row, pEvents);
        this.events = pEvents == null ? new ArrayList<Event>() : pEvents;
        this.context = pContext;
        this.code = pCode;
        this.title = pTitle;
        this.type = pType;
        this.mInflater = (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.event_layout_row, parent, false);
            holder = new ViewHolder();
            holder.event_icon = (ImageView) convertView.findViewById(R.id.event_icon);
            holder.description = (TextView) convertView.findViewById(R.id.description);
            holder.time = (TextView) convertView.findViewById(R.id.event_time);
            holder.yes_counter = (TextView) convertView.findViewById(R.id.yes_counter);
            holder.no_counter = (TextView) convertView.findViewById(R.id.no_counter);
            holder.yesIcon = (ImageButton) convertView.findViewById(R.id.yes_icon);
            holder.noIcon = (ImageButton) convertView.findViewById(R.id.no_icon);
            holder.true_text = (TextView) convertView.findViewById(R.id.true_text);
            holder.false_text = (TextView) convertView.findViewById(R.id.false_text);
            holder.ticket_font_green = (TextView) convertView.findViewById(R.id.ticket_font_green);
            holder.ticket_font_red = (TextView) convertView.findViewById(R.id.ticket_font_red);
            holder.confirm_button_layout = (RelativeLayout) convertView.findViewById(R.id.confirm_button_layout);
            holder.decline_button_layout = (RelativeLayout) convertView.findViewById(R.id.decline_button_layout);
            Typeface robotoRegular = Typeface.createFromAsset(this.getContext().getAssets(),
                    this.context.getString(R.string.roboto_regular_font));
            Typeface robotoBold = Typeface.createFromAsset(this.getContext().getAssets(),
                    this.context.getString(R.string.roboto_bold_font));
            Typeface iconFont = Typeface.createFromAsset(this.getContext().getAssets(),
                    this.context.getString(R.string.icon_font));
            holder.setHolderTypefaceRegular(robotoRegular);
            holder.setHolderTypefaceBold(robotoBold);
            holder.setHolderIconFont(iconFont);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Event event = this.events.get(position);

        //Descripcion del evento
        String description = this.events.get(position).getDescription();
        holder.description.setText(description);

        //Time del evento
        String date = this.events.get(position).getTime();
        holder.time.setText(date);

        //Confirmaciones del evento
        String confirms = this.events.get(position).getConfirms() + "";
        holder.yes_counter.setText(confirms);

        //Negaciones del evento
        String declines = this.events.get(position).getDeclines() + "";
        holder.no_counter.setText(declines);

        // Event icon
        int resDrawableId = this.events.get(position).getResDrawableId();
        holder.event_icon.setImageResource(resDrawableId);

        //id del evento
        final String id = this.events.get(position).getId();

        final EventRequest eventRequest;

        if (this.type.equals("Bus")) {
            eventRequest = new BusEventRequest(this.code, this.title, id);
        } else {
            eventRequest = new BusStopEventRequest(this.code, id);
        }

        holder.decline_button_layout.setVisibility(View.VISIBLE);
        holder.decline_button_layout.setVisibility(View.VISIBLE);
        if (getLastStateAccordingToPosition(position).equals(State.ONLY_CONFIRM)) {
            holder.decline_button_layout.setVisibility(View.GONE);
        }
        if (getLastStateAccordingToPosition(position).equals(State.ONLY_DECLINE)) {
            holder.confirm_button_layout.setVisibility(View.GONE);
        }

        //Listener de eventos
        holder.yesIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> attributes = new HashMap<>();
                attributes.put("Event name", event.getName());
                attributes.put("BusStop/Service Code", code);
                TranSappApplication.addFabricLog("Event confirmed", attributes);

                setCurrentState(position, State.ONLY_CONFIRM);
                confirmEvent(holder, eventRequest, event);
                holder.decline_button_layout.setVisibility(View.GONE);
                holder.ticket_font_green.setVisibility(View.VISIBLE);
            }
        });
        holder.noIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, String> attributes = new HashMap<>();
                attributes.put("Event name", event.getName());
                attributes.put("BusStop/Service Code", code);
                TranSappApplication.addFabricLog("Event denied", attributes);

                setCurrentState(position, State.ONLY_DECLINE);
                denyEvent(holder, eventRequest, event);
                holder.confirm_button_layout.setVisibility(View.GONE);
                holder.ticket_font_red.setVisibility(View.VISIBLE);
            }
        });

        /* save holder onStopListener convertView */
        if (position % 2 == 0) {
            convertView.setBackgroundColor(ContextCompat.getColor(context, R.color.even_row));
        } else {
            convertView.setBackgroundColor(ContextCompat.getColor(context, R.color.odd_row));
        }
        return convertView;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public void setDialog(AlertDialog dialog) {
        this.alertDialog = dialog;
    }

    public void sendRequest(EventRequest request) {
        ServerConnectionController.sendReportRequest(request, this, null);
    }

    /**
     * Method called if an event is confirmed
     *
     * @param holder       The View Holder
     * @param eventRequest The request to be done to the server
     * @param event        The event being sent, used to update the confirms
     */
    private void confirmEvent(ViewHolder holder, EventRequest eventRequest, Event event) {
        int yesAmount = Integer.parseInt((String) holder.yes_counter.getText());
        holder.yes_counter.setText((yesAmount + 1) + "");
        holder.yesIcon.setOnClickListener(null);
        holder.noIcon.setOnClickListener(null);
        eventRequest.setRequestToConfirm();
        event.updateConfirms();
        sendRequest(eventRequest);
    }

    /**
     * Method calles if an event is declined
     *
     * @param holder       The View Holder
     * @param eventRequest The request to be done to the server
     * @param event        The event being sent, used to update the declines
     */
    private void denyEvent(ViewHolder holder, EventRequest eventRequest, Event event) {
        int noAmount = Integer.parseInt(holder.no_counter.getText().toString());
        holder.no_counter.setText((noAmount + 1) + "");
        holder.yesIcon.setOnClickListener(null);
        holder.noIcon.setOnClickListener(null);
        eventRequest.setRequestToDecline();
        event.updateDeclines();
        sendRequest(eventRequest);
    }

    @Override
    public void callback(ArrayList<Event> response) {

    }

    @Override
    public String getToken() {
        return TAG;
    }

    /* ViewHolder to hold parameters */
    private static class ViewHolder extends EditableViewHolder {
        ImageView event_icon;
        ImageButton yesIcon;
        ImageButton noIcon;
        TextView description;
        TextView time;
        TextView yes_counter;
        TextView no_counter;
        TextView true_text;
        TextView false_text;
        TextView ticket_font_green;
        TextView ticket_font_red;
        RelativeLayout confirm_button_layout;
        RelativeLayout decline_button_layout;

        @Override
        public void setHolderTypefaceRegular(Typeface font) {
            setFont(font, time, true_text, false_text, yes_counter, no_counter);
        }

        @Override
        public void setHolderTypefaceBold(Typeface font) {
            setFont(font, description);
        }

        @Override
        public void setHolderIconFont(Typeface font) {
            setFont(font, ticket_font_green, ticket_font_red);
        }
    }
}