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