Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

Ir a la última revisión | | 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
 
13 gabriel 11
import android.text.Editable;
12
import android.text.TextWatcher;
2 gabriel 13
import android.util.Log;
14
import android.view.LayoutInflater;
11 gabriel 15
import android.view.MotionEvent;
2 gabriel 16
import android.view.View;
17
import android.view.ViewGroup;
11 gabriel 18
import android.widget.AdapterView;
9 gabriel 19
import android.widget.Button;
8 gabriel 20
import android.widget.EditText;
9 gabriel 21
import android.widget.ImageView;
22
import android.widget.ProgressBar;
2 gabriel 23
import android.widget.TextView;
24
 
9 gabriel 25
import com.bumptech.glide.Glide;
26
import com.bumptech.glide.load.engine.DiskCacheStrategy;
27
import com.bumptech.glide.load.model.GlideUrl;
28
import com.bumptech.glide.load.model.LazyHeaders;
29
import com.bumptech.glide.request.RequestOptions;
30
import com.cesams.twogetskills.Constants;
2 gabriel 31
import com.cesams.twogetskills.R;
32
import com.cesams.twogetskills.adapter.CapsuleListViewAdapter;
4 gabriel 33
import com.cesams.twogetskills.adapter.TabsCapsulesAdapter;
2 gabriel 34
import com.cesams.twogetskills.dao.CapsuleDao;
35
import com.cesams.twogetskills.dao.ProgressDao;
36
import com.cesams.twogetskills.dao.TopicDao;
37
import com.cesams.twogetskills.entity.Capsule;
38
import com.cesams.twogetskills.entity.Progress;
39
import com.cesams.twogetskills.entity.Topic;
9 gabriel 40
import com.cesams.twogetskills.library.MD5;
2 gabriel 41
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
12 gabriel 42
import com.google.android.material.tabs.TabLayout;
2 gabriel 43
 
44
import java.util.ArrayList;
9 gabriel 45
import java.util.Calendar;
2 gabriel 46
import java.util.HashMap;
47
import java.util.List;
9 gabriel 48
import java.util.Random;
49
import java.util.TimeZone;
2 gabriel 50
 
51
/**
52
 * A simple {@link Fragment} subclass.
53
 * Use the {@link WelcomeFragment#newInstance} factory method to
54
 * create an instance of this fragment.
55
 */
11 gabriel 56
public class WelcomeFragment extends Fragment implements LifecycleOwner {
2 gabriel 57
 
10 gabriel 58
    TextView username, titulotarjeta, progresoporcentaje, empezar; ProgressBar progresotarjeta;
9 gabriel 59
    private ITwoGetSkills iTwoGetSkills; View card;
2 gabriel 60
    ArrayList<HashMap<String, String>> capsuleList;
11 gabriel 61
    ImageView imagetarjeta;
62
    TabsCapsulesAdapter adapter2;
63
    RecyclerView categorizados;
9 gabriel 64
    EditText busqueda; TextView textowelcome; Button continuar;
13 gabriel 65
    TabLayout tabs; String tabactual="pendientes";
2 gabriel 66
 
67
    public WelcomeFragment() {
68
        // Required empty public constructor
69
    }
70
 
71
    public static WelcomeFragment newInstance() {
72
        WelcomeFragment fragment = new WelcomeFragment();
73
        return fragment;
74
    }
75
 
76
    @Override
77
    public void onCreate(Bundle savedInstanceState) {
78
        super.onCreate(savedInstanceState);
79
          }
80
 
81
    @Override
82
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
83
                             Bundle savedInstanceState) {
84
 
8 gabriel 85
 
2 gabriel 86
        iTwoGetSkills = (ITwoGetSkills) getActivity();
8 gabriel 87
        iTwoGetSkills.showNavigationAndToolbar();
2 gabriel 88
 
89
        View view= inflater.inflate(R.layout.fragment_welcome, container, false);
90
 
91
        username=view.findViewById(R.id.textView14);
8 gabriel 92
        textowelcome= view.findViewById(R.id.textView15);
93
        busqueda=view.findViewById(R.id.busqueda);
12 gabriel 94
        card= view.findViewById(R.id.include); empezar= view.findViewById(R.id.titlenotifi); tabs=view.findViewById(R.id.tabLayout);
8 gabriel 95
        capsuleList = new ArrayList<>();
2 gabriel 96
 
9 gabriel 97
        //Tarjeta Principal
11 gabriel 98
        titulotarjeta = view.findViewById(R.id.description); progresotarjeta= view.findViewById(R.id.progressBar2); imagetarjeta= view.findViewById(R.id.imagelist);
9 gabriel 99
        continuar = view.findViewById(R.id.button); progresoporcentaje = view.findViewById(R.id.textView16);
100
 
101
 
2 gabriel 102
        username.setText("¡Hola, "+(iTwoGetSkills.getPreference().getFirstName() +"!"));
103
 
12 gabriel 104
        tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
105
            @Override
106
            public void onTabSelected(TabLayout.Tab tab) {
107
                int position=tab.getPosition();
2 gabriel 108
 
12 gabriel 109
                capsuleList.clear();
110
                adapter2.notifyDataSetChanged();
2 gabriel 111
 
12 gabriel 112
                if(position==0)
113
                {
13 gabriel 114
                    tabactual="pendientes";
115
                    loadData(tabactual,"");
12 gabriel 116
                    adapter2.notifyDataSetChanged();
117
                }
118
                else if(position==1)
119
                {
13 gabriel 120
                    tabactual="cursando";
121
                    loadData("cursando","");
12 gabriel 122
                    adapter2.notifyDataSetChanged();
123
                }
124
                else if(position==2)
125
                {
13 gabriel 126
                    tabactual="finalizados";
127
                    loadData("finalizados","");
12 gabriel 128
                    adapter2.notifyDataSetChanged();
129
                }
130
            }
131
 
132
            @Override
133
            public void onTabUnselected(TabLayout.Tab tab) {
134
 
135
            }
136
 
137
            @Override
138
            public void onTabReselected(TabLayout.Tab tab) {
139
 
140
            }
141
        });
142
 
13 gabriel 143
        loadData("pendientes","");
12 gabriel 144
 
8 gabriel 145
        busqueda.setOnFocusChangeListener(new View.OnFocusChangeListener() {
146
            @Override
147
            public void onFocusChange(View v, boolean hasFocus) {
148
                if(hasFocus)
149
                {
9 gabriel 150
                        card.setVisibility(View.GONE);
8 gabriel 151
                        username.setVisibility(View.GONE);
152
                        textowelcome.setVisibility(View.GONE);
153
                }
154
 
155
            }
156
        });
157
 
13 gabriel 158
        busqueda.addTextChangedListener(new TextWatcher() {
159
            @Override
160
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
8 gabriel 161
 
13 gabriel 162
            }
163
 
164
            @Override
165
            public void onTextChanged(CharSequence s, int start, int before, int count) {
166
                Log.e("El texto","buscado: "+s);
167
                capsuleList.clear();
168
                adapter2.notifyDataSetChanged();
169
                loadData(tabactual, s.toString());
170
            }
171
 
172
            @Override
173
            public void afterTextChanged(Editable s) {
174
 
175
 
176
 
177
            }
178
        });
179
 
180
 
4 gabriel 181
        adapter2 = new TabsCapsulesAdapter(getContext(),capsuleList);
2 gabriel 182
 
183
 
184
 
4 gabriel 185
        categorizados = view.findViewById(R.id.categorizados);
186
        GridLayoutManager layoutcategoria = new GridLayoutManager(getContext(),1,GridLayoutManager.VERTICAL, false);
187
        categorizados.setLayoutManager(layoutcategoria);
188
        categorizados.setAdapter(adapter2);
189
 
11 gabriel 190
 
191
 
192
 
8 gabriel 193
        categorizados.setOnFlingListener(new RecyclerView.OnFlingListener() {
194
            @Override
195
            public boolean onFling(int velocityX, int velocityY) {
196
 
197
                if (categorizados.canScrollVertically(-1) ) {
198
 
199
                    Log.e("Se activa", "On fling");
9 gabriel 200
                    card.setVisibility(View.GONE);
8 gabriel 201
                    username.setVisibility(View.GONE);
202
                    textowelcome.setVisibility(View.GONE);
203
                }else
204
                {
9 gabriel 205
                    card.setVisibility(View.VISIBLE);
8 gabriel 206
                    username.setVisibility(View.VISIBLE);
207
                    textowelcome.setVisibility(View.VISIBLE);
208
                }
209
 
210
                return false;
211
            }
212
        });
213
 
11 gabriel 214
 
8 gabriel 215
     /*   categorizados.setOnScrollChangeListener(new View.OnScrollChangeListener() {
216
            @Override
217
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
218
 
219
                if (!categorizados.canScrollVertically(-1)) {
220
                 //   Toast.makeText(getActivity(), "Last", Toast.LENGTH_LONG).show();
221
                    capsulas.setVisibility(View.VISIBLE);
222
                    username.setVisibility(View.VISIBLE);
223
                    textowelcome.setVisibility(View.VISIBLE);
224
                    Log.e("On scroll"," se activa");
225
                }
226
 
227
            }
228
        });
229
*/
2 gabriel 230
        return view;
231
    }
232
 
9 gabriel 233
    @Override
234
    public void onResume() {
235
        super.onResume();
236
        loadContinuecard();
237
    }
2 gabriel 238
 
11 gabriel 239
 
240
 
9 gabriel 241
    @Override
242
    public void onHiddenChanged(boolean hidden) {
243
        super.onHiddenChanged(hidden);
244
 
245
        Log.e("TAG", "onHiddenChanged : " + (hidden ? "true" : "false"));
246
 
247
        if(!hidden) {
248
            loadContinuecard();
249
        }
250
    }
251
 
252
    private void loadContinuecard(){
253
 
254
        Log.e("Cargo datos"," en tarjeta principal");
255
 
256
        //Capsula de primera tarjeta
257
        CapsuleDao capsuleDao = iTwoGetSkills.getDatabase().getCapsuleDao();
258
 
259
        Progress progress;
260
        ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();
261
 
262
        List<Capsule> dbCapsules= capsuleDao.selectAll();
263
        for(Capsule dbCapsule : dbCapsules)
264
        {
265
            progress = progressDao.selectByCapsuleUuid(dbCapsule.getUuid());
266
 
267
            if (progress != null) {
268
 
10 gabriel 269
                    if (progress.getProgress() < 100) {
270
                        Log.e("Es menor"," a 100");
9 gabriel 271
 
272
                        titulotarjeta.setText(dbCapsule.getName());
10 gabriel 273
                        progresotarjeta.setVisibility(View.VISIBLE);
274
                        progresoporcentaje.setVisibility(View.VISIBLE);
9 gabriel 275
                        progresotarjeta.setProgress((int) progress.getProgress());
276
                        progresoporcentaje.setText("" + progress.getProgress() + " %");
10 gabriel 277
                        empezar.setText("En curso");
9 gabriel 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
 
10 gabriel 316
            }
317
 
318
 
319
        }
320
 
321
        if(titulotarjeta.getText().equals(""))
322
        {
323
            Log.e("Nombre","vacio");
324
 
325
            //Despues del recorrido no se encontro progreso en ninguna capsula, por lo cual la tarjeta queda vacia
326
            // vamos a llenarla con la ultima capsula, que debe ser nueva al no tener progreso
327
 
328
            for(Capsule dbCapsule : dbCapsules)
329
            {
330
 
331
                progress = progressDao.selectByCapsuleUuid(dbCapsule.getUuid());
332
 
333
                if (progress == null) {
334
 
335
                        Log.e("Es nueva","");
336
 
337
                        titulotarjeta.setText(dbCapsule.getName());
338
                        progresotarjeta.setVisibility(View.INVISIBLE);
339
                        progresoporcentaje.setVisibility(View.INVISIBLE);
340
                        empezar.setText("Nueva");
341
                        continuar.setText("Ver cápsula");
342
 
343
                        iTwoGetSkills.getPreference().setCapsuleUuidActive(dbCapsule.getUuid());
344
                        iTwoGetSkills.getPreference().setTopicUuidActive(dbCapsule.getTopicUuid());
345
 
346
                        if (dbCapsule.getImage() != null && !dbCapsule.getImage().equals("")) {
347
 
348
                            TimeZone timeZone = TimeZone.getTimeZone("UTC");
349
                            Calendar calendar = Calendar.getInstance(timeZone);
350
                            TimeZone tz = calendar.getTimeZone();
351
                            int created = (int) (calendar.getTimeInMillis() / 1000);
352
 
353
                            Random random = new Random(created);
354
                            int rand = 1000 + random.nextInt(8999);
355
 
356
 
357
                            String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();
358
                            String password = iTwoGetSkills.getPreference().getPassword();
359
 
360
 
361
                            String secret = MD5.generar(password + ':' + created + ':' + rand);
362
                            GlideUrl url = new GlideUrl(dbCapsule.getImage(), new LazyHeaders.Builder()
363
                                    .addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
364
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid)
365
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret)
366
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created))
367
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand))
368
                                    .build());
369
 
370
                            RequestOptions options = new RequestOptions()
371
                                    .diskCacheStrategy(DiskCacheStrategy.ALL);
372
 
373
                            Glide.with(getContext()).load(url)
374
                                    .thumbnail()
375
                                    .apply(options)
376
                                    .into(imagetarjeta);
377
                        }
378
 
379
                    }
380
 
381
                /*
382
                if (progress != null) {
383
 
384
                    double progre = progress.getProgress();
385
 
386
                    if (progre >= 100) {
387
                        Log.e("Es mayor"," a 100");
388
 
389
                    } else {
390
 
391
                        Log.e("Es nueva","");
392
 
393
                        titulotarjeta.setText(dbCapsule.getName());
394
                        progresotarjeta.setVisibility(View.INVISIBLE);
395
                        progresoporcentaje.setVisibility(View.INVISIBLE);
396
                        empezar.setText("Nueva");
397
                        continuar.setText("Ver cápsula");
398
 
399
                        iTwoGetSkills.getPreference().setCapsuleUuidActive(dbCapsule.getUuid());
400
                        iTwoGetSkills.getPreference().setTopicUuidActive(dbCapsule.getTopicUuid());
401
 
402
                        if (dbCapsule.getImage() != null && !dbCapsule.getImage().equals("")) {
403
 
404
                            TimeZone timeZone = TimeZone.getTimeZone("UTC");
405
                            Calendar calendar = Calendar.getInstance(timeZone);
406
                            TimeZone tz = calendar.getTimeZone();
407
                            int created = (int) (calendar.getTimeInMillis() / 1000);
408
 
409
                            Random random = new Random(created);
410
                            int rand = 1000 + random.nextInt(8999);
411
 
412
 
413
                            String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();
414
                            String password = iTwoGetSkills.getPreference().getPassword();
415
 
416
 
417
                            String secret = MD5.generar(password + ':' + created + ':' + rand);
418
                            GlideUrl url = new GlideUrl(dbCapsule.getImage(), new LazyHeaders.Builder()
419
                                    .addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
420
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid)
421
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret)
422
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created))
423
                                    .addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand))
424
                                    .build());
425
 
426
                            RequestOptions options = new RequestOptions()
427
                                    .diskCacheStrategy(DiskCacheStrategy.ALL);
428
 
429
                            Glide.with(getContext()).load(url)
430
                                    .thumbnail()
431
                                    .apply(options)
432
                                    .into(imagetarjeta);
433
                        }
434
 
435
                    }
9 gabriel 436
                }
10 gabriel 437
*/
438
            }
9 gabriel 439
 
10 gabriel 440
 
441
 
9 gabriel 442
        }
443
 
10 gabriel 444
 
445
 
9 gabriel 446
        continuar.setOnClickListener(v -> {
447
 
448
            iTwoGetSkills.invokeFragment(Constants.IDX_FRAGMENT_SLIDES);
449
        });
450
 
451
        //Fin de primer tarjeta
452
 
453
    }
454
 
13 gabriel 455
    private void loadData(String tabposition, String buscar)
2 gabriel 456
    {
457
 
13 gabriel 458
      //  Log.e("Las tabs", "position"+tabposition);
12 gabriel 459
 
2 gabriel 460
        TopicDao topicDao = iTwoGetSkills.getDatabase().getTopicDao();
461
        ArrayList<Topic> dbTopics = (ArrayList<Topic>) topicDao.selectAll();
462
 
463
        List<Capsule> dbCapsules;
464
        CapsuleDao capsuleDao = iTwoGetSkills.getDatabase().getCapsuleDao();
465
 
9 gabriel 466
 
467
 
468
 
2 gabriel 469
        for (Topic dbTopic : dbTopics) {
470
 
471
            dbCapsules = capsuleDao.selectAllByTopicUuid(dbTopic.getUuid());
472
 
9 gabriel 473
 
2 gabriel 474
            Capsule capsule;
475
            Progress progress;
476
            ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();
477
 
478
            HashMap<String, String> m_li;
479
 
9 gabriel 480
 
481
 
2 gabriel 482
            for(Capsule dbCapsule : dbCapsules)
483
            {
484
                capsule = new Capsule();
485
                capsule.setTopicUuid(dbCapsule.getTopicUuid());
486
                capsule.setUuid(dbCapsule.getUuid());
487
                capsule.setName(dbCapsule.getName());
488
                capsule.setDescription(dbCapsule.getDescription());
489
                capsule.setImage(dbCapsule.getImage());
490
                capsule.setPosition(dbCapsule.getPosition());
491
 
492
 
493
                m_li = new HashMap<>();
494
 
495
 
496
                //m_li.put("copyright",copy);
497
 
498
 
499
                progress = progressDao.selectByCapsuleUuid(capsule.getUuid());
500
                if(progress != null) {
501
                    capsule.setCompleted( progress.getCompleted());
502
                    capsule.setViewSlides(progress.getViewSlides());
503
 
504
                    capsule.setTotalSlides( progress.getTotalSlides());
505
                    capsule.setProgress(progress.getProgress());
506
 
507
                    m_li.put("view", String.valueOf(progress.getViewSlides()));
508
                    m_li.put("total", String.valueOf(progress.getTotalSlides()));
509
                    m_li.put("progress", String.valueOf(progress.getProgress()));
510
                }
511
 
512
 
513
                m_li.put("imagen",capsule.getImage());
514
                m_li.put("nombre",capsule.getName());
11 gabriel 515
                m_li.put("uuid",capsule.getUuid());
516
                m_li.put("description",capsule.getDescription());
2 gabriel 517
 
13 gabriel 518
                //Lleno la lista segun el requerimiento de filtro accionado por el usuario
519
 
12 gabriel 520
                if(tabposition.equals("pendientes") && progress==null){
2 gabriel 521
 
13 gabriel 522
                    if(buscar.equals(""))
523
                    {
524
                        capsuleList.add(m_li);
12 gabriel 525
 
13 gabriel 526
                    }
527
                    else
528
                    {
529
                        if (capsule.getName().matches("(?i).*"+buscar+".*")){
530
                            capsuleList.add(m_li);
531
                    }
532
 
533
                    }
534
 
12 gabriel 535
                }
536
                else if (tabposition.equals("cursando") && progress != null && progress.getProgress()<100){
537
 
13 gabriel 538
                    if(buscar.equals(""))
539
                    {
540
                        capsuleList.add(m_li);
12 gabriel 541
 
13 gabriel 542
                    }
543
                    else
544
                    {
545
                        if (capsule.getName().matches("(?i).*"+buscar+".*")){
546
                            capsuleList.add(m_li);
547
                        }
548
 
549
                    }
550
 
2 gabriel 551
            }
12 gabriel 552
                else if (tabposition.equals("finalizados") && progress != null && progress.getProgress()>=100){
2 gabriel 553
 
13 gabriel 554
                    if(buscar.equals(""))
555
                    {
556
                        capsuleList.add(m_li);
2 gabriel 557
 
13 gabriel 558
                    }
559
                    else
560
                    {
561
                        if (capsule.getName().matches("(?i).*"+buscar+".*")){
562
                            capsuleList.add(m_li);
563
                        }
564
 
565
                    }
566
 
12 gabriel 567
                }
2 gabriel 568
 
12 gabriel 569
            }
2 gabriel 570
 
12 gabriel 571
 
572
 
573
 
2 gabriel 574
        }
575
 
576
    }
577
 
578
 
579
}