Proyectos de Subversion Android Microlearning

Rev

Rev 4 | Rev 11 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
package com.cesams.twogetskills.adapter;
2
 
3
import android.annotation.SuppressLint;
4
import android.content.Context;
5
import android.text.TextUtils;
6
import android.util.Log;
7
import android.view.LayoutInflater;
8
import android.view.View;
9
import android.view.ViewGroup;
10
import android.widget.Button;
11
import android.widget.ImageView;
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.dao.ProgressDao;
22
import com.cesams.twogetskills.dao.SyncDao;
23
import com.cesams.twogetskills.dao.UserLogDao;
24
import com.cesams.twogetskills.entity.Progress;
25
import com.cesams.twogetskills.entity.Slide;
26
import com.cesams.twogetskills.entity.Sync;
27
import com.cesams.twogetskills.entity.UserLog;
28
import com.cesams.twogetskills.library.MD5;
29
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
30
 
31
import org.json.JSONException;
32
import org.json.JSONObject;
33
 
34
import java.text.SimpleDateFormat;
35
import java.util.Calendar;
36
import java.util.Date;
37
import java.util.List;
38
import java.util.Random;
39
import java.util.TimeZone;
40
 
41
public class GalleryViewPageAdapter extends RecyclerView.Adapter<GalleryViewPageAdapter.ViewHolder>  {
42
    private final static String TAG = "C2GS - GalleryAdapter";
43
    private List<Slide> mData;
44
    private LayoutInflater mInflater;
45
    private ITwoGetSkills iTwoGetSkills;
46
    private Context mContext;
47
    private Slide mItemCurrent;
48
 
49
    // data is passed into the constructor
50
    public  GalleryViewPageAdapter(Context context, List<Slide> data) {
51
        this.mContext = context;
52
        this.iTwoGetSkills = (ITwoGetSkills) context;
53
        this.mInflater = LayoutInflater.from(context);
54
        this.mData = data;
55
    }
56
 
57
    // inflates the row layout from xml when needed
58
    @Override
59
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
60
        View view = mInflater.inflate(R.layout.fragment_gallery_viewpager_item, parent, false);
61
        return new ViewHolder(view);
62
    }
63
 
64
    // binds the data to the TextView in each row
65
    @SuppressLint("NewApi")
66
    @Override
67
    public void onBindViewHolder(ViewHolder holder, int position) {
68
        holder.mCheckReady.setVisibility(View.INVISIBLE);
69
        holder.mButtonFinish.setVisibility(View.INVISIBLE);
70
        holder.mButtonViewer.setVisibility(View.INVISIBLE);
71
 
72
        mItemCurrent = mData.get(position);
73
 
74
        if(mItemCurrent.isShowFinish()) {
75
            holder.mButtonFinish.setVisibility(View.VISIBLE);
76
        }
77
        if(mItemCurrent.getCompleted()== 1) {
78
            holder.mCheckReady.setVisibility(View.VISIBLE);
79
        }
80
 
81
        switch(mItemCurrent.getType()) {
82
 
83
            case Constants.SLIDE_TYPE_TEXT :
84
 
85
                holder.mButtonViewer.setVisibility(View.VISIBLE);
86
                holder.mButtonViewer.setText(this.mContext.getString(R.string.button_launch_text));
87
                break;
88
 
89
 
90
            case Constants.SLIDE_TYPE_AUDIO :
91
 
92
                holder.mButtonViewer.setVisibility(View.VISIBLE);
93
                holder.mButtonViewer.setText(this.mContext.getString(R.string.button_launch_audio));
94
                break;
95
 
96
            case Constants.SLIDE_TYPE_DOCUMENT :
97
 
98
                holder.mButtonViewer.setVisibility(View.VISIBLE);
99
                holder.mButtonViewer.setText(this.mContext.getString(R.string.button_launch_document));
100
                break;
101
 
102
            case Constants.SLIDE_TYPE_VIDEO :
103
 
104
                holder.mButtonViewer.setVisibility(View.VISIBLE);
105
                holder.mButtonViewer.setText(this.mContext.getString(R.string.button_launch_video));
106
                break;
107
 
108
 
109
            case Constants.SLIDE_TYPE_QUIZ :
110
                if(mItemCurrent.getCompleted()  == 0) {
111
 
112
                    holder.mButtonViewer.setVisibility(View.VISIBLE);
113
                    holder.mButtonViewer.setText(this.mContext.getString(R.string.button_launch_quiz));
114
                } else {
115
                    holder.mButtonViewer.setVisibility(View.INVISIBLE);
116
                }
117
                break;
118
 
119
            default :
120
                holder.mButtonViewer.setVisibility(View.INVISIBLE);
121
                break;
122
 
123
        }
124
 
125
 
126
 
127
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
128
        Calendar calendar = Calendar.getInstance(timeZone);
129
        TimeZone tz = calendar.getTimeZone();
130
        int created =  (int) (calendar.getTimeInMillis() / 1000);
131
 
132
        Random random = new Random(created);
133
        int rand = 1000 + random.nextInt(8999);
134
 
135
 
136
        String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();
137
        String password =  iTwoGetSkills.getPreference().getPassword();
138
 
139
        Log.d(TAG, "token = " + deviceUuid);
140
        Log.d(TAG, "created = " + created);
141
        Log.d(TAG, "rand = " + rand);
142
        Log.d(TAG, "calc = " + password + ':' +  created + ':' + rand);
143
 
144
        String image = TextUtils.isEmpty(mItemCurrent.getBackground()) ?mItemCurrent.getFile() : mItemCurrent.getBackground();
145
        Log.d(TAG, "gallery slide image = " + image);
146
 
147
        String secret = MD5.generar(password + ':' +  created + ':' + rand);
148
 
149
        GlideUrl url = new GlideUrl(image, new LazyHeaders.Builder()
150
                .addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
151
                .addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid)
152
                .addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret)
153
                .addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created))
154
                .addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand))
155
                .build());
156
 
157
        RequestOptions options = new RequestOptions()
158
                .diskCacheStrategy(DiskCacheStrategy.ALL);
159
 
160
        Glide.with(mContext).load(url)
161
                .thumbnail()
162
                .apply(options)
163
                .into(holder.mImage);
164
 
165
 
166
 
167
    }
168
 
169
 
170
 
171
 
172
    // total number of rows
173
    @Override
174
    public int getItemCount() {
175
        return mData.size();
176
    }
177
 
178
 
179
    // stores and recycles views as they are scrolled off screen
180
    public class ViewHolder extends RecyclerView.ViewHolder {
181
        ImageView mImage;
182
        ImageView mCheckReady;
183
        Button mButtonViewer;
184
        Button mButtonFinish;
185
 
186
 
187
        ViewHolder(View itemView) {
188
            super(itemView);
8 gabriel 189
            mImage = itemView.findViewById(R.id.fragment_gallery_viewpage_item_image);
190
            mCheckReady = itemView.findViewById(R.id.fragment_gallery_viewpage_item_check_ready);
191
            mButtonViewer = itemView.findViewById(R.id.fragment_gallery_viewpage_item_button_viewer);
1 efrain 192
            mButtonViewer.setOnClickListener(new View.OnClickListener() {
193
                @Override
194
                public void onClick(View view) {
195
                    launchViewer();
196
                }
197
            });
198
 
8 gabriel 199
            mButtonFinish = itemView.findViewById(R.id.fragment_gallery_viewpage_item_button_finish);
1 efrain 200
            mButtonFinish.setOnClickListener(new View.OnClickListener() {
201
                @Override
202
                public void onClick(View view) {
203
                    finishCapsuleAndOrTopic();
204
                }
205
            });
206
        }
207
    }
208
 
209
    public void launchViewer()
210
    {
211
 
212
 
213
        switch (mItemCurrent.getType()) {
214
            case Constants.SLIDE_TYPE_TEXT:
215
 
216
                iTwoGetSkills.launchTextViewer(mItemCurrent.getDescription());
217
                break;
218
 
219
            case Constants.SLIDE_TYPE_AUDIO:
220
 
221
                iTwoGetSkills.launchAudioViewer(mItemCurrent.getFile());
222
                break;
223
 
224
            case Constants.SLIDE_TYPE_DOCUMENT:
225
                iTwoGetSkills.launchDocumentViewer(mItemCurrent.getFile());
226
 
227
                break;
228
 
229
            case Constants.SLIDE_TYPE_VIDEO:
230
 
231
                iTwoGetSkills.launchVideoViewer(mItemCurrent.getFile());
232
                break;
233
 
234
 
235
            case Constants.SLIDE_TYPE_QUIZ:
236
                iTwoGetSkills.launchQuizViewer(mItemCurrent.getQuizUuid());
237
 
238
                break;
239
        }
240
    }
241
 
242
    public void finishCapsuleAndOrTopic()
243
    {
244
        UserLogDao userLogDao = iTwoGetSkills.getDatabase().getUserLogDao();
245
        SyncDao syncDao = iTwoGetSkills.getDatabase().getSyncDao();
246
        ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();
247
 
248
        Progress progressCapsule = progressDao.selectByCapsuleUuid(mItemCurrent.getCapsuleUuid());
249
        Progress progressTopic = progressDao.selectByTopicUuid(mItemCurrent.getTopicUuid());
250
 
251
        Calendar calendar = Calendar.getInstance();
252
        Date date = calendar.getTime();
253
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_DATETIME_SERVICE);
254
        String  dateOn = simpleDateFormat.format(date);
255
 
256
        UserLog userLog;
257
        Sync sync;
258
 
259
        int toInvokeFragment = Constants.IDX_FRAGMENT_FINISH_TOPIC;
260
 
261
        if(progressCapsule.getProgress() >= 100 && progressCapsule.getCompleted() == 0) {
262
            toInvokeFragment = Constants.IDX_FRAGMENT_FINISH_CAPSULE;
263
 
264
            userLog = new UserLog();
265
            userLog.setUserUuid(iTwoGetSkills.getPreference().getUserUuid());
266
            userLog.setActivity(Constants.USER_LOG_ACTIVITY_COMPLETED_CAPSULE);
267
            userLog.setCompanyUuid(progressCapsule.getCompanyUuid());
268
            userLog.setTopicUuid(progressCapsule.getTopicUuid());
269
            userLog.setCapsuleUuid(progressCapsule.getCapsuleUuid());
270
            userLog.setAddedOn(dateOn);
271
            userLogDao.insert(userLog);
272
 
273
            try {
274
                JSONObject json = userLog.toJson();
275
                json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG);
276
 
277
                sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC,json.toString());
278
                syncDao.insert(sync);
279
 
280
            } catch (JSONException e) {
281
                Log.d(TAG, e.getMessage());
282
            }
283
 
284
            progressCapsule.setCompleted(1);
285
            progressDao.update(progressCapsule);
286
 
287
            try {
288
                JSONObject json = progressCapsule.toJson();
289
                json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS);
290
 
291
                sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
292
                syncDao.insert(sync);
293
 
294
            } catch (JSONException e) {
295
                Log.d(TAG, e.getMessage());
296
            }
297
        }
298
 
4 gabriel 299
 
1 efrain 300
        if (progressTopic.getProgress() >= 100 && progressTopic.getCompleted() == 0) {
301
            toInvokeFragment = Constants.IDX_FRAGMENT_FINISH_TOPIC;
302
 
303
            userLog = new UserLog();
304
            userLog.setUserUuid(iTwoGetSkills.getPreference().getUserUuid());
305
            userLog.setActivity(Constants.USER_LOG_ACTIVITY_COMPLETED_TOPIC);
306
            userLog.setCompanyUuid(progressTopic.getCompanyUuid());
307
            userLog.setTopicUuid(progressTopic.getTopicUuid());
308
            userLog.setAddedOn(dateOn);
309
            userLogDao.insert(userLog);
310
 
311
            try {
312
                JSONObject json = userLog.toJson();
313
                json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_USER_LOG);
314
 
315
                sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC, json.toString());
316
                syncDao.insert(sync);
317
 
318
            } catch (JSONException e) {
319
                Log.d(TAG, e.getMessage());
320
            }
321
 
322
            progressTopic.setCompleted(1);
323
            progressDao.update(progressTopic);
324
 
325
            try {
326
                JSONObject json = progressTopic.toJson();
327
                json.put(Constants.SYNC_ADAPTER_DATA_TYPE_FIELD_NAME, Constants.SYNC_ADAPTER_DATA_TYPE_MICROLEARNING_PROGRESS);
328
 
329
                sync = new Sync(Constants.SYNC_ADAPTER_TYPE_SYNC,json.toString());
330
                syncDao.insert(sync);
331
            } catch (JSONException e) {
332
                Log.d(TAG, e.getMessage());
333
            }
334
        }
335
 
336
        iTwoGetSkills.requestExecuteSyncAdapter();
337
        iTwoGetSkills.invokeFragment(toInvokeFragment);
338
    }
339
}