1 |
efrain |
1 |
package com.cesams.twogetskills.adapter;
|
|
|
2 |
|
|
|
3 |
import android.content.Context;
|
|
|
4 |
import android.util.Log;
|
|
|
5 |
import android.view.LayoutInflater;
|
|
|
6 |
import android.view.View;
|
|
|
7 |
import android.view.ViewGroup;
|
|
|
8 |
import android.widget.ImageView;
|
|
|
9 |
import android.widget.ProgressBar;
|
|
|
10 |
import android.widget.TextView;
|
|
|
11 |
|
|
|
12 |
import androidx.recyclerview.widget.RecyclerView;
|
|
|
13 |
|
|
|
14 |
import com.bumptech.glide.Glide;
|
|
|
15 |
import com.bumptech.glide.ListPreloader;
|
|
|
16 |
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
|
|
17 |
import com.bumptech.glide.load.model.GlideUrl;
|
|
|
18 |
import com.bumptech.glide.load.model.LazyHeaders;
|
|
|
19 |
import com.bumptech.glide.request.RequestOptions;
|
|
|
20 |
import com.bumptech.glide.util.FixedPreloadSizeProvider;
|
|
|
21 |
import com.cesams.twogetskills.Constants;
|
|
|
22 |
import com.cesams.twogetskills.R;
|
|
|
23 |
import com.cesams.twogetskills.entity.Topic;
|
|
|
24 |
import com.cesams.twogetskills.library.MD5;
|
|
|
25 |
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
import java.text.DecimalFormat;
|
|
|
30 |
import java.util.Calendar;
|
|
|
31 |
import java.util.List;
|
|
|
32 |
import java.util.ArrayList;
|
|
|
33 |
import java.util.Random;
|
|
|
34 |
import java.util.TimeZone;
|
|
|
35 |
|
|
|
36 |
public class TopicListViewAdapter extends RecyclerView.Adapter<TopicListViewAdapter.ViewHolder> {
|
|
|
37 |
private final static String TAG = "C2GS - TopicAdapter";
|
|
|
38 |
private List<Topic> mData;
|
|
|
39 |
private LayoutInflater mInflater;
|
|
|
40 |
private ItemClickListener mClickListener;
|
|
|
41 |
private Context mContext;
|
|
|
42 |
private ITwoGetSkills iTwoGetSkills;
|
|
|
43 |
private DecimalFormat mDecimalFormat;
|
|
|
44 |
private final int imageWidthPixels = 1024;
|
|
|
45 |
private final int imageHeightPixels = 768;
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
// data is passed into the constructor
|
|
|
49 |
public TopicListViewAdapter(Context context, List<Topic> data) {
|
|
|
50 |
this.mData = data;
|
|
|
51 |
this.mContext = context;
|
|
|
52 |
this.iTwoGetSkills = (ITwoGetSkills) context;
|
|
|
53 |
this.mInflater = LayoutInflater.from(context);
|
|
|
54 |
this.mDecimalFormat = new DecimalFormat("#.##");
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
@Override
|
|
|
58 |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
59 |
Log.d(TAG, "onCreateViewHolder");
|
|
|
60 |
View view = mInflater.inflate(R.layout.fragment_topic_listitem, parent, false);
|
|
|
61 |
|
|
|
62 |
//View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_topic_listitem,parent,false);
|
|
|
63 |
return new ViewHolder(view);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
// binds the data to the TextView in each row
|
|
|
67 |
@Override
|
|
|
68 |
public void onBindViewHolder(ViewHolder holder, int position) {
|
|
|
69 |
Log.d(TAG, "onBindViewHolder");
|
|
|
70 |
|
|
|
71 |
Topic mItem = mData.get(position);
|
|
|
72 |
holder.mName.setText(mItem.getName());
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
holder.mProgressbar.setMax(mItem.getTotalSlides());
|
|
|
77 |
holder.mProgressbar.setProgress(mItem.getViewSlides());
|
|
|
78 |
holder.mProgress.setText(mDecimalFormat.format(mItem.getProgress()) + " %");
|
|
|
79 |
|
|
|
80 |
Log.d(TAG, mItem.getImage());
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
TimeZone timeZone = TimeZone.getTimeZone("UTC");
|
|
|
86 |
Calendar calendar = Calendar.getInstance(timeZone);
|
|
|
87 |
TimeZone tz = calendar.getTimeZone();
|
|
|
88 |
int created = (int) (calendar.getTimeInMillis() / 1000);
|
|
|
89 |
|
|
|
90 |
Random random = new Random(created);
|
|
|
91 |
int rand = 1000 + random.nextInt(8999);
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();
|
|
|
95 |
String password = iTwoGetSkills.getPreference().getPassword();
|
|
|
96 |
|
|
|
97 |
Log.d(TAG, "token = " + deviceUuid);
|
|
|
98 |
Log.d(TAG, "created = " + created);
|
|
|
99 |
Log.d(TAG, "rand = " + rand);
|
|
|
100 |
Log.d(TAG, "calc = " + password + ':' + created + ':' + rand);
|
|
|
101 |
|
|
|
102 |
String secret = MD5.generar(password + ':' + created + ':' + rand);
|
|
|
103 |
|
|
|
104 |
GlideUrl url = new GlideUrl(mItem.getImage(), new LazyHeaders.Builder()
|
|
|
105 |
.addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
|
|
|
106 |
.addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid)
|
|
|
107 |
.addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret)
|
|
|
108 |
.addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created))
|
|
|
109 |
.addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand))
|
|
|
110 |
.build());
|
|
|
111 |
|
|
|
112 |
RequestOptions options = new RequestOptions()
|
|
|
113 |
.diskCacheStrategy(DiskCacheStrategy.ALL);
|
|
|
114 |
|
|
|
115 |
Glide.with(mContext).load(url)
|
|
|
116 |
.thumbnail()
|
|
|
117 |
.apply(options)
|
|
|
118 |
.into(holder.mImage);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
// total number of rows
|
|
|
122 |
@Override
|
|
|
123 |
public int getItemCount() {
|
|
|
124 |
return mData.size();
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
// stores and recycles views as they are scrolled off screen
|
|
|
129 |
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
|
|
130 |
TextView mName;
|
|
|
131 |
ImageView mImage;
|
|
|
132 |
ProgressBar mProgressbar;
|
|
|
133 |
TextView mProgress;
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
ViewHolder(View itemView) {
|
|
|
138 |
super(itemView);
|
|
|
139 |
mName = (TextView) itemView.findViewById(R.id.fragment_topic_listitem_name);
|
|
|
140 |
mImage = (ImageView) itemView.findViewById(R.id.fragment_topic_listitem_image);
|
|
|
141 |
mProgress = (TextView) itemView.findViewById(R.id.fragment_topic_listitem_progress);
|
|
|
142 |
mProgressbar = (ProgressBar) itemView.findViewById(R.id.fragment_topic_listitem_progressbar);
|
|
|
143 |
itemView.setOnClickListener(this);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
@Override
|
|
|
147 |
public void onClick(View view) {
|
|
|
148 |
if (mClickListener != null) {
|
|
|
149 |
mClickListener.onItemClick(view, getAdapterPosition());
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
// convenience method for getting data at click position
|
|
|
155 |
public Topic getItem(int id) {
|
|
|
156 |
return mData.get(id);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
// allows clicks events to be caught
|
|
|
160 |
public void setClickListener(ItemClickListener itemClickListener) {
|
|
|
161 |
this.mClickListener = itemClickListener;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
// parent activity will implement this method to respond to click events
|
|
|
165 |
public interface ItemClickListener {
|
|
|
166 |
public void onItemClick(View view, int position);
|
|
|
167 |
}
|
|
|
168 |
}
|