Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
2 gabriel 1
package com.cesams.twogetskills.fragment;
2
 
3
import android.os.Bundle;
4
 
11 gabriel 5
import androidx.annotation.NonNull;
2 gabriel 6
import androidx.fragment.app.Fragment;
7
import androidx.lifecycle.LifecycleOwner;
8
import androidx.recyclerview.widget.GridLayoutManager;
9
import androidx.recyclerview.widget.RecyclerView;
10
 
11
import android.util.Log;
12
import android.view.LayoutInflater;
11 gabriel 13
import android.view.MotionEvent;
2 gabriel 14
import android.view.View;
15
import android.view.ViewGroup;
11 gabriel 16
import android.widget.AdapterView;
9 gabriel 17
import android.widget.Button;
8 gabriel 18
import android.widget.EditText;
9 gabriel 19
import android.widget.ImageView;
20
import android.widget.ProgressBar;
2 gabriel 21
import android.widget.TextView;
22
 
9 gabriel 23
import com.bumptech.glide.Glide;
24
import com.bumptech.glide.load.engine.DiskCacheStrategy;
25
import com.bumptech.glide.load.model.GlideUrl;
26
import com.bumptech.glide.load.model.LazyHeaders;
27
import com.bumptech.glide.request.RequestOptions;
28
import com.cesams.twogetskills.Constants;
2 gabriel 29
import com.cesams.twogetskills.R;
30
import com.cesams.twogetskills.adapter.CapsuleListViewAdapter;
4 gabriel 31
import com.cesams.twogetskills.adapter.TabsCapsulesAdapter;
2 gabriel 32
import com.cesams.twogetskills.dao.CapsuleDao;
33
import com.cesams.twogetskills.dao.ProgressDao;
34
import com.cesams.twogetskills.dao.TopicDao;
35
import com.cesams.twogetskills.entity.Capsule;
36
import com.cesams.twogetskills.entity.Progress;
37
import com.cesams.twogetskills.entity.Topic;
9 gabriel 38
import com.cesams.twogetskills.library.MD5;
2 gabriel 39
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
40
 
41
import java.util.ArrayList;
9 gabriel 42
import java.util.Calendar;
2 gabriel 43
import java.util.HashMap;
44
import java.util.List;
9 gabriel 45
import java.util.Random;
46
import java.util.TimeZone;
2 gabriel 47
 
48
/**
49
 * A simple {@link Fragment} subclass.
50
 * Use the {@link WelcomeFragment#newInstance} factory method to
51
 * create an instance of this fragment.
52
 */
11 gabriel 53
public class WelcomeFragment extends Fragment implements LifecycleOwner {
2 gabriel 54
 
10 gabriel 55
    TextView username, titulotarjeta, progresoporcentaje, empezar; ProgressBar progresotarjeta;
9 gabriel 56
    private ITwoGetSkills iTwoGetSkills; View card;
2 gabriel 57
    ArrayList<HashMap<String, String>> capsuleList;
11 gabriel 58
    ImageView imagetarjeta;
59
    TabsCapsulesAdapter adapter2;
60
    RecyclerView categorizados;
9 gabriel 61
    EditText busqueda; TextView textowelcome; Button continuar;
2 gabriel 62
 
63
    public WelcomeFragment() {
64
        // Required empty public constructor
65
    }
66
 
67
    public static WelcomeFragment newInstance() {
68
        WelcomeFragment fragment = new WelcomeFragment();
69
        return fragment;
70
    }
71
 
72
    @Override
73
    public void onCreate(Bundle savedInstanceState) {
74
        super.onCreate(savedInstanceState);
75
          }
76
 
77
    @Override
78
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
79
                             Bundle savedInstanceState) {
80
 
8 gabriel 81
 
2 gabriel 82
        iTwoGetSkills = (ITwoGetSkills) getActivity();
8 gabriel 83
        iTwoGetSkills.showNavigationAndToolbar();
2 gabriel 84
 
85
        View view= inflater.inflate(R.layout.fragment_welcome, container, false);
86
 
87
        username=view.findViewById(R.id.textView14);
8 gabriel 88
        textowelcome= view.findViewById(R.id.textView15);
89
        busqueda=view.findViewById(R.id.busqueda);
11 gabriel 90
        card= view.findViewById(R.id.include); empezar= view.findViewById(R.id.titlenotifi);
8 gabriel 91
        capsuleList = new ArrayList<>();
2 gabriel 92
 
9 gabriel 93
        //Tarjeta Principal
11 gabriel 94
        titulotarjeta = view.findViewById(R.id.description); progresotarjeta= view.findViewById(R.id.progressBar2); imagetarjeta= view.findViewById(R.id.imagelist);
9 gabriel 95
        continuar = view.findViewById(R.id.button); progresoporcentaje = view.findViewById(R.id.textView16);
96
 
97
 
2 gabriel 98
        username.setText("¡Hola, "+(iTwoGetSkills.getPreference().getFirstName() +"!"));
99
 
100
 
101
        loadData();
102
 
8 gabriel 103
        busqueda.setOnFocusChangeListener(new View.OnFocusChangeListener() {
104
            @Override
105
            public void onFocusChange(View v, boolean hasFocus) {
106
                if(hasFocus)
107
                {
9 gabriel 108
                        card.setVisibility(View.GONE);
8 gabriel 109
                        username.setVisibility(View.GONE);
110
                        textowelcome.setVisibility(View.GONE);
111
                }
112
 
113
            }
114
        });
115
 
116
 
4 gabriel 117
        adapter2 = new TabsCapsulesAdapter(getContext(),capsuleList);
2 gabriel 118
 
119
 
120
 
4 gabriel 121
        categorizados = view.findViewById(R.id.categorizados);
122
        GridLayoutManager layoutcategoria = new GridLayoutManager(getContext(),1,GridLayoutManager.VERTICAL, false);
123
        categorizados.setLayoutManager(layoutcategoria);
124
        categorizados.setAdapter(adapter2);
125
 
11 gabriel 126
 
127
 
128
 
8 gabriel 129
        categorizados.setOnFlingListener(new RecyclerView.OnFlingListener() {
130
            @Override
131
            public boolean onFling(int velocityX, int velocityY) {
132
 
133
                if (categorizados.canScrollVertically(-1) ) {
134
 
135
                    Log.e("Se activa", "On fling");
9 gabriel 136
                    card.setVisibility(View.GONE);
8 gabriel 137
                    username.setVisibility(View.GONE);
138
                    textowelcome.setVisibility(View.GONE);
139
                }else
140
                {
9 gabriel 141
                    card.setVisibility(View.VISIBLE);
8 gabriel 142
                    username.setVisibility(View.VISIBLE);
143
                    textowelcome.setVisibility(View.VISIBLE);
144
                }
145
 
146
                return false;
147
            }
148
        });
149
 
11 gabriel 150
 
8 gabriel 151
     /*   categorizados.setOnScrollChangeListener(new View.OnScrollChangeListener() {
152
            @Override
153
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
154
 
155
                if (!categorizados.canScrollVertically(-1)) {
156
                 //   Toast.makeText(getActivity(), "Last", Toast.LENGTH_LONG).show();
157
                    capsulas.setVisibility(View.VISIBLE);
158
                    username.setVisibility(View.VISIBLE);
159
                    textowelcome.setVisibility(View.VISIBLE);
160
                    Log.e("On scroll"," se activa");
161
                }
162
 
163
            }
164
        });
165
*/
2 gabriel 166
        return view;
167
    }
168
 
9 gabriel 169
    @Override
170
    public void onResume() {
171
        super.onResume();
172
        loadContinuecard();
173
    }
2 gabriel 174
 
11 gabriel 175
 
176
 
9 gabriel 177
    @Override
178
    public void onHiddenChanged(boolean hidden) {
179
        super.onHiddenChanged(hidden);
180
 
181
        Log.e("TAG", "onHiddenChanged : " + (hidden ? "true" : "false"));
182
 
183
        if(!hidden) {
184
            loadContinuecard();
185
        }
186
    }
187
 
188
    private void loadContinuecard(){
189
 
190
        Log.e("Cargo datos"," en tarjeta principal");
191
 
192
        //Capsula de primera tarjeta
193
        CapsuleDao capsuleDao = iTwoGetSkills.getDatabase().getCapsuleDao();
194
 
195
        Progress progress;
196
        ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();
197
 
198
        List<Capsule> dbCapsules= capsuleDao.selectAll();
199
        for(Capsule dbCapsule : dbCapsules)
200
        {
201
            progress = progressDao.selectByCapsuleUuid(dbCapsule.getUuid());
202
 
203
            if (progress != null) {
204
 
10 gabriel 205
                    if (progress.getProgress() < 100) {
206
                        Log.e("Es menor"," a 100");
9 gabriel 207
 
208
                        titulotarjeta.setText(dbCapsule.getName());
10 gabriel 209
                        progresotarjeta.setVisibility(View.VISIBLE);
210
                        progresoporcentaje.setVisibility(View.VISIBLE);
9 gabriel 211
                        progresotarjeta.setProgress((int) progress.getProgress());
212
                        progresoporcentaje.setText("" + progress.getProgress() + " %");
10 gabriel 213
                        empezar.setText("En curso");
9 gabriel 214
 
215
                        iTwoGetSkills.getPreference().setCapsuleUuidActive(dbCapsule.getUuid());
216
                        iTwoGetSkills.getPreference().setTopicUuidActive(dbCapsule.getTopicUuid());
217
 
218
                        if (dbCapsule.getImage() != null && !dbCapsule.getImage().equals("")) {
219
 
220
                            TimeZone timeZone = TimeZone.getTimeZone("UTC");
221
                            Calendar calendar = Calendar.getInstance(timeZone);
222
                            TimeZone tz = calendar.getTimeZone();
223
                            int created = (int) (calendar.getTimeInMillis() / 1000);
224
 
225
                            Random random = new Random(created);
226
                            int rand = 1000 + random.nextInt(8999);
227
 
228
 
229
                            String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();
230
                            String password = iTwoGetSkills.getPreference().getPassword();
231
 
232
 
233
                            String secret = MD5.generar(password + ':' + created + ':' + rand);
234
                            GlideUrl url = new GlideUrl(dbCapsule.getImage(), new LazyHeaders.Builder()
235
                                    .addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
236
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid)
237
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret)
238
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created))
239
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand))
240
                                    .build());
241
 
242
                            RequestOptions options = new RequestOptions()
243
                                    .diskCacheStrategy(DiskCacheStrategy.ALL);
244
 
245
                            Glide.with(getContext()).load(url)
246
                                    .thumbnail()
247
                                    .apply(options)
248
                                    .into(imagetarjeta);
249
                        }
250
                    }
251
 
10 gabriel 252
            }
253
 
254
 
255
        }
256
 
257
        if(titulotarjeta.getText().equals(""))
258
        {
259
            Log.e("Nombre","vacio");
260
 
261
            //Despues del recorrido no se encontro progreso en ninguna capsula, por lo cual la tarjeta queda vacia
262
            // vamos a llenarla con la ultima capsula, que debe ser nueva al no tener progreso
263
 
264
            for(Capsule dbCapsule : dbCapsules)
265
            {
266
 
267
                progress = progressDao.selectByCapsuleUuid(dbCapsule.getUuid());
268
 
269
                if (progress == null) {
270
 
271
                        Log.e("Es nueva","");
272
 
273
                        titulotarjeta.setText(dbCapsule.getName());
274
                        progresotarjeta.setVisibility(View.INVISIBLE);
275
                        progresoporcentaje.setVisibility(View.INVISIBLE);
276
                        empezar.setText("Nueva");
277
                        continuar.setText("Ver cápsula");
278
 
279
                        iTwoGetSkills.getPreference().setCapsuleUuidActive(dbCapsule.getUuid());
280
                        iTwoGetSkills.getPreference().setTopicUuidActive(dbCapsule.getTopicUuid());
281
 
282
                        if (dbCapsule.getImage() != null && !dbCapsule.getImage().equals("")) {
283
 
284
                            TimeZone timeZone = TimeZone.getTimeZone("UTC");
285
                            Calendar calendar = Calendar.getInstance(timeZone);
286
                            TimeZone tz = calendar.getTimeZone();
287
                            int created = (int) (calendar.getTimeInMillis() / 1000);
288
 
289
                            Random random = new Random(created);
290
                            int rand = 1000 + random.nextInt(8999);
291
 
292
 
293
                            String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();
294
                            String password = iTwoGetSkills.getPreference().getPassword();
295
 
296
 
297
                            String secret = MD5.generar(password + ':' + created + ':' + rand);
298
                            GlideUrl url = new GlideUrl(dbCapsule.getImage(), new LazyHeaders.Builder()
299
                                    .addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
300
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid)
301
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret)
302
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created))
303
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand))
304
                                    .build());
305
 
306
                            RequestOptions options = new RequestOptions()
307
                                    .diskCacheStrategy(DiskCacheStrategy.ALL);
308
 
309
                            Glide.with(getContext()).load(url)
310
                                    .thumbnail()
311
                                    .apply(options)
312
                                    .into(imagetarjeta);
313
                        }
314
 
315
                    }
316
 
317
                /*
318
                if (progress != null) {
319
 
320
                    double progre = progress.getProgress();
321
 
322
                    if (progre >= 100) {
323
                        Log.e("Es mayor"," a 100");
324
 
325
                    } else {
326
 
327
                        Log.e("Es nueva","");
328
 
329
                        titulotarjeta.setText(dbCapsule.getName());
330
                        progresotarjeta.setVisibility(View.INVISIBLE);
331
                        progresoporcentaje.setVisibility(View.INVISIBLE);
332
                        empezar.setText("Nueva");
333
                        continuar.setText("Ver cápsula");
334
 
335
                        iTwoGetSkills.getPreference().setCapsuleUuidActive(dbCapsule.getUuid());
336
                        iTwoGetSkills.getPreference().setTopicUuidActive(dbCapsule.getTopicUuid());
337
 
338
                        if (dbCapsule.getImage() != null && !dbCapsule.getImage().equals("")) {
339
 
340
                            TimeZone timeZone = TimeZone.getTimeZone("UTC");
341
                            Calendar calendar = Calendar.getInstance(timeZone);
342
                            TimeZone tz = calendar.getTimeZone();
343
                            int created = (int) (calendar.getTimeInMillis() / 1000);
344
 
345
                            Random random = new Random(created);
346
                            int rand = 1000 + random.nextInt(8999);
347
 
348
 
349
                            String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();
350
                            String password = iTwoGetSkills.getPreference().getPassword();
351
 
352
 
353
                            String secret = MD5.generar(password + ':' + created + ':' + rand);
354
                            GlideUrl url = new GlideUrl(dbCapsule.getImage(), new LazyHeaders.Builder()
355
                                    .addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
356
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid)
357
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret)
358
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created))
359
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand))
360
                                    .build());
361
 
362
                            RequestOptions options = new RequestOptions()
363
                                    .diskCacheStrategy(DiskCacheStrategy.ALL);
364
 
365
                            Glide.with(getContext()).load(url)
366
                                    .thumbnail()
367
                                    .apply(options)
368
                                    .into(imagetarjeta);
369
                        }
370
 
371
                    }
9 gabriel 372
                }
10 gabriel 373
*/
374
            }
9 gabriel 375
 
10 gabriel 376
 
377
 
9 gabriel 378
        }
379
 
10 gabriel 380
 
381
 
9 gabriel 382
        continuar.setOnClickListener(v -> {
383
 
384
            iTwoGetSkills.invokeFragment(Constants.IDX_FRAGMENT_SLIDES);
385
        });
386
 
387
        //Fin de primer tarjeta
388
 
389
    }
390
 
2 gabriel 391
    private void loadData()
392
    {
393
 
394
        TopicDao topicDao = iTwoGetSkills.getDatabase().getTopicDao();
395
        ArrayList<Topic> dbTopics = (ArrayList<Topic>) topicDao.selectAll();
396
 
397
        List<Capsule> dbCapsules;
398
        CapsuleDao capsuleDao = iTwoGetSkills.getDatabase().getCapsuleDao();
399
 
9 gabriel 400
 
401
 
402
 
2 gabriel 403
        for (Topic dbTopic : dbTopics) {
404
 
405
            dbCapsules = capsuleDao.selectAllByTopicUuid(dbTopic.getUuid());
406
 
9 gabriel 407
 
2 gabriel 408
            Capsule capsule;
409
            Progress progress;
410
            ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();
411
 
412
            HashMap<String, String> m_li;
413
 
9 gabriel 414
 
415
 
2 gabriel 416
            for(Capsule dbCapsule : dbCapsules)
417
            {
418
                capsule = new Capsule();
419
                capsule.setTopicUuid(dbCapsule.getTopicUuid());
420
                capsule.setUuid(dbCapsule.getUuid());
421
                capsule.setName(dbCapsule.getName());
422
                capsule.setDescription(dbCapsule.getDescription());
423
                capsule.setImage(dbCapsule.getImage());
424
                capsule.setPosition(dbCapsule.getPosition());
425
 
426
 
427
                m_li = new HashMap<>();
428
 
429
 
430
                //m_li.put("copyright",copy);
431
 
432
 
433
                progress = progressDao.selectByCapsuleUuid(capsule.getUuid());
434
                if(progress != null) {
435
                    capsule.setCompleted( progress.getCompleted());
436
                    capsule.setViewSlides(progress.getViewSlides());
437
 
438
                    capsule.setTotalSlides( progress.getTotalSlides());
439
                    capsule.setProgress(progress.getProgress());
440
 
441
                    m_li.put("view", String.valueOf(progress.getViewSlides()));
442
                    m_li.put("total", String.valueOf(progress.getTotalSlides()));
443
                    m_li.put("progress", String.valueOf(progress.getProgress()));
444
                }
445
 
446
 
447
                m_li.put("imagen",capsule.getImage());
448
                m_li.put("nombre",capsule.getName());
11 gabriel 449
                m_li.put("uuid",capsule.getUuid());
450
                m_li.put("description",capsule.getDescription());
2 gabriel 451
 
452
                capsuleList.add(m_li);
453
 
454
            }
455
 
456
 
457
 
458
 
459
        }
460
 
461
    }
462
 
463
 
464
}