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