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.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.cesams.twogetskills.Constants;
import com.cesams.twogetskills.R;
import com.cesams.twogetskills.entity.Capsule;
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.Random;
import java.util.TimeZone;
public class CapsuleListViewAdapter extends RecyclerView.Adapter<CapsuleListViewAdapter.ViewHolder> {
private final static String TAG = "C2GS - CapsuleAdapter";
private List<Capsule> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context mContext;
private ITwoGetSkills iTwoGetSkills;
private DecimalFormat mDecimalFormat;
// data is passed into the constructor
public CapsuleListViewAdapter(Context context, List<Capsule> data) {
this.mContext = context;
this.iTwoGetSkills = (ITwoGetSkills) context;
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.mDecimalFormat = new DecimalFormat("#.##");
}
// inflates the row layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.fragment_capsule_listitem, parent, false);
return new ViewHolder(view);
}
/*
public void updateCapsuleList(final List<Capsule> mData) {
this.mData.clear();
this.mData = mData;
Log.d(TAG, "Notificando el cambio al adaptador");
notifyDataSetChanged();
}
*/
// binds the data to the TextView in each row
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Capsule mItem = mData.get(position);
holder.mName.setText(mItem.getName());
/*
ProgressDao progressDao = new ProgressDao(mContext);
Progress progress = progressDao.selectByCapsuleUuid(mItem.uuid);
progressDao.close();
if(progress == null) {
holder.mProgressbar.setMax(100);
holder.mProgressbar.setProgress(0);
holder.mProgreess.setText("0 %");
} else {
holder.mProgressbar.setMax(progress.totalSlides);
holder.mProgressbar.setProgress(progress.viewSlides);
holder.mProgreess.setText(mDecimalFormat.format(progress.progress) + " %");
}*/
holder.mProgressbar.setMax(mItem.getTotalSlides());
holder.mProgressbar.setProgress(mItem.getViewSlides());
holder.mProgreess.setText(mDecimalFormat.format(mItem.getProgress()) + " %");
Log.d(TAG, mItem.getImage());
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;
//TextView mDescription;
//JustifyTextView mDescription;
ImageView mImage;
ProgressBar mProgressbar;
TextView mProgreess;
ViewHolder(View itemView) {
super(itemView);
mName = (TextView) itemView.findViewById(R.id.fragment_capsule_listitem_name);
mImage = (ImageView) itemView.findViewById(R.id.fragment_capsule_listitem_image);
mProgreess = (TextView) itemView.findViewById(R.id.fragment_capsule_listitem_progress);
mProgressbar = (ProgressBar) itemView.findViewById(R.id.fragment_capsule_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 Capsule 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);
}
}