Proyectos de Subversion Android Microlearning

Rev

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