Proyectos de Subversion Android Microlearning - Inconcert

Rev

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