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