Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

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

Rev Autor Línea Nro. Línea
1 gabriel 1
package com.cesams.twogetskills.fragment;
2
 
3
import android.os.Bundle;
52 gabriel 4
import android.os.Handler;
5
import android.os.Looper;
1 gabriel 6
import android.util.Log;
7
import android.view.LayoutInflater;
8
import android.view.Menu;
9
import android.view.MenuInflater;
10
import android.view.View;
11
import android.view.ViewGroup;
36 gabriel 12
import android.widget.Button;
49 gabriel 13
import android.widget.Toast;
1 gabriel 14
 
15
import androidx.annotation.NonNull;
16
import androidx.annotation.Nullable;
17
import androidx.fragment.app.Fragment;
18
import androidx.lifecycle.LifecycleOwner;
19
import androidx.recyclerview.widget.RecyclerView;
20
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
21
 
22
import com.cesams.twogetskills.Configuration;
23
import com.cesams.twogetskills.R;
24
import com.cesams.twogetskills.adapter.TopicListViewAdapter;
25
import com.cesams.twogetskills.dao.AnswerDao;
26
import com.cesams.twogetskills.dao.CapsuleDao;
27
import com.cesams.twogetskills.dao.CompanyDao;
28
import com.cesams.twogetskills.dao.ProgressDao;
29
import com.cesams.twogetskills.dao.QuestionDao;
30
import com.cesams.twogetskills.dao.QuizDao;
31
import com.cesams.twogetskills.dao.SlideDao;
32
import com.cesams.twogetskills.dao.TopicDao;
33
import com.cesams.twogetskills.library.Http;
34
import com.cesams.twogetskills.library.MD5;
35
import com.cesams.twogetskills.entity.Progress;
36
import com.cesams.twogetskills.entity.Topic;
19 gabriel 37
import com.cesams.twogetskills.preference.Preference;
53 gabriel 38
import com.cesams.twogetskills.skeleton.IReloadData;
1 gabriel 39
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
40
import com.cesams.twogetskills.viewmodel.TopicViewModel;
52 gabriel 41
import com.google.firebase.messaging.FirebaseMessaging;
1 gabriel 42
 
43
import org.json.JSONException;
44
import org.json.JSONObject;
45
 
46
import java.io.IOException;
49 gabriel 47
import java.sql.Ref;
1 gabriel 48
import java.util.ArrayList;
49
import java.util.Calendar;
50
import java.util.List;
51
import java.util.Random;
52
import java.util.TimeZone;
53
 
54
import okhttp3.Call;
55
import okhttp3.Callback;
56
import okhttp3.OkHttpClient;
57
import okhttp3.Request;
58
import okhttp3.Response;
59
 
60
import androidx.lifecycle.Observer;
61
import androidx.lifecycle.ViewModelProvider;
62
 
63
 
53 gabriel 64
public class TopicFragment extends Fragment implements TopicListViewAdapter.ItemClickListener, LifecycleOwner, IReloadData {
1 gabriel 65
    public final static String TAG = "C2GS - TopicFragment";
66
    private RecyclerView listView;
67
    private TopicListViewAdapter adapter;
68
    private ITwoGetSkills iTwoGetSkills;
69
    private SwipeRefreshLayout swipeRefreshLayout;
70
    private TopicViewModel mTopicViewModel;
71
 
53 gabriel 72
 
1 gabriel 73
    @Override
74
    public void onCreate(@Nullable Bundle savedInstanceState) {
75
        super.onCreate(savedInstanceState);
76
        setHasOptionsMenu(true);
77
 
78
 
79
    }
80
 
81
    @Override
82
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
83
        super.onCreateOptionsMenu(menu, inflater);
84
        menu.clear();
85
    }
86
 
87
    @Override
88
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
89
 
90
        return inflater.inflate(R.layout.fragment_topic, container, false);
91
    }
92
 
93
    @Override
94
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
95
        super.onViewCreated(view, savedInstanceState);
96
 
97
        Log.d(TAG, "onViewCreated");
98
 
99
        iTwoGetSkills = (ITwoGetSkills) getActivity();
27 gabriel 100
        iTwoGetSkills.showNavigationAndToolbar();
1 gabriel 101
 
102
        mTopicViewModel = new ViewModelProvider(requireActivity()).get(TopicViewModel.class);
103
 
104
        adapter = new TopicListViewAdapter(requireActivity(), mTopicViewModel.getTopicArrayList());
105
        adapter.setClickListener(this);
106
 
107
        Observer<ArrayList<Topic>> topicListUpdateObserver = new Observer<ArrayList<Topic>>() {
108
            @Override
109
            public void onChanged(ArrayList<Topic> topicList) {
110
                adapter.notifyDataSetChanged();
111
            }
112
        };
113
 
114
        mTopicViewModel.getTopicMutableLiveData().observe(requireActivity(), topicListUpdateObserver);
115
        iTwoGetSkills.setTitleActionBar(getActivity().getString(R.string.menu_topics));
116
 
117
        swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.fragment_topic_swipe_layout);
118
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
119
            @Override
120
            public void onRefresh() {
121
 
71 efrain 122
                refrescarContent();
1 gabriel 123
 
39 gabriel 124
            }
125
        });
1 gabriel 126
 
127
 
39 gabriel 128
        listView = getView().findViewById(R.id.fragment_topic_listview);
129
        listView.setAdapter(adapter);
130
        //listView.setLayoutManager(gridLayoutManager);
131
        listView.setHasFixedSize(true);
1 gabriel 132
 
133
 
39 gabriel 134
    }
1 gabriel 135
 
71 efrain 136
    private void refrescarContent(){
39 gabriel 137
        if (iTwoGetSkills.isConnectedInternet()) {
71 efrain 138
            Toast.makeText(getContext(), "Iniciando actualización...", Toast.LENGTH_SHORT).show();
1 gabriel 139
 
39 gabriel 140
            try {
141
                TimeZone timeZone = TimeZone.getTimeZone("UTC");
142
                Calendar calendar = Calendar.getInstance(timeZone);
143
                TimeZone tz = calendar.getTimeZone();
144
                int created = (int) (calendar.getTimeInMillis() / 1000);
1 gabriel 145
 
39 gabriel 146
                Random random = new Random(created);
147
                int rand = 1000 + random.nextInt(8999);
1 gabriel 148
 
149
 
39 gabriel 150
                Log.d(TAG, "token = " + iTwoGetSkills.getPreference().getDeviceUuid());
151
                Log.d(TAG, "created = " + created);
152
                Log.d(TAG, "rand = " + rand);
153
                Log.d(TAG, "calc = " + iTwoGetSkills.getPreference().getPassword() + ':' + created + ':' + rand);
1 gabriel 154
 
39 gabriel 155
                String secret = MD5.generar(iTwoGetSkills.getPreference().getPassword() + ':' + created + ':' + rand);
1 gabriel 156
 
39 gabriel 157
                Log.d(TAG, "secret = " + secret);
1 gabriel 158
 
39 gabriel 159
                Log.d(TAG, "URL = " + Configuration.URL_REFRESH);
160
                Request request = new Request.Builder()
161
                        .url(Configuration.URL_REFRESH)
162
                        .build();
1 gabriel 163
 
39 gabriel 164
                Http http = new Http(getActivity().getCacheDir(), iTwoGetSkills.getPreference().getDeviceUuid(), secret, created, rand);
165
                OkHttpClient client = http.getHttpClient(false);
1 gabriel 166
 
39 gabriel 167
                Call call = client.newCall(request);
168
                call.enqueue(new Callback() {
169
                    public void onResponse(Call call, Response response)
170
                            throws IOException {
171
                        processResponseServer(response.body().string());
1 gabriel 172
 
39 gabriel 173
                    }
1 gabriel 174
 
39 gabriel 175
                    public void onFailure(Call call, IOException e) {
176
                        Log.d(TAG, "Error :  " + e.getMessage());
177
                    }
178
                });
1 gabriel 179
 
39 gabriel 180
            } catch (Exception e) {
181
                iTwoGetSkills.showMessageSnackBar(e.getMessage());
182
            }
183
 
184
 
185
        } else {
186
            swipeRefreshLayout.setRefreshing(false);
187
        }
1 gabriel 188
    }
189
 
190
    @Override
191
    public void onResume() {
192
        super.onResume();
193
 
194
        loadData();
49 gabriel 195
 
53 gabriel 196
        if(iTwoGetSkills.getPreference().isRefreshContentRequired())
197
        {
198
            Log.e("el contenido","Es requerido refresh");
71 efrain 199
            refrescarContent();
53 gabriel 200
        }
201
 
1 gabriel 202
    }
203
 
204
 
205
    @Override
206
    public void onHiddenChanged(boolean hidden) {
207
        super.onHiddenChanged(hidden);
208
 
209
        Log.d(TAG, "onHiddenChanged : " + (hidden ? "true" : "false"));
210
 
211
        if(!hidden) {
212
            loadData();
52 gabriel 213
 
53 gabriel 214
            if(iTwoGetSkills.getPreference().isRefreshContentRequired())
215
            {
216
                Log.e("el contenido","Es requerido refresh");
71 efrain 217
                refrescarContent();
53 gabriel 218
            }
1 gabriel 219
        }
49 gabriel 220
 
1 gabriel 221
    }
222
 
223
    private void loadData()
224
    {
19 gabriel 225
        String userUuid = iTwoGetSkills.getPreference().getUserUuid();
1 gabriel 226
        iTwoGetSkills.setTitleActionBar(requireActivity().getString(R.string.menu_topics));
227
 
228
        TopicDao topicDao = iTwoGetSkills.getDatabase().getTopicDao();
229
        ArrayList<Topic> dbTopics = (ArrayList<Topic>) topicDao.selectAll();
230
 
231
        mTopicViewModel.getTopicArrayList().clear();
232
        Topic topic;
233
        Progress progress;
234
        ProgressDao progressDao = iTwoGetSkills.getDatabase().getProgressDao();
235
        for (Topic dbTopic : dbTopics)
236
        {
237
            topic = new Topic();
238
            topic.setCompanyUuid(dbTopic.getCompanyUuid());
239
            topic.setUuid(dbTopic.getUuid());
240
            topic.setName(dbTopic.getName());
241
            topic.setDescription(dbTopic.getDescription());
242
            topic.setImage(dbTopic.getImage());
243
            topic.setPosition(dbTopic.getPosition());
244
 
245
 
19 gabriel 246
            progress = progressDao.selectByTopicUuidAndUserUuid(topic.getUuid(), userUuid);
1 gabriel 247
            if(progress != null) {
248
                topic.setCompleted(progress.getCompleted());
249
                topic.setViewSlides( progress.getViewSlides());
250
                topic.setTotalSlides(progress.getTotalSlides());
251
                topic.setProgress(progress.getProgress());
252
            }
253
 
254
            mTopicViewModel.getTopicArrayList().add(topic);
255
        }
256
 
257
        mTopicViewModel.getTopicMutableLiveData().setValue(mTopicViewModel.getTopicArrayList());
258
 
259
    }
260
 
261
    @Override
262
    public void onItemClick(View view, int position) {
263
        Log.d(TAG, "onItemClick");
264
        iTwoGetSkills.changeTopicActive(mTopicViewModel.getTopicArrayList().get(position).getUuid());
265
    }
266
 
267
    private void processResponseServer(String dataString) {
19 gabriel 268
       // Log.e("El device uuid","en topic: "+iTwoGetSkills.getPreference().getDeviceUuid());
1 gabriel 269
 
270
        Log.d(TAG, "processResponseServer = " + dataString);
271
 
272
        getActivity().runOnUiThread(new Runnable() {
273
 
274
            @Override
275
            public void run() {
276
 
277
                try {
278
                    JSONObject objJSON = new JSONObject(dataString);
279
                    boolean success = objJSON.has("success") ? objJSON.getBoolean("success") : false;
280
                    String message = "";
281
                    if (objJSON.has("data")) {
282
                        Object item = objJSON.get("data");
283
                        if (item instanceof String) {
284
                            message = item.toString();
285
                        }
286
                    }
287
 
288
                    if (success) {
43 gabriel 289
 
19 gabriel 290
                        Preference preference = iTwoGetSkills.getPreference();
291
 
1 gabriel 292
                        iTwoGetSkills.getDatabase().getAnswerDao().removeAll();
293
                        iTwoGetSkills.getDatabase().getQuestionDao().removeAll();
294
                        iTwoGetSkills.getDatabase().getQuizDao().removeAll();
295
                        iTwoGetSkills.getDatabase().getSlideDao().removeAll();
296
                        iTwoGetSkills.getDatabase().getCapsuleDao().removeAll();
297
                        iTwoGetSkills.getDatabase().getTopicDao().removeAll();
298
                        iTwoGetSkills.getDatabase().getUserExtendedDao().removeAll();
19 gabriel 299
                        iTwoGetSkills.getDatabase().getUserLogDao().removeAllUserUuidNotEqual(preference.getUserUuid());
300
                        iTwoGetSkills.getDatabase().getProgressDao().removeAllUserUuidNotEqual(preference.getUserUuid());
1 gabriel 301
 
302
                        JSONObject data = objJSON.getJSONObject("data");
303
                        iTwoGetSkills.syncFromServer(data);
304
 
49 gabriel 305
                        preference.setRefreshContentRequired(false);
306
                        Toast.makeText(getContext(), "¡Contenido actualizado con exito!", Toast.LENGTH_SHORT).show();
1 gabriel 307
 
308
                        loadData();
49 gabriel 309
 
1 gabriel 310
                    } else {
311
                        iTwoGetSkills.showMessageSnackBar(message);
312
                    }
313
 
314
 
315
                } catch (JSONException e) {
316
                    Log.d(TAG, e.getMessage());
317
                }
318
 
8 gabriel 319
              //  iTwoGetSkills.reloadNavHeader();
1 gabriel 320
                swipeRefreshLayout.setRefreshing(false);
321
 
322
            }
323
        });
324
 
325
 
326
    }
53 gabriel 327
 
328
    @Override
329
    public void onReloadData() {
71 efrain 330
        refrescarContent();
53 gabriel 331
    }
19 gabriel 332
}