|
| 1 | +package dpi.ks19.participantapp.Adapter; |
| 2 | + |
| 3 | +import android.app.ProgressDialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.drawable.ColorDrawable; |
| 7 | +import android.graphics.drawable.Drawable; |
| 8 | +import android.support.annotation.NonNull; |
| 9 | +import android.support.constraint.ConstraintLayout; |
| 10 | +import android.support.v7.widget.LinearLayoutManager; |
| 11 | +import android.support.v7.widget.RecyclerView; |
| 12 | +import android.view.LayoutInflater; |
| 13 | +import android.view.View; |
| 14 | +import android.view.ViewGroup; |
| 15 | +import android.widget.ImageView; |
| 16 | +import android.widget.TextView; |
| 17 | +import android.widget.Toast; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | + |
| 21 | +import dpi.ks19.participantapp.CallbackInterface.ScheduleInterface; |
| 22 | +import dpi.ks19.participantapp.Fragments.DayOneFrag; |
| 23 | +import dpi.ks19.participantapp.Fragments.DayThreeFrag; |
| 24 | +import dpi.ks19.participantapp.Fragments.DayTwoFrag; |
| 25 | +import dpi.ks19.participantapp.Model.EventClass; |
| 26 | +import dpi.ks19.participantapp.R; |
| 27 | + |
| 28 | +public class ClusterAdapter extends RecyclerView.Adapter<ClusterAdapter.ClusterHolder> implements ScheduleInterface { |
| 29 | + //Cluster Adapter for Cluster RecyclerView. |
| 30 | + ArrayList<Drawable> clusterIcon = new ArrayList<>(); |
| 31 | + Context mContext; |
| 32 | + ProgressDialog progressDialog; |
| 33 | + int day; |
| 34 | + private ClusterAdapter callback; |
| 35 | + private String cluster[]; |
| 36 | + |
| 37 | + public ClusterAdapter(Context mContext, int d) { //Constructor initializes mContext, callback and data for clusters as it is Static. |
| 38 | + this.mContext = mContext; |
| 39 | + this.callback = this; |
| 40 | + cluster = mContext.getResources().getStringArray(R.array.cluster_list); |
| 41 | + day = d; |
| 42 | + |
| 43 | + progressDialog = new ProgressDialog(mContext); //Initializes progress Dialog When loading for data. |
| 44 | + progressDialog.setTitle("Loading..."); |
| 45 | + progressDialog.setMessage("Downloading Data.....Please Wait"); |
| 46 | + progressDialog.setCancelable(false); |
| 47 | + progressDialog.setCanceledOnTouchOutside(false); |
| 48 | + progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); |
| 49 | + progressDialog.setIndeterminate(true); |
| 50 | + |
| 51 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#ffffff"))); |
| 52 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#ffff00"))); |
| 53 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#ff00ff"))); |
| 54 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#00ffff"))); |
| 55 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#ff0000"))); |
| 56 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#00ff00"))); |
| 57 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#0000ff"))); |
| 58 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#ccccff"))); |
| 59 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#000000"))); |
| 60 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#2cd84a"))); |
| 61 | + clusterIcon.add(new ColorDrawable(Color.parseColor("#c34af7"))); |
| 62 | + } |
| 63 | + |
| 64 | + @NonNull |
| 65 | + @Override |
| 66 | + public ClusterHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 67 | + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cluster_card_schedule, parent, false); |
| 68 | + return new ClusterHolder(view); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onBindViewHolder(@NonNull final ClusterHolder holder, final int listPosition) { |
| 73 | + final String clusterName = cluster[listPosition]; |
| 74 | + |
| 75 | + holder.clusterName.setText(clusterName); |
| 76 | + holder.clusterIcon.setImageDrawable(clusterIcon.get(listPosition)); |
| 77 | + holder.rootLayout.setOnClickListener(new View.OnClickListener() { |
| 78 | + @Override |
| 79 | + public void onClick(View v) { |
| 80 | + progressDialog.show(); |
| 81 | + if (day == 22) |
| 82 | + DayOneFrag.getInstance().getSchedule(getQueryWord(clusterName), callback, holder); |
| 83 | + else if (day == 23) |
| 84 | + DayTwoFrag.getInstance().getSchedule(getQueryWord(clusterName), callback, holder); |
| 85 | + else |
| 86 | + DayThreeFrag.getInstance().getSchedule(getQueryWord(clusterName), callback, holder); |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void onViewDetachedFromWindow(@NonNull ClusterHolder holder) { |
| 93 | + super.onViewDetachedFromWindow(holder); //Hides the Schedule if the item is detached. |
| 94 | + holder.hideRecyclerView(); |
| 95 | + } |
| 96 | + |
| 97 | + private String getQueryWord(String clusterName) { |
| 98 | + //Returns the query word. |
| 99 | + if (clusterName.equals(cluster[0])) { |
| 100 | + return cluster[0]; |
| 101 | + } else if (clusterName.equals(cluster[1])) { |
| 102 | + return cluster[1]; |
| 103 | + } else if (clusterName.equals(cluster[2])) { |
| 104 | + return cluster[2]; |
| 105 | + } else if (clusterName.equals(cluster[3])) { |
| 106 | + return cluster[3]; |
| 107 | + } else if (clusterName.equals(cluster[4])) { |
| 108 | + return cluster[4]; |
| 109 | + } else if (clusterName.equals(cluster[5])) { |
| 110 | + return cluster[5]; |
| 111 | + } else if (clusterName.equals(cluster[6])) { |
| 112 | + return cluster[6]; |
| 113 | + } else if (clusterName.equals(cluster[7])) { |
| 114 | + return cluster[7]; |
| 115 | + } else if (clusterName.equals(cluster[8])) { |
| 116 | + return cluster[8]; |
| 117 | + } else if (clusterName.equals(cluster[9])) { |
| 118 | + return cluster[9]; |
| 119 | + } else if (clusterName.equals(cluster[10])) { |
| 120 | + return cluster[10]; |
| 121 | + } else if (clusterName.equals(cluster[11])) { |
| 122 | + return cluster[11]; |
| 123 | + } else { |
| 124 | + return clusterName; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + @Override |
| 130 | + public int getItemCount() { |
| 131 | + return clusterIcon.size(); |
| 132 | + } //Gets the total number of items. |
| 133 | + |
| 134 | + @Override |
| 135 | + public void callback(String querySchedule) { |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void setScheduleData(ArrayList<EventClass> data, ClusterHolder clusterHolder, boolean isEmpty) { //To set Schedule data |
| 141 | + if (isEmpty) { |
| 142 | + progressDialog.dismiss(); |
| 143 | + Toast.makeText(mContext, "Sorry! Unfortunate Error occurred.", Toast.LENGTH_SHORT).show(); |
| 144 | + } else { |
| 145 | + clusterHolder.setRecyclerView(data); //To set recycler view for schedule. |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public class ClusterHolder extends RecyclerView.ViewHolder { |
| 150 | + ConstraintLayout rootLayout; |
| 151 | + TextView clusterName; |
| 152 | + ImageView clusterIcon; |
| 153 | + RecyclerView scheduleRecyclerView; |
| 154 | + ScheduleAdapter adapter; |
| 155 | + |
| 156 | + public ClusterHolder(@NonNull View itemView) { //Holder for cluster item which holds the current recycler view in use. |
| 157 | + super(itemView); |
| 158 | + rootLayout = itemView.findViewById(R.id.root_layout); |
| 159 | + clusterName = itemView.findViewById(R.id.clusterName); |
| 160 | + clusterIcon = itemView.findViewById(R.id.clusterIcon); |
| 161 | + scheduleRecyclerView = itemView.findViewById(R.id.ScheduleRecyclerView); |
| 162 | + adapter = new ScheduleAdapter(mContext); |
| 163 | + scheduleRecyclerView.setLayoutManager(new LinearLayoutManager(itemView.getContext())); |
| 164 | + scheduleRecyclerView.setAdapter(adapter); |
| 165 | + } |
| 166 | + |
| 167 | + public void setRecyclerView(ArrayList<EventClass> data) { //Sets the data, make the schedules visible and dismisses the AlertDialog. |
| 168 | + scheduleRecyclerView.setVisibility(View.VISIBLE); |
| 169 | + adapter.setData(data); |
| 170 | + progressDialog.dismiss(); |
| 171 | + } |
| 172 | + |
| 173 | + public void hideRecyclerView() { //Hides the recyclerView. |
| 174 | + scheduleRecyclerView.setVisibility(View.GONE); |
| 175 | + } |
| 176 | + |
| 177 | + } |
| 178 | +} |
| 179 | + |
0 commit comments