Proyectos de Subversion Android Microlearning - Inconcert

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
18 efrain 1
package com.cesams.twogetskills.inconcert.fragment;
2
 
3
import android.os.Bundle;
4
 
5
import androidx.annotation.Nullable;
6
import androidx.fragment.app.Fragment;
7
import androidx.lifecycle.LifecycleOwner;
8
import androidx.lifecycle.Observer;
9
import androidx.lifecycle.ViewModelProvider;
10
import androidx.recyclerview.widget.GridLayoutManager;
11
import androidx.recyclerview.widget.RecyclerView;
12
 
13
import android.text.Editable;
14
import android.text.TextWatcher;
15
import android.util.Log;
16
import android.view.LayoutInflater;
17
import android.view.View;
18
import android.view.ViewGroup;
19
import android.widget.Button;
20
import android.widget.EditText;
21
import android.widget.ImageView;
22
import android.widget.ProgressBar;
23
import android.widget.TextView;
24
import android.widget.Toast;
25
 
26
 
27
import com.cesams.twogetskills.inconcert.Constants;
28
import com.cesams.twogetskills.inconcert.R;
29
import com.cesams.twogetskills.adapter.MyCapsulesAdapter;
30
import com.cesams.twogetskills.inconcert.dao.CapsuleDao;
31
import com.cesams.twogetskills.inconcert.dao.ProgressDao;
32
import com.cesams.twogetskills.inconcert.dao.SlideDao;
33
import com.cesams.twogetskills.inconcert.entity.Capsule;
34
import com.cesams.twogetskills.inconcert.entity.Progress;
35
import com.cesams.twogetskills.inconcert.entity.Slide;
36
import com.cesams.twogetskills.inconcert.library.ImageService;
37
import com.cesams.twogetskills.inconcert.skeleton.ITwoGetSkills;
38
import com.cesams.twogetskills.inconcert.viewmodel.MyCapsulesInProgressViewModel;
39
import com.cesams.twogetskills.inconcert.viewmodel.MyCapsulesViewModel;
40
import com.google.android.material.tabs.TabLayout;
41
 
42
import java.text.DecimalFormat;
43
import java.util.ArrayList;
44
import java.util.List;
45
import org.apache.commons.lang3.StringUtils;
46
 
47
 
48
 
49
public class MyCapsulesFragment extends Fragment implements MyCapsulesAdapter.ClickListener, LifecycleOwner {
50
 
51
    private ITwoGetSkills iTwoGetSkills;
52
    private TextView textViewHelloUsername;
53
    private EditText editTextSearch;
54
    private TextView textViewWelcome;
55
    private RecyclerView recyclerViewCapsules;
56
    private TabLayout tabLayout;
57
    private MyCapsulesAdapter myCapsulesAdapter;
58
    private TextView textViewListingEmpty;
59
    private MyCapsulesViewModel myCapsulesViewModel;
60
    private MyCapsulesInProgressViewModel myCapsulesInProgressViewModel;
61
 
62
    private View cardView;
63
    private TextView cardTextViewStatus;
64
    private TextView cardTextViewTitle;
65
    private ProgressBar cardProgressBar;
66
    private TextView cardTextViewProgress;
67
    private ImageView cardImageView;
68
    private Button cardButtonContinue;
69
 
70
    private DecimalFormat mDecimalFormat;
71
 
72
 
73
    @Override
74
    public void onCreate(@Nullable Bundle savedInstanceState) {
75
        super.onCreate(savedInstanceState);
76
 
77
        this.mDecimalFormat = new DecimalFormat("#.##");
78
    }
79
 
80
    @Override
81
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
82
                             Bundle savedInstanceState) {
83
 
84
        iTwoGetSkills = (ITwoGetSkills) getActivity();
85
 
86
 
87
        View view = inflater.inflate(R.layout.fragment_my_capsules, container, false);
88
        textViewHelloUsername =view.findViewById(R.id.fragment_my_capsules_textview_hello_username);
89
 
90
 
91
        String helloUsername = StringUtils.replace(requireActivity().getString(R.string.my_capsules_hello), "%", iTwoGetSkills.getPreference().getFirstName());
92
        textViewHelloUsername .setText(helloUsername);
93
 
94
        textViewListingEmpty = view.findViewById(R.id.fragment_my_capsules_textview_listing_empty);
95
        textViewWelcome= view.findViewById(R.id.fragment_my_capsules_textview_welcome);
96
        editTextSearch=view.findViewById(R.id.fragment_my_capsules_edit_text_search);
97
        cardView = view.findViewById(R.id.fragment_my_capsules_include_cardview);
98
 
99
        //Tarjeta Principal
100
 
101
        cardTextViewStatus = view.findViewById(R.id.cardview_item_textview_status);
102
        cardTextViewTitle = view.findViewById(R.id.cardview_item_textview_title);
103
        cardImageView = view.findViewById(R.id.cardview_item_imageview);
104
        cardProgressBar = view.findViewById(R.id.cardview_item_progressbar);
105
        cardTextViewProgress = view.findViewById(R.id.cardview_item_textview_progress);
106
        cardButtonContinue = view.findViewById(R.id.cardview_item_button_continue);
107
        cardButtonContinue.setOnClickListener(new View.OnClickListener() {
108
            @Override
109
            public void onClick(View view) {
110
                if (!StringUtils.isEmpty(myCapsulesInProgressViewModel.getCapsule().getTopicUuid()) || !StringUtils.isEmpty(myCapsulesInProgressViewModel.getCapsule().getUuid())) {
111
                    iTwoGetSkills.changeCapsuleActiveSourceNavigationMyCapsules(myCapsulesInProgressViewModel.getCapsule().getTopicUuid(), myCapsulesInProgressViewModel.getCapsule().getUuid());
112
 
113
                }
114
            }
115
        });
116
 
117
 
118
 
119
        myCapsulesViewModel = new ViewModelProvider(requireActivity()).get(MyCapsulesViewModel.class);
120
        myCapsulesAdapter = new MyCapsulesAdapter(requireActivity(), myCapsulesViewModel.getCapsuleArrayList());
121
        myCapsulesAdapter.setClickListener(this);
122
 
123
        Observer<ArrayList<Capsule>> myCapsuleListUpdateObserver = new Observer<ArrayList<Capsule>>() {
124
            @Override
125
            public void onChanged(ArrayList<Capsule> capsuleList) {
126
                myCapsulesAdapter.notifyDataSetChanged();
127
            }
128
        };
129
 
130
        myCapsulesViewModel.getCapsuleMutableLiveData().observe(requireActivity(), myCapsuleListUpdateObserver);
131
 
132
 
133
        myCapsulesInProgressViewModel = new ViewModelProvider(requireActivity()).get(MyCapsulesInProgressViewModel.class);
134
        Observer<Capsule> myCapsuleInProgressUpdateObserver = new Observer<Capsule>() {
135
            @Override
136
            public void onChanged(Capsule capsuleInProgress) {
137
                if (capsuleInProgress == null || StringUtils.isEmpty(capsuleInProgress.getUuid()))  {
138
                    cardTextViewStatus.setText(getContext().getString(R.string.my_capsules_capsule_in_progress_not_found));
139
                    cardTextViewTitle.setText("");
140
                    cardProgressBar.setProgress(0);
141
                    cardTextViewProgress.setText("0 %");
142
                    cardButtonContinue.setVisibility(View.GONE);
143
                } else {
144
 
145
                    String title = StringUtils.trim(capsuleInProgress.getName());
146
                    title = title.length() > Constants.TITLE_MAX_LENGTH ? StringUtils.substring(title, 0, Constants.TITLE_MAX_LENGTH) + "...": title;
147
 
148
                    cardTextViewStatus.setText(getContext().getString(R.string.my_capsules_capsule_in_progress_found));
149
                    cardTextViewTitle.setText(title);
150
                    cardProgressBar.setProgress((int) capsuleInProgress.getProgress());
151
                    cardTextViewProgress.setText(mDecimalFormat.format(capsuleInProgress.getProgress()) + " %");
152
 
153
                    cardButtonContinue.setVisibility(View.VISIBLE);
154
 
155
                    ImageService.retrieve(getContext(), capsuleInProgress.getImage(), cardImageView);
156
                }
157
            }
158
        };
159
 
160
        myCapsulesInProgressViewModel.getCapsuleMutableLiveData().observe(requireActivity(), myCapsuleInProgressUpdateObserver);
161
 
162
 
163
 
164
 
165
        tabLayout = view.findViewById(R.id.fragment_my_capsules_tabLayout);
166
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
167
            @Override
168
            public void onTabSelected(TabLayout.Tab tab) {
169
 
170
                String search = "";
171
                if (editTextSearch != null) {
172
                    search = StringUtils.trimToEmpty(editTextSearch.getText().toString());
173
                }
174
 
175
                int tabPosition = Constants.MY_CAPSULES_TAB_PENDING;
176
                if (tabLayout != null) {
177
                    tabPosition = tabLayout.getSelectedTabPosition();
178
                }
179
 
180
 
181
                loadData(tabPosition, search);
182
            }
183
 
184
            @Override
185
            public void onTabUnselected(TabLayout.Tab tab) {
186
 
187
            }
188
 
189
            @Override
190
            public void onTabReselected(TabLayout.Tab tab) {
191
 
192
            }
193
        });
194
 
195
        editTextSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
196
            @Override
197
            public void onFocusChange(View v, boolean hasFocus) {
198
                if(hasFocus)  {
199
                    cardView.setVisibility(View.GONE);
200
                    textViewHelloUsername.setVisibility(View.GONE);
201
                    textViewWelcome.setVisibility(View.GONE);
202
                }
203
 
204
            }
205
        });
206
 
207
        editTextSearch.addTextChangedListener(new TextWatcher() {
208
            @Override
209
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
210
 
211
            }
212
 
213
            @Override
214
            public void onTextChanged(CharSequence s, int start, int before, int count) {
215
                String search = "";
216
                if (editTextSearch != null) {
217
                    search = StringUtils.trimToEmpty(editTextSearch.getText().toString());
218
                }
219
 
220
                int tabPosition = Constants.MY_CAPSULES_TAB_PENDING;
221
                if (tabLayout != null) {
222
                    tabPosition = tabLayout.getSelectedTabPosition();
223
                }
224
 
225
 
226
                loadData(tabPosition, search);
227
            }
228
 
229
            @Override
230
            public void afterTextChanged(Editable s) {
231
 
232
            }
233
        });
234
 
235
        GridLayoutManager recyclerViewCapsulesLayout = new GridLayoutManager(getContext(),1,GridLayoutManager.VERTICAL, false);
236
        recyclerViewCapsules = view.findViewById(R.id.fragment_my_capsules_listing_capsules);
237
        recyclerViewCapsules.setLayoutManager(recyclerViewCapsulesLayout);
238
        recyclerViewCapsules.setAdapter(myCapsulesAdapter);
239
        recyclerViewCapsules.setOnFlingListener(new RecyclerView.OnFlingListener() {
240
            @Override
241
            public boolean onFling(int velocityX, int velocityY) {
242
            if (recyclerViewCapsules.canScrollVertically(-1) ) {
243
                cardView.setVisibility(View.GONE);
244
                textViewHelloUsername.setVisibility(View.GONE);
245
                textViewWelcome.setVisibility(View.GONE);
246
            } else {
247
                cardView.setVisibility(View.VISIBLE);
248
                textViewHelloUsername.setVisibility(View.VISIBLE);
249
                textViewWelcome.setVisibility(View.VISIBLE);
250
            }
251
 
252
            return false;
253
            }
254
        });
255
 
256
 
257
     /*   categorizados.setOnScrollChangeListener(new View.OnScrollChangeListener() {
258
            @Override
259
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
260
 
261
                if (!categorizados.canScrollVertically(-1)) {
262
                 //   Toast.makeText(getActivity(), "Last", Toast.LENGTH_LONG).show();
263
                    capsulas.setVisibility(View.VISIBLE);
264
                    username.setVisibility(View.VISIBLE);
265
                    textowelcome.setVisibility(View.VISIBLE);
266
                    Log.e("On scroll"," se activa");
267
                }
268
 
269
            }
270
        });
271
*/
272
        getActivity().runOnUiThread(() -> {
273
 
274
 
275
            String message;
276
            if (myCapsulesViewModel.getCapsuleArrayList().size() == 1) {
277
                message = getContext().getString(R.string.common_one_capsule_available);
278
            } else {
279
                message = getContext().getString(R.string.common_more_capsules_available);
280
                message = StringUtils.replace(message, "%", String.valueOf(myCapsulesViewModel.getCapsuleArrayList().size()));
281
            }
282
            Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
283
 
284
 
285
            iTwoGetSkills.saveNotificationCenter(getContext().getString(R.string.common_content_available),"",message);
286
 
287
        });
288
        iTwoGetSkills.showNavigationAndToolbar();
289
 
290
        return view;
291
    }
292
 
293
 
294
 
295
    @Override
296
    public void onStart() {
297
        super.onStart();
298
        getActivity().runOnUiThread(() -> {
299
 
300
            String search = "";
301
            if (editTextSearch != null) {
302
                search = StringUtils.trimToEmpty(editTextSearch.getText().toString());
303
            }
304
 
305
            int tabPosition = Constants.MY_CAPSULES_TAB_PENDING;
306
            if (tabLayout != null) {
307
                tabPosition = tabLayout.getSelectedTabPosition();
308
            }
309
 
310
            loadData(tabPosition, search);
311
            loadCapsuleInProgerss();
312
        });
313
    }
314
 
315
    @Override
316
    public void onResume() {
317
        super.onResume();
318
        getActivity().runOnUiThread(() -> {
319
 
320
            String search = "";
321
            if (editTextSearch != null) {
322
                search = StringUtils.trimToEmpty(editTextSearch.getText().toString());
323
            }
324
 
325
            int tabPosition = Constants.MY_CAPSULES_TAB_PENDING;
326
            if (tabLayout != null) {
327
                tabPosition = tabLayout.getSelectedTabPosition();
328
            }
329
 
330
            loadData(tabPosition, search);
331
            loadCapsuleInProgerss();
332
        });
333
    }
334
 
335
    @Override
336
    public void onHiddenChanged(boolean hidden) {
337
        super.onHiddenChanged(hidden);
338
 
339
 
340
        if(!hidden) {
341
 
342
            getActivity().runOnUiThread(() -> {
343
 
344
                String search = "";
345
                if (editTextSearch != null) {
346
                    search = StringUtils.trimToEmpty(editTextSearch.getText().toString());
347
                }
348
 
349
                int tabPosition = Constants.MY_CAPSULES_TAB_PENDING;
350
                if (tabLayout != null) {
351
                    tabPosition = tabLayout.getSelectedTabPosition();
352
                }
353
 
354
                loadData(tabPosition, search);
355
                loadCapsuleInProgerss();
356
            });
357
        }
358
    }
359
 
360
    private void loadCapsuleInProgerss()
361
    {
362
        CapsuleDao capsuleDao = iTwoGetSkills.getDatabase().getCapsuleDao();
363
        Capsule capsule = capsuleDao.selectLastInProgress(iTwoGetSkills.getPreference().getUserUuid());
364
 
365
        if (capsule == null) {
366
            myCapsulesInProgressViewModel.getCapsule().setUuid("");
367
            myCapsulesInProgressViewModel.getCapsule().setTopicUuid("");
368
            myCapsulesInProgressViewModel.getCapsule().setName("");
369
            myCapsulesInProgressViewModel.getCapsule().setDescription("");
370
            myCapsulesInProgressViewModel.getCapsule().setPosition(0);
371
            myCapsulesInProgressViewModel.getCapsule().setImage("");
372
            myCapsulesInProgressViewModel.getCapsule().setCompleted(0);
373
            myCapsulesInProgressViewModel.getCapsule().setTotalComments(0);
374
            myCapsulesInProgressViewModel.getCapsule().setLinkComments("");
375
            myCapsulesInProgressViewModel.getCapsule().setLinkCommentAdd("");
376
            myCapsulesInProgressViewModel.getCapsule().setViewSlides(0);
377
            myCapsulesInProgressViewModel.getCapsule().setTotalSlides(0);
378
            myCapsulesInProgressViewModel.getCapsule().setProgress(0);
379
            myCapsulesInProgressViewModel.getCapsule().setAddedOn("");
380
            myCapsulesInProgressViewModel.getCapsule().setUpdatedOn("");
381
 
382
 
383
        } else {
384
            myCapsulesInProgressViewModel.getCapsule().setUuid(capsule.getUuid());
385
            myCapsulesInProgressViewModel.getCapsule().setTopicUuid(capsule.getTopicUuid());
386
            myCapsulesInProgressViewModel.getCapsule().setName(capsule.getName());
387
            myCapsulesInProgressViewModel.getCapsule().setDescription(capsule.getDescription());
388
            myCapsulesInProgressViewModel.getCapsule().setPosition(capsule.getPosition());
389
            myCapsulesInProgressViewModel.getCapsule().setImage(capsule.getImage());
390
            myCapsulesInProgressViewModel.getCapsule().setCompleted(capsule.getCompleted());
391
            myCapsulesInProgressViewModel.getCapsule().setTotalComments(capsule.getTotalComments());
392
            myCapsulesInProgressViewModel.getCapsule().setLinkComments(capsule.getLinkComments());
393
            myCapsulesInProgressViewModel.getCapsule().setLinkCommentAdd(capsule.getLinkCommentAdd());
394
            myCapsulesInProgressViewModel.getCapsule().setViewSlides(capsule.getViewSlides());
395
            myCapsulesInProgressViewModel.getCapsule().setTotalSlides(capsule.getTotalSlides());
396
            myCapsulesInProgressViewModel.getCapsule().setProgress(capsule.getProgress());
397
            myCapsulesInProgressViewModel.getCapsule().setAddedOn(capsule.getAddedOn());
398
            myCapsulesInProgressViewModel.getCapsule().setUpdatedOn(capsule.getUpdatedOn());
399
 
400
            ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();
401
            Progress progress = progressDao.selectByCapsuleUuid(capsule.getUuid());
402
 
403
            if (progress != null) {
404
                capsule.setViewSlides(progress.getViewSlides());
405
                capsule.setTotalSlides(progress.getTotalSlides());
406
                capsule.setProgress(progress.getProgress());
407
 
408
 
409
                myCapsulesInProgressViewModel.getCapsule().setViewSlides(progress.getViewSlides());
410
                myCapsulesInProgressViewModel.getCapsule().setTotalSlides(progress.getTotalSlides());
411
                myCapsulesInProgressViewModel.getCapsule().setProgress(progress.getProgress());
412
            }
413
        }
414
 
415
        myCapsulesInProgressViewModel.getCapsuleMutableLiveData().setValue(capsule);
416
 
417
 
418
 
419
 
420
    }
421
 
422
 
423
 
424
    private void loadData(int tabPosition, String search)
425
    {
426
 
427
        Capsule capsule;
428
        List<Capsule> dbCapsules;
429
        CapsuleDao capsuleDao = iTwoGetSkills.getDatabase().getCapsuleDao();
430
 
431
        SlideDao slideDao = iTwoGetSkills.getDatabase().getSlideDao();
432
        ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();
433
        Progress progress;
434
        switch (tabPosition)
435
        {
436
            case Constants.MY_CAPSULES_TAB_IN_PROGRESS :
437
                if (StringUtils.isEmpty(search)) {
438
                    dbCapsules = capsuleDao.selectAllInProgress(iTwoGetSkills.getPreference().getUserUuid());
439
                } else {
440
                    dbCapsules = capsuleDao.selectAllInProgressWithSearch(iTwoGetSkills.getPreference().getUserUuid(), '%' + search + '%');
441
                }
442
               break;
443
 
444
            case Constants.MY_CAPSULES_TAB_COMPLETED:
445
                if (StringUtils.isEmpty(search)) {
446
                    dbCapsules = capsuleDao.selectAllCompleted(iTwoGetSkills.getPreference().getUserUuid());
447
                } else {
448
                    dbCapsules = capsuleDao.selectAllCompletedWithSearch(iTwoGetSkills.getPreference().getUserUuid(), '%' + search + '%');
449
                }
450
                break;
451
 
452
            default :
453
                if (StringUtils.isEmpty(search)) {
454
                    dbCapsules = capsuleDao.selectAllPending(iTwoGetSkills.getPreference().getUserUuid());
455
                } else {
456
                    dbCapsules = capsuleDao.selectAllPendingWithSearch(iTwoGetSkills.getPreference().getUserUuid(), '%' + search + '%');
457
                }
458
                break;
459
 
460
        }
461
        myCapsulesViewModel.getCapsuleArrayList().clear();
462
        for (Capsule dbCapsule: dbCapsules) {
463
            capsule = new Capsule();
464
            capsule.setUuid(dbCapsule.getUuid());
465
            capsule.setTopicUuid(dbCapsule.getTopicUuid());
466
            capsule.setName(dbCapsule.getName());
467
            capsule.setDescription(dbCapsule.getDescription());
468
            capsule.setImage(dbCapsule.getImage());
469
            capsule.setPosition(dbCapsule.getPosition());
470
            capsule.setLinkCommentAdd(dbCapsule.getLinkCommentAdd());
471
            capsule.setLinkComments(dbCapsule.getLinkComments());
472
            capsule.setTotalComments(dbCapsule.getTotalComments());
473
            capsule.setTotalRating(dbCapsule.getTotalRating());
474
            capsule.setTotalSlides(0);
475
            capsule.setViewSlides(0);
476
            capsule.setProgress(0);
477
            capsule.setAddedOn(dbCapsule.getAddedOn());
478
            capsule.setUpdatedOn(dbCapsule.getUpdatedOn());
479
 
480
 
481
            progress = progressDao.selectByCapsuleUuid(capsule.getUuid());
482
            if (progress == null) {
483
                capsule.setTotalSlides(slideDao.getCountByCapsuleUuid(capsule.getUuid()).getCount());
484
            } else {
485
                capsule.setProgress(progress.getProgress());
486
                capsule.setViewSlides(progress.getViewSlides());
487
                capsule.setTotalSlides(progress.getTotalSlides());
488
            }
489
 
490
            myCapsulesViewModel.getCapsuleArrayList().add(capsule);
491
        }
492
 
493
        myCapsulesViewModel.getCapsuleMutableLiveData().setValue(myCapsulesViewModel.getCapsuleArrayList());
494
 
495
        textViewListingEmpty.setVisibility(dbCapsules.size() == 0 ?  View.VISIBLE : View.GONE);
496
   }
497
 
498
 
499
    @Override
500
    public void onItemClick(int position, View v) {
501
 
502
        iTwoGetSkills.hideKeyboard(v);
503
 
504
        Capsule capsule = myCapsulesViewModel.getCapsuleArrayList().get(position);
505
        iTwoGetSkills.changeCapsuleActiveSourceNavigationMyCapsules(capsule.getTopicUuid(), capsule.getUuid());
506
    }
507
}