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.TextView;
|
|
|
10 |
import androidx.recyclerview.widget.RecyclerView;
|
|
|
11 |
|
|
|
12 |
import com.bumptech.glide.Glide;
|
|
|
13 |
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
|
|
14 |
import com.bumptech.glide.load.model.GlideUrl;
|
|
|
15 |
import com.bumptech.glide.load.model.LazyHeaders;
|
|
|
16 |
import com.bumptech.glide.request.RequestOptions;
|
|
|
17 |
import com.cesams.twogetskills.Constants;
|
|
|
18 |
import com.cesams.twogetskills.R;
|
|
|
19 |
import com.cesams.twogetskills.entity.Company;
|
|
|
20 |
import com.cesams.twogetskills.library.MD5;
|
|
|
21 |
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
|
|
|
22 |
|
|
|
23 |
import java.util.Calendar;
|
|
|
24 |
import java.util.List;
|
|
|
25 |
import java.util.Random;
|
|
|
26 |
import java.util.TimeZone;
|
|
|
27 |
|
|
|
28 |
public class CompanyListViewAdapter extends RecyclerView.Adapter<CompanyListViewAdapter.ViewHolder> {
|
|
|
29 |
private final static String TAG = "C2GS - CompListAdapter";
|
|
|
30 |
private List<Company> mData;
|
|
|
31 |
private LayoutInflater mInflater;
|
|
|
32 |
private ItemClickListener mClickListener;
|
|
|
33 |
private ITwoGetSkills iTwoGetSkills;
|
|
|
34 |
private Context mContext;
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
// data is passed into the constructor
|
|
|
38 |
public CompanyListViewAdapter(Context context, List<Company> data) {
|
|
|
39 |
this.mContext = context;
|
|
|
40 |
this.iTwoGetSkills = (ITwoGetSkills) context;
|
|
|
41 |
this.mInflater = LayoutInflater.from(context);
|
|
|
42 |
this.mData = data;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
// inflates the row layout from xml when needed
|
|
|
46 |
@Override
|
|
|
47 |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
|
48 |
View view = mInflater.inflate(R.layout.fragment_company_listitem, parent, false);
|
|
|
49 |
return new ViewHolder(view);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
// binds the data to the TextView in each row
|
|
|
53 |
@Override
|
|
|
54 |
public void onBindViewHolder(ViewHolder holder, int position) {
|
|
|
55 |
Company mItem = mData.get(position);
|
|
|
56 |
holder.mName.setText(mItem.getName());
|
|
|
57 |
|
|
|
58 |
Log.d(TAG, mItem.getImage());
|
|
|
59 |
|
|
|
60 |
TimeZone timeZone = TimeZone.getTimeZone("UTC");
|
|
|
61 |
Calendar calendar = Calendar.getInstance(timeZone);
|
|
|
62 |
TimeZone tz = calendar.getTimeZone();
|
|
|
63 |
int created = (int) (calendar.getTimeInMillis() / 1000);
|
|
|
64 |
|
|
|
65 |
Random random = new Random(created);
|
|
|
66 |
int rand = 1000 + random.nextInt(8999);
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();
|
|
|
70 |
String password = iTwoGetSkills.getPreference().getPassword();
|
|
|
71 |
|
|
|
72 |
Log.d(TAG, "token = " + deviceUuid);
|
|
|
73 |
Log.d(TAG, "created = " + created);
|
|
|
74 |
Log.d(TAG, "rand = " + rand);
|
|
|
75 |
Log.d(TAG, "calc = " + password + ':' + created + ':' + rand);
|
|
|
76 |
|
|
|
77 |
String secret = MD5.generar(password + ':' + created + ':' + rand);
|
|
|
78 |
|
|
|
79 |
GlideUrl url = new GlideUrl(mItem.getImage(), new LazyHeaders.Builder()
|
|
|
80 |
.addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
|
|
|
81 |
.addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid)
|
|
|
82 |
.addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret)
|
|
|
83 |
.addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created))
|
|
|
84 |
.addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand))
|
|
|
85 |
.build());
|
|
|
86 |
|
|
|
87 |
RequestOptions options = new RequestOptions()
|
|
|
88 |
.diskCacheStrategy(DiskCacheStrategy.ALL);
|
|
|
89 |
|
|
|
90 |
Glide.with(mContext).load(url)
|
|
|
91 |
.thumbnail()
|
|
|
92 |
.apply(options)
|
|
|
93 |
.into(holder.mImage);
|
|
|
94 |
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
// total number of rows
|
|
|
98 |
@Override
|
|
|
99 |
public int getItemCount() {
|
|
|
100 |
return mData.size();
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
// stores and recycles views as they are scrolled off screen
|
|
|
105 |
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
|
|
106 |
TextView mName;
|
|
|
107 |
ImageView mImage;
|
|
|
108 |
|
|
|
109 |
ViewHolder(View itemView) {
|
|
|
110 |
super(itemView);
|
|
|
111 |
mName = (TextView) itemView.findViewById(R.id.fragment_company_listitem_name);
|
|
|
112 |
mImage = (ImageView) itemView.findViewById(R.id.fragment_company_listitem_image);
|
|
|
113 |
itemView.setOnClickListener(this);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
@Override
|
|
|
117 |
public void onClick(View view) {
|
|
|
118 |
if (mClickListener != null) {
|
|
|
119 |
mClickListener. onItemClick(view, getAdapterPosition());
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
// convenience method for getting data at click position
|
|
|
125 |
public Company getItem(int id) {
|
|
|
126 |
return mData.get(id);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
// allows clicks events to be caught
|
|
|
130 |
public void setClickListener(ItemClickListener itemClickListener) {
|
|
|
131 |
this.mClickListener = itemClickListener;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
// parent activity will implement this method to respond to click events
|
|
|
135 |
public interface ItemClickListener {
|
|
|
136 |
public void onItemClick(View view, int position);
|
|
|
137 |
}
|
|
|
138 |
}
|