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