Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

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

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