|
| 1 | +package org.fossasia.fossasiaorgaandroidapp.Adapters; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.DialogInterface; |
| 6 | +import android.graphics.Color; |
| 7 | +import android.support.v4.content.ContextCompat; |
| 8 | +import android.support.v7.app.AlertDialog; |
| 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.Button; |
| 15 | +import android.widget.TextView; |
| 16 | + |
| 17 | +import com.android.volley.VolleyError; |
| 18 | +import com.google.gson.Gson; |
| 19 | +import com.google.gson.internal.bind.ArrayTypeAdapter; |
| 20 | + |
| 21 | +import org.fossasia.fossasiaorgaandroidapp.Api.ApiCall; |
| 22 | +import org.fossasia.fossasiaorgaandroidapp.Api.LoginCall; |
| 23 | +import org.fossasia.fossasiaorgaandroidapp.Interfaces.VolleyCallBack; |
| 24 | +import org.fossasia.fossasiaorgaandroidapp.R; |
| 25 | +import org.fossasia.fossasiaorgaandroidapp.Utils.Constants; |
| 26 | +import org.fossasia.fossasiaorgaandroidapp.model.AttendeeDetails; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +/** |
| 33 | + * Created by rishabhkhanna on 27/04/17. |
| 34 | + */ |
| 35 | + |
| 36 | +public class AttendeeListAdapter extends RecyclerView.Adapter<AttendeeListAdapter.AttendeeListAdapterHolder> { |
| 37 | + ArrayList<AttendeeDetails> attendeeDetailses; |
| 38 | + Activity activity; |
| 39 | + long id; |
| 40 | + public static final String TAG = "AttendeeAdapter"; |
| 41 | + |
| 42 | + public AttendeeListAdapter(ArrayList<AttendeeDetails> attendeeDetailses, Activity activity, long id) { |
| 43 | + this.attendeeDetailses = attendeeDetailses; |
| 44 | + this.activity = activity; |
| 45 | + this.id = id; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public AttendeeListAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 50 | + LayoutInflater li = activity.getLayoutInflater(); |
| 51 | + View itemView = li.inflate(R.layout.attendee_layout,null); |
| 52 | + |
| 53 | + AttendeeListAdapterHolder attendeeListAdapterHolder = new AttendeeListAdapterHolder(itemView); |
| 54 | + return attendeeListAdapterHolder; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onBindViewHolder(final AttendeeListAdapterHolder holder, final int position) { |
| 59 | + final AttendeeDetails thisAttendee = attendeeDetailses.get(position); |
| 60 | + holder.tvLastName.setText(thisAttendee.getLastname()); |
| 61 | + holder.tvFirstName.setText(thisAttendee.getFirstname()); |
| 62 | + holder.tvEmail.setText(thisAttendee.getEmail()); |
| 63 | + if(thisAttendee.getCheckedIn()) { |
| 64 | + holder.btnCheckedIn.setText("Checked In"); |
| 65 | + holder.btnCheckedIn.setBackgroundColor(ContextCompat.getColor(activity,android.R.color.holo_green_dark)); |
| 66 | + }else{ |
| 67 | + holder.btnCheckedIn.setText("Check In"); |
| 68 | + holder.btnCheckedIn.setBackgroundColor(ContextCompat.getColor(activity,android.R.color.holo_red_light)); |
| 69 | + } |
| 70 | + holder.btnCheckedIn.setOnClickListener(new View.OnClickListener() { |
| 71 | + @Override |
| 72 | + public void onClick(View v) { |
| 73 | + Log.d(TAG, "onClick: alert builder click"); |
| 74 | + AlertDialog.Builder builder = new AlertDialog.Builder(activity); |
| 75 | + String alertTitle = ""; |
| 76 | + if(thisAttendee.getCheckedIn()){ |
| 77 | + alertTitle = Constants.AttendeeCheckingOut; |
| 78 | + }else{ |
| 79 | + alertTitle = Constants.attendeeChechingIn; |
| 80 | + } |
| 81 | + builder.setTitle(alertTitle).setMessage(thisAttendee.getFirstname() + " " |
| 82 | + + thisAttendee.getLastname() + "\n" |
| 83 | + + "Ticket: " + thisAttendee.getTicket().getType() ) |
| 84 | + .setPositiveButton("OK", new DialogInterface.OnClickListener() { |
| 85 | + @Override |
| 86 | + public void onClick(DialogInterface dialog, int which) { |
| 87 | + Log.d(TAG, "onClick: inside ok"); |
| 88 | + changeCheckStatus(activity,thisAttendee,holder.btnCheckedIn,attendeeDetailses,position); |
| 89 | + } |
| 90 | + }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { |
| 91 | + @Override |
| 92 | + public void onClick(DialogInterface dialog, int which) { |
| 93 | + |
| 94 | + } |
| 95 | + }); |
| 96 | + builder.create(); |
| 97 | + builder.show(); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public int getItemCount() { |
| 105 | + return attendeeDetailses.size(); |
| 106 | + } |
| 107 | + |
| 108 | + public class AttendeeListAdapterHolder extends RecyclerView.ViewHolder{ |
| 109 | + |
| 110 | + TextView tvLastName; |
| 111 | + TextView tvFirstName; |
| 112 | + TextView tvEmail; |
| 113 | + Button btnCheckedIn; |
| 114 | + |
| 115 | + public AttendeeListAdapterHolder(View itemView) { |
| 116 | + super(itemView); |
| 117 | + tvLastName = (TextView) itemView.findViewById(R.id.tvLastName); |
| 118 | + tvFirstName = (TextView) itemView.findViewById(R.id.tvFirstName); |
| 119 | + tvEmail = (TextView) itemView.findViewById(R.id.tvEmail); |
| 120 | + btnCheckedIn = (Button) itemView.findViewById(R.id.btnCheckedin); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public void changeCheckStatus(Activity activity, AttendeeDetails thisAttendee, final Button btnCheckedIn, final ArrayList<AttendeeDetails> attendeeDetailses, final int position){ |
| 125 | + ApiCall.PostApiCall(activity, Constants.eventDetails +id + Constants.attendeesToggle + thisAttendee.getId(), new VolleyCallBack() { |
| 126 | + @Override |
| 127 | + public void onSuccess(String result) { |
| 128 | + Log.d(TAG, "onSuccess: " + result); |
| 129 | + Gson gson = new Gson(); |
| 130 | + AttendeeDetails newattendeeDetailses = gson.fromJson(result,AttendeeDetails.class); |
| 131 | + attendeeDetailses.set(position , newattendeeDetailses); |
| 132 | + notifyDataSetChanged(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void onError(VolleyError error) { |
| 137 | + |
| 138 | + } |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments