Proyectos de Subversion Android Microlearning

Rev

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