Proyectos de Subversion Android Microlearning - Inconcert

Rev

Rev 18 | Ir a la última revisión | | Ultima modificación | Ver Log |

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