Proyectos de Subversion Android Microlearning

Rev

| Ultima modificación | Ver Log |

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