Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

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