Proyectos de Subversion Android Microlearning

Rev

Rev 29 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
package com.cesams.twogetskills.fragment;
2
 
3
 
4
import android.graphics.Color;
5
import android.graphics.Typeface;
6
import android.os.Bundle;
7
 
8
import androidx.annotation.NonNull;
9
import androidx.annotation.Nullable;
10
import androidx.fragment.app.Fragment;
11
import androidx.lifecycle.Observer;
12
import androidx.lifecycle.ViewModelProvider;
13
 
14
import android.text.TextUtils;
15
import android.util.Log;
16
import android.view.LayoutInflater;
17
import android.view.Menu;
18
import android.view.MenuInflater;
19
import android.view.View;
20
import android.view.ViewGroup;
21
import android.widget.TextView;
22
 
44 efrain 23
import com.cesams.twogetskills.Constants;
1 efrain 24
import com.cesams.twogetskills.R;
25
import com.cesams.twogetskills.dao.CapsuleDao;
26
import com.cesams.twogetskills.dao.ProgressDao;
27
 
28
import com.cesams.twogetskills.dao.TopicDao;
29
import com.cesams.twogetskills.entity.Capsule;
30
import com.cesams.twogetskills.entity.Company;
31
import com.cesams.twogetskills.entity.Progress;
32
import com.cesams.twogetskills.entity.Topic;
33
import com.cesams.twogetskills.room.ResultCount;
34
import com.cesams.twogetskills.room.ResultTotalInt;
35
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
36
import com.cesams.twogetskills.viewdata.ProgressViewData;
37
import com.cesams.twogetskills.viewmodel.ProgressViewModel;
38
import com.github.mikephil.charting.charts.PieChart;
39
import com.github.mikephil.charting.data.PieData;
40
import com.github.mikephil.charting.data.PieDataSet;
41
import com.github.mikephil.charting.data.PieEntry;
42
 
43
import java.util.ArrayList;
44
import java.util.List;
45
 
46
 
47
public class ProgressFragment extends Fragment  {
48
    private static final String TAG = "C2GS - ProgressFragment";
49
    private ITwoGetSkills iTwoGetSkills;
50
    private PieChart mChartProgress;
51
    private PieChart mChartReturning;
52
    private TextView mTextViewTotal;
53
    private TextView mTextViewStarted;
54
    private TextView mTextViewForStart;
55
    private TextView mTextViewCompleted;
56
    private ProgressViewModel mProgressViewModel;
57
 
58
 
59
 
60
    @Override
61
    public void onCreate(@Nullable Bundle savedInstanceState) {
62
        super.onCreate(savedInstanceState);
63
        setHasOptionsMenu(true);
64
 
65
 
66
    }
67
 
68
    @Override
69
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
70
        super.onCreateOptionsMenu(menu, inflater);
71
        menu.clear();
72
    }
73
 
74
 
75
    @Override
76
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
77
                             Bundle savedInstanceState) {
78
        // Inflate the layout for this fragment
79
 
80
 
81
        return inflater.inflate(R.layout.fragment_progress, container, false);
82
    }
83
 
84
 
85
    @Override
86
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
87
        super.onViewCreated(view, savedInstanceState);
88
 
89
        mProgressViewModel = new ViewModelProvider(requireActivity()).get(ProgressViewModel.class);
90
 
91
        iTwoGetSkills = (ITwoGetSkills) requireActivity();
92
        iTwoGetSkills.setTitleActionBar(requireActivity().getString(R.string.menu_progress));
93
 
94
 
95
        mChartProgress = (PieChart) requireActivity().findViewById(R.id.fragment_progress_chart_progress);
96
        mChartProgress.animateY(1000);
97
        mChartProgress.setEntryLabelColor(Color.BLACK);
98
        mChartProgress.getLegend().setEnabled(false);
99
        mChartProgress.getDescription().setEnabled(false);
100
        mChartProgress.setDrawEntryLabels(false);
101
        mChartProgress.setUsePercentValues(true);
102
        mChartProgress.setEntryLabelTextSize(16f);
103
        mChartProgress.setEntryLabelTypeface(Typeface.SANS_SERIF);
104
        mChartProgress.setTransparentCircleRadius(30f);
105
 
106
        mChartProgress.setCenterTextColor(R.color.appBar);
107
        mChartProgress.setCenterTextSize(20);
108
 
109
 
110
        mChartReturning = (PieChart) requireActivity().findViewById(R.id.fragment_progress_chart_returning);
111
        mChartReturning.animateY(1000);
112
        mChartReturning.setEntryLabelColor(Color.BLACK);
113
        mChartReturning.getLegend().setEnabled(false);
114
        mChartReturning.getDescription().setEnabled(false);
115
        mChartReturning.setDrawEntryLabels(false);
116
        mChartReturning.setUsePercentValues(true);
117
        mChartReturning.setEntryLabelTextSize(16f);
118
        mChartReturning.setEntryLabelTypeface(Typeface.SANS_SERIF);
119
        mChartReturning.setTransparentCircleRadius(30f);
120
        mChartReturning.setCenterTextColor(R.color.appBar);
121
        mChartReturning.setCenterTextSize(20);
122
 
123
 
124
 
125
        mTextViewTotal = (TextView) requireActivity().findViewById(R.id.fragment_progress_textview_status_total);
126
        mTextViewStarted = (TextView) requireActivity().findViewById(R.id.fragment_progress_textview_status_total_started);
127
        mTextViewForStart = (TextView) requireActivity().findViewById(R.id.fragment_progress_textview_status_total_for_start);
128
        mTextViewCompleted = (TextView) requireActivity().findViewById(R.id.fragment_progress_textview_status_total_completed);
129
 
130
        Log.d(TAG, "Declarando el Observador");
131
        Observer<ProgressViewData> progressUpdateObserver = new Observer<ProgressViewData>() {
132
            @Override
133
            public void onChanged(ProgressViewData progressViewData) {
134
                Log.d(TAG, "Observador - Actualizar el progreso");
135
                mChartProgress.invalidate();
136
                mChartReturning.invalidate();
137
 
138
                mTextViewTotal.setText(String.valueOf(progressViewData.getCapsuleTotal()));
139
                mTextViewStarted.setText(String.valueOf(progressViewData.getCapsuleStarted()));
140
                mTextViewForStart.setText(String.valueOf(progressViewData.getCapsuleForStart()));
141
                mTextViewCompleted.setText(String.valueOf(progressViewData.getCapsuleCompleted()));
142
 
143
 
144
                ArrayList<Integer> colors = new ArrayList<>();
145
                colors.add(Color.rgb(70,155,200));
146
                colors.add(Color.rgb(130,208,  212));
147
 
148
                ArrayList<PieEntry> dataEntryProgress = new ArrayList<PieEntry>();
149
                dataEntryProgress.add(new PieEntry(progressViewData.getPercentCompleted(), requireActivity().getString(R.string.chart_completed)));
150
                dataEntryProgress.add(new PieEntry(progressViewData.getPercentIncompleted(), requireActivity().getString(R.string.chart_incompleted)));
151
 
152
                PieDataSet pieDataSetProgress = new PieDataSet(dataEntryProgress,  "Progress");
153
                pieDataSetProgress.setColors(colors);
154
                pieDataSetProgress.setDrawValues(false);
155
                pieDataSetProgress.setSliceSpace(5f);
156
 
157
                PieData pieDataProgress = new PieData(pieDataSetProgress);
158
                pieDataProgress.setValueTextSize(18f);
159
                pieDataProgress.setValueTextColor(R.color.colorTextColor);
160
                pieDataProgress.setValueTypeface(Typeface.DEFAULT_BOLD);
161
 
162
                mChartProgress.setData(pieDataProgress);
163
                Log.d(TAG, "ChartProgress - CenterText : " + progressViewData.getPercentCompleted() + " %");
164
 
165
                mChartProgress.setCenterText(progressViewData.getPercentCompleted() + " %");
166
                mChartProgress.notifyDataSetChanged();
167
 
168
 
169
                ArrayList<PieEntry> dataEntryReturning = new ArrayList<PieEntry>();
170
                dataEntryReturning.add(new PieEntry( progressViewData.getCapsuleWithReturning(), requireActivity().getString(R.string.chart_returning)));
171
                dataEntryReturning.add(new PieEntry( progressViewData.getCapsuleWithoutReturning(), requireActivity().getString(R.string.chart_new)));
172
 
173
 
174
                PieDataSet pieDataSetReturning = new PieDataSet(dataEntryReturning,  "Returning");
175
                pieDataSetReturning.setColors(colors);
176
                pieDataSetReturning.setDrawValues(false);
177
                pieDataSetReturning.setSliceSpace(5f);
178
 
179
                PieData pieDataReturning = new PieData(pieDataSetReturning);
180
                pieDataReturning.setValueTextSize(18f);
181
                pieDataReturning.setValueTextColor(R.color.colorTextColor);
182
                pieDataReturning.setValueTypeface(Typeface.DEFAULT_BOLD);
183
 
184
                Log.d(TAG, "CharReturning - CenterText : " + progressViewData.getCapsuleWithReturning());
185
 
186
                mChartReturning.setData(pieDataReturning);
187
                mChartReturning.setCenterText(String.valueOf(progressViewData.getCapsuleWithReturning()));
188
                mChartReturning.notifyDataSetChanged();
189
 
190
            }
191
        };
192
 
193
 
194
 
195
 
196
        mProgressViewModel.getProgressMutableLiveData().observe(requireActivity(),progressUpdateObserver);
197
        loadData();
198
    }
199
 
200
    @Override
201
    public void onResume() {
202
        super.onResume();
203
 
204
        Log.d(TAG, "onResume");
205
        loadData();
206
    }
207
 
208
 
209
 
210
 
211
    @Override
212
    public void onHiddenChanged(boolean hidden) {
213
        super.onHiddenChanged(hidden);
214
 
215
        Log.d(TAG, "onHiddenChanged : " + (hidden ? "true" : "false"));
216
 
217
        if(!hidden) {
218
            loadData();
219
        }
220
    }
221
 
222
    private void loadData()
223
    {
44 efrain 224
        int fragmentIdxActive = iTwoGetSkills.getPreference().getFragmentIdxActive();
225
        if (fragmentIdxActive != Constants.IDX_FRAGMENT_PROGRESS) {
226
            return;
227
        }
1 efrain 228
 
44 efrain 229
        Log.d("BUG 2PLANO", "TopicFragment - loadData");
230
 
231
 
232
        String userUuid = iTwoGetSkills.getPreference().getUserUuid();
1 efrain 233
        String companyUuid = iTwoGetSkills.getCompanyUuidActive();
234
        if(TextUtils.isEmpty(companyUuid)) {
235
            List<Company> companies = iTwoGetSkills.getDatabase().getCompanyDao().selectAll();
236
            if(companies.size() > 0) {
237
                companyUuid = companies.get(0).getUuid();
238
                iTwoGetSkills.getPreference().setCompanyUuidActive(companyUuid);
29 efrain 239
                iTwoGetSkills.getPreference().save();
1 efrain 240
            }
241
        }
242
 
243
 
244
 
245
        TopicDao topicDao = iTwoGetSkills.getDatabase().getTopicDao();
246
        List<Topic> topics = topicDao.selectAllByCompanyUuid(companyUuid);
247
 
248
 
249
        mProgressViewModel.getProgress().setCapsuleTotal(0);
250
        mProgressViewModel.getProgress().setCapsuleStarted(0);
251
        mProgressViewModel.getProgress().setCapsuleForStart(0);
252
        mProgressViewModel.getProgress().setCapsuleCompleted (0);
253
        mProgressViewModel.getProgress().setCapsuleWithReturning( 0);
254
        mProgressViewModel.getProgress().setCapsuleWithoutReturning( 0);
255
 
256
        ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();
257
        CapsuleDao capsuleDao = iTwoGetSkills.getDatabase().getCapsuleDao();
258
        List<Capsule> capsules = new ArrayList<>();
259
        Progress progress;
260
        ResultCount resultCount;
261
        ResultTotalInt resultTotalInt;
262
        int capsuleTotal = 0;
263
        int capsuleTotalCompleted = 0;
264
        int capsuleTotalWithReturning = 0;
265
        int capsuleTotalWithoutReturning = 0;
266
        int totalCapsuleStarted = 0;
267
 
268
        for(Topic topic : topics )
269
        {
270
            resultCount = capsuleDao.getCountByTopicUuid(topic.getUuid());
271
            capsuleTotal =  capsuleTotal + resultCount.getCount();
272
 
273
 
44 efrain 274
            resultCount = progressDao.getCountCapsulesCompletedByTopicUuidAndUserUuid(topic.getUuid(), userUuid);
1 efrain 275
            capsuleTotalCompleted = capsuleTotalCompleted + resultCount.getCount();
276
 
277
 
278
 
44 efrain 279
            resultTotalInt = progressDao.getCountCapsulesCompletedWithReturningByTopicUuidAndUserUuid(topic.getUuid(), userUuid);
1 efrain 280
            capsuleTotalWithReturning = capsuleTotalWithReturning + resultTotalInt.getTotal();
281
 
282
 
283
 
44 efrain 284
            resultCount = progressDao.getCountCapsulesCompletedWithoutReturningByTopicUuidAndUserUuid(topic.getUuid(), userUuid);
1 efrain 285
            capsuleTotalWithoutReturning = capsuleTotalWithoutReturning + resultCount.getCount();mProgressViewModel.getProgress().setCapsuleWithoutReturning(mProgressViewModel.getProgress().getCapsuleWithoutReturning() +resultCount.getCount());
286
 
287
            //totalSlides += slideDao.getCountByTopicUuid(topic.uuid);
288
            //totalSlidesCompleted  += progressDao.getCountSlidesCompletedByTopicUuid(topic.uuid);
289
 
290
            capsules = capsuleDao.selectAllByTopicUuid(topic.getUuid());
291
            for(Capsule capsule : capsules) {
44 efrain 292
                progress = progressDao.selectByCapsuleUuidAndUserUuid(capsule.getUuid(), userUuid);
1 efrain 293
                if (progress != null) {
294
                    if (progress.getCompleted() == 0) {
295
                        totalCapsuleStarted++;
296
 
297
 
298
                    }
299
 
300
                }
301
            }
302
 
303
 
304
        }
305
 
306
        Log.d(TAG, "capsuleTotal : " + capsuleTotal);
307
        Log.d(TAG, "capsuleTotalCompleted : " + capsuleTotalCompleted);
308
        Log.d(TAG, "capsuleTotalWithReturning : " + capsuleTotalWithReturning);
309
        Log.d(TAG, "capsuleTotalWithoutReturning : " + capsuleTotalWithoutReturning);
310
        Log.d(TAG, "totalCapsuleStarted : " + totalCapsuleStarted);
311
 
312
 
313
        mProgressViewModel.getProgress().setCapsuleTotal(capsuleTotal);
314
        mProgressViewModel.getProgress().setCapsuleCompleted(capsuleTotalCompleted);
315
        mProgressViewModel.getProgress().setCapsuleWithReturning(capsuleTotalWithReturning);
316
        mProgressViewModel.getProgress().setCapsuleWithoutReturning(capsuleTotalWithoutReturning);
317
        mProgressViewModel.getProgress().setCapsuleStarted(totalCapsuleStarted);
318
 
319
        mProgressViewModel.getProgress().setCapsuleForStart(mProgressViewModel.getProgress().getCapsuleTotal() - mProgressViewModel.getProgress().getCapsuleCompleted());
320
 
321
 
322
        mProgressViewModel.getProgress().setPercentCompleted(0);
323
        mProgressViewModel.getProgress().setPercentIncompleted(100);
324
 
325
 
326
        if( mProgressViewModel.getProgress().getCapsuleTotal() > 0) {
327
            mProgressViewModel.getProgress().setPercentCompleted((mProgressViewModel.getProgress().getCapsuleCompleted() * 100) /  mProgressViewModel.getProgress().getCapsuleTotal());
328
            mProgressViewModel.getProgress().setPercentIncompleted(100 - mProgressViewModel.getProgress().getPercentCompleted());
329
        }
330
 
331
        mProgressViewModel.getProgressMutableLiveData().setValue(mProgressViewModel.getProgress());
332
    }
333
}