AutorÃa | Ultima modificación | Ver Log |
package com.cesams.twogetskills.adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.util.FixedPreloadSizeProvider;
import com.cesams.twogetskills.Constants;
import com.cesams.twogetskills.R;
import com.cesams.twogetskills.entity.Topic;
import com.cesams.twogetskills.library.MD5;
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.TimeZone;
public class TopicListViewAdapter extends RecyclerView.Adapter<TopicListViewAdapter.ViewHolder> {
private final static String TAG = "C2GS - TopicAdapter";
private List<Topic> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context mContext;
private ITwoGetSkills iTwoGetSkills;
private DecimalFormat mDecimalFormat;
private final int imageWidthPixels = 1024;
private final int imageHeightPixels = 768;
// data is passed into the constructor
public TopicListViewAdapter(Context context, List<Topic> data) {
this.mData = data;
this.mContext = context;
this.iTwoGetSkills = (ITwoGetSkills) context;
this.mInflater = LayoutInflater.from(context);
this.mDecimalFormat = new DecimalFormat("#.##");
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder");
View view = mInflater.inflate(R.layout.fragment_topic_listitem, parent, false);
//View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_topic_listitem,parent,false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder");
Topic mItem = mData.get(position);
holder.mName.setText(mItem.getName());
holder.mProgressbar.setMax(mItem.getTotalSlides());
holder.mProgressbar.setProgress(mItem.getViewSlides());
holder.mProgress.setText(mDecimalFormat.format(mItem.getProgress()) + " %");
Log.d(TAG, mItem.getImage());
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
TimeZone tz = calendar.getTimeZone();
int created = (int) (calendar.getTimeInMillis() / 1000);
Random random = new Random(created);
int rand = 1000 + random.nextInt(8999);
String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();
String password = iTwoGetSkills.getPreference().getPassword();
Log.d(TAG, "token = " + deviceUuid);
Log.d(TAG, "created = " + created);
Log.d(TAG, "rand = " + rand);
Log.d(TAG, "calc = " + password + ':' + created + ':' + rand);
String secret = MD5.generar(password + ':' + created + ':' + rand);
GlideUrl url = new GlideUrl(mItem.getImage(), new LazyHeaders.Builder()
.addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
.addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid)
.addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret)
.addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created))
.addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand))
.build());
RequestOptions options = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(mContext).load(url)
.thumbnail()
.apply(options)
.into(holder.mImage);
}
// total number of rows
@Override
public int getItemCount() {
return mData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView mName;
ImageView mImage;
ProgressBar mProgressbar;
TextView mProgress;
ViewHolder(View itemView) {
super(itemView);
mName = (TextView) itemView.findViewById(R.id.fragment_topic_listitem_name);
mImage = (ImageView) itemView.findViewById(R.id.fragment_topic_listitem_image);
mProgress = (TextView) itemView.findViewById(R.id.fragment_topic_listitem_progress);
mProgressbar = (ProgressBar) itemView.findViewById(R.id.fragment_topic_listitem_progressbar);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) {
mClickListener.onItemClick(view, getAdapterPosition());
}
}
}
// convenience method for getting data at click position
public Topic getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
public void onItemClick(View view, int position);
}
}