Blame view

app/src/main/java/com/dinhcv/lifelogpedometer/activity/TopDateFragment.java 12.8 KB
7f095a929   chudinhbka@gmail.com   Create GIT Project
1
2
3
4
5
  package com.dinhcv.lifelogpedometer.activity;
  
  import android.app.DatePickerDialog;
  import android.content.Context;
  import android.content.Intent;
7f095a929   chudinhbka@gmail.com   Create GIT Project
6
7
  import android.os.Bundle;
  import android.support.annotation.Nullable;
7f095a929   chudinhbka@gmail.com   Create GIT Project
8
9
10
11
12
13
14
15
16
17
18
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.DatePicker;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  import com.dinhcv.lifelogpedometer.R;
  import com.dinhcv.lifelogpedometer.adapter.NoticeAdapter;
  import com.dinhcv.lifelogpedometer.customview.ExpandableListCustomView;
7f095a929   chudinhbka@gmail.com   Create GIT Project
19
  import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener;
7f095a929   chudinhbka@gmail.com   Create GIT Project
20
21
  import com.dinhcv.lifelogpedometer.model.structure.home.NoticeInfo;
  import com.dinhcv.lifelogpedometer.model.structure.home.TagetInfo;
5520a4b27   Dinh Chu   update
22
  import com.dinhcv.lifelogpedometer.portal.ApiServices;
7f095a929   chudinhbka@gmail.com   Create GIT Project
23
24
25
26
27
28
29
30
31
32
33
34
35
  import com.dinhcv.lifelogpedometer.portal.LLAPIManager;
  import com.dinhcv.lifelogpedometer.utils.Const;
  import com.dinhcv.lifelogpedometer.utils.Debug;
  import com.dinhcv.lifelogpedometer.utils.Utils;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.util.ArrayList;
  import java.util.Calendar;
  import java.util.Date;
  import java.util.List;
0c1f9bf91   Dinh Chu   update top handle
36
  public class TopDateFragment extends FragmentBase implements SettingFragmentPresenter {
7f095a929   chudinhbka@gmail.com   Create GIT Project
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  
      private TextView tvDate;
      private TextView tvStep;
      private TextView tvRemain;
      private TextView tvRateDone;
      private ImageView ivBack;
      private ImageView ivNext;
  
      private LinearLayout llBike;
      private LinearLayout llWalking;
      private LinearLayout llRunning;
  
      private Date mAnaDate;
      private Calendar mCalendar;
  
      private int mAnaDay;
      private int mAnaMonth;
      private int mAnaYear;
      private Context mContext;
  
      private Const.STEP_TYPE stepType;
      private TagetInfo mTagetInfo = new TagetInfo();
0c1f9bf91   Dinh Chu   update top handle
59
      private ImageView ivToDay;
7f095a929   chudinhbka@gmail.com   Create GIT Project
60
61
      private ExpandableListCustomView lvNotice;
      private NoticeAdapter mNoticeAdapter;
0c1f9bf91   Dinh Chu   update top handle
62
63
64
65
66
67
      private View mRootview;
      private TopFragment mTopFragment;
  
      public void setRootFragment(TopFragment frag) {
          this.mTopFragment = frag;
      }
7f095a929   chudinhbka@gmail.com   Create GIT Project
68
69
70
71
72
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
              savedInstanceState) {
          // Inflate the layout for this fragment
0c1f9bf91   Dinh Chu   update top handle
73
          mRootview = inflater.inflate(R.layout.fragment_top_date, container, false);
7f095a929   chudinhbka@gmail.com   Create GIT Project
74
          mContext = getActivity();
0c1f9bf91   Dinh Chu   update top handle
75
          initView(mRootview);
7f095a929   chudinhbka@gmail.com   Create GIT Project
76
          initData();
0c1f9bf91   Dinh Chu   update top handle
77
          return mRootview;
7f095a929   chudinhbka@gmail.com   Create GIT Project
78
79
80
81
82
83
84
85
86
87
88
89
90
91
      }
  
  
      private void initView(View rootView) {
          tvDate = (TextView) rootView.findViewById(R.id.tv_date);
          tvStep = (TextView) rootView.findViewById(R.id.tv_step);
          tvRemain = (TextView) rootView.findViewById(R.id.tv_remainingStep);
          tvRateDone = (TextView) rootView.findViewById(R.id.tv_completeRate);
          ivBack = (ImageView) rootView.findViewById(R.id.iv_back);
          ivNext = (ImageView) rootView.findViewById(R.id.iv_next);
  
          llBike = (LinearLayout) rootView.findViewById(R.id.ll_bike);
          llWalking = (LinearLayout) rootView.findViewById(R.id.ll_walking);
          llRunning = (LinearLayout) rootView.findViewById(R.id.ll_running);
0c1f9bf91   Dinh Chu   update top handle
92
          ivToDay = (ImageView) rootView.findViewById(R.id.iv_toDay);
7f095a929   chudinhbka@gmail.com   Create GIT Project
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
          lvNotice = (ExpandableListCustomView) rootView.findViewById(R.id.lv_notice);
  
          handleEvent();
      }
  
      private void handleEvent(){
          tvDate.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  handleAnaDatePicker();
              }
          });
  
          ivBack.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  mCalendar = Calendar.getInstance();
                  mCalendar.setTime(mAnaDate);
                  mCalendar.add(Calendar.DAY_OF_MONTH, -1);
                  Date date = mCalendar.getTime();
                  tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date));
                  mAnaDate = date;
                  getHomePage(mAnaDate, stepType);
5520a4b27   Dinh Chu   update
116
                  getNews(mAnaDate);
7f095a929   chudinhbka@gmail.com   Create GIT Project
117
118
119
120
121
122
123
124
125
126
127
128
129
              }
          });
  
          ivNext.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  mCalendar = Calendar.getInstance();
                  mCalendar.setTime(mAnaDate);
                  mCalendar.add(Calendar.DAY_OF_MONTH, +1);
                  Date date = mCalendar.getTime();
                  tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date));
                  mAnaDate = date;
                  getHomePage(mAnaDate, stepType);
5520a4b27   Dinh Chu   update
130
                  getNews(mAnaDate);
7f095a929   chudinhbka@gmail.com   Create GIT Project
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
              }
          });
  
  
          llWalking.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  stepType = Const.STEP_TYPE.WALKING;
                  updateUiStepType(false, true, false);
                  // add data
                  getHomePage(mAnaDate, stepType);
              }
          });
  
          llRunning.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  stepType = Const.STEP_TYPE.RUNNING;
                  updateUiStepType(false, false, true);
                  // add
                  getHomePage(mAnaDate, stepType);
              }
          });
  
          llBike.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  stepType = Const.STEP_TYPE.BIKE;
                  updateUiStepType(true, false, false);
                  // add data
                  getHomePage(mAnaDate, stepType);
              }
          });
0c1f9bf91   Dinh Chu   update top handle
164
165
166
167
168
169
          ivToDay.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  mTopFragment.showDetailFragment();
              }
          });
7f095a929   chudinhbka@gmail.com   Create GIT Project
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
      }
  
      private void updateUiStepType(boolean b1, boolean b2, boolean b3) {
          llBike.setSelected(b1);
          llWalking.setSelected(b2);
          llRunning.setSelected(b3);
      }
  
      /**
       * Show date picker dialog
       */
      private void handleAnaDatePicker() {
  
          new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
              @Override
              public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  
                  mCalendar = Calendar.getInstance();
                  mCalendar.set(year, monthOfYear, dayOfMonth);
                  mAnaYear = year;
                  mAnaMonth = monthOfYear;
                  mAnaDay = dayOfMonth;
                  Date date = mCalendar.getTime();
                  tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(date));
                  mAnaDate = date;
  
                  getHomePage(mAnaDate, stepType);
5520a4b27   Dinh Chu   update
197
                  getNews(mAnaDate);
7f095a929   chudinhbka@gmail.com   Create GIT Project
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
              }
          }, mAnaYear, mAnaMonth, mAnaDay).show();
  
      }
  
  
      /**
       * init data for
       */
      private void initData() {
          mCalendar = Calendar.getInstance();
          mCalendar.setTime(new Date());
          mAnaDate = mCalendar.getTime();
          mAnaYear = mCalendar.get(Calendar.YEAR);
          mAnaMonth = mCalendar.get(Calendar.MONTH);
          mAnaDay = mCalendar.get(Calendar.DAY_OF_MONTH);
  
          tvDate.setText(Utils.dateToStringFormatDayMonthYearJp(mAnaDate));
  
          stepType = Const.STEP_TYPE.WALKING;
          updateUiStepType(false, true, false);
          getHomePage(mAnaDate, stepType);
5520a4b27   Dinh Chu   update
220
          getNews(mAnaDate);
7f095a929   chudinhbka@gmail.com   Create GIT Project
221
222
223
224
225
226
      }
  
  
      private void getHomePage(Date date, Const.STEP_TYPE stepType){
  
          showDialog(mContext);
5520a4b27   Dinh Chu   update
227
          ApiServices.homePage(date, stepType, new LLAPIManagerListener() {
7f095a929   chudinhbka@gmail.com   Create GIT Project
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
              @Override
              public void onError(Error error) {
                  Debug.error("Get data history error");
                  hiddenDialog();
                  showDialogNotData();
              }
  
              @Override
              public void onSuccess(String json) {
                  Debug.error("Get data history success");
                  hiddenDialog();
                  loadDataDone(json);
              }
  
              @Override
              public void onSuccess(JSONObject object) {
                  Debug.error("Get data history success");
                  hiddenDialog();
              }
          });
      }
  
      private void showDialogNotData(){
          showAlerDialog(mContext, getResources().getString(R.string.can_not_get_data));
      }
  
      private void loadDataDone(String jsonString) {
          JSONObject jsonObject = null;
          try {
              jsonObject = new JSONObject(jsonString);
              int status = jsonObject.optInt("status");
              if (status == 1) {
                  JSONObject jsonObject1 = jsonObject.optJSONObject("result");
                  JSONObject targetInf = jsonObject1.getJSONObject("targetInf");
                  JSONArray listNotice = jsonObject1.getJSONArray("listNotice");
  
                  if (targetInf != null){
                      String target = targetInf.optString("target_step");
                      Debug.normal("Target: "+ target);
                      mTagetInfo.setTaget(targetInf.optString("target_step"));
                      mTagetInfo.setSteps(targetInf.optString("num_step"));
                      mTagetInfo.setStepRemain(targetInf.optString("remaining_step"));
                      mTagetInfo.setCompletePercent(targetInf.optString("complete_percent"));
                  }
  
                  if (listNotice != null && listNotice.length() > 0) {
                      List<NoticeInfo> infoLists = new ArrayList<>();
                      for (int i = 0; i < listNotice.length(); i++){
                          NoticeInfo noticeInfo = new NoticeInfo();
                          JSONObject ob = (JSONObject) listNotice.get(i);
                          noticeInfo.setId(ob.optInt("id"));
                          noticeInfo.setContent(ob.optString("notice_content"));
                          infoLists.add(noticeInfo);
                      }
  
                      mTagetInfo.setNoticeList(infoLists);
                  }
  
              }
          } catch (JSONException e) {
              e.printStackTrace();
              mTagetInfo = new TagetInfo();;
          }
54e5fcd56   chudinhbka@gmail.com   handle top api
291
          loadUI();
7f095a929   chudinhbka@gmail.com   Create GIT Project
292
      }
54e5fcd56   chudinhbka@gmail.com   handle top api
293
294
295
      private void loadUI(){
          tvStep.setText(String.valueOf(mTagetInfo.getSteps()));
          tvRemain.setText(String.valueOf(mTagetInfo.getStepRemain()));
383e85498   chudinhbka@gmail.com   update handle UI
296
          tvRateDone.setText(mContext.getResources().getString(R.string.percent_unit, mTagetInfo.getCompletePercent()));
5520a4b27   Dinh Chu   update
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
      }
  
      private void getNews(Date date){
  
          ApiServices.getNews(date, new LLAPIManagerListener() {
              @Override
              public void onError(Error error) {
                  Debug.error("Get data news error");
              }
  
              @Override
              public void onSuccess(String json) {
                  Debug.error("Get data news success");
                  loadNewsDone(json);
              }
  
              @Override
              public void onSuccess(JSONObject object) {
                  Debug.error("Get data news success");
              }
          });
      }
  
      private void loadNewsDone(String jsonString) {
  
          List<NoticeInfo> contentList = new ArrayList<>();
          JSONObject jsonObject = null;
          try {
              jsonObject = new JSONObject(jsonString);
              int status = jsonObject.optInt("status");
              if (status == 1) {
                  JSONObject jsonResult = jsonObject.optJSONObject("result");
                  JSONArray listNotice = jsonResult.getJSONArray("listNotice");
  
                  if (listNotice != null) {
7f095a929   chudinhbka@gmail.com   Create GIT Project
332

5520a4b27   Dinh Chu   update
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
                      for (int i = 0; i < listNotice.length(); i++) {
                          JSONObject obContent = (JSONObject) listNotice.get(i);
                          String content = obContent.optString("notice_content");
                          Debug.normal("Content: " + content);
                          contentList.add(new NoticeInfo(i+1,content));
                      }
                  }
  
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          addNews(contentList);
      }
  
      private void addNews(List<NoticeInfo> noticeInfos){
          if (noticeInfos != null && noticeInfos.size() >0){
              mNoticeAdapter = new NoticeAdapter(mContext, noticeInfos);
7f095a929   chudinhbka@gmail.com   Create GIT Project
352
353
              lvNotice.setAdapter(mNoticeAdapter);
              lvNotice.setExpanded(true);
5520a4b27   Dinh Chu   update
354
355
          }else {
              lvNotice.setAdapter(null);
7f095a929   chudinhbka@gmail.com   Create GIT Project
356
          }
7f095a929   chudinhbka@gmail.com   Create GIT Project
357
      }
7f095a929   chudinhbka@gmail.com   Create GIT Project
358

5520a4b27   Dinh Chu   update
359

7f095a929   chudinhbka@gmail.com   Create GIT Project
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
      @Override
      public void onAttach(Context context) {
          super.onAttach(context);
  
      }
  
  
      @Override
      public void onSaveData() {
  
      }
  
      @Override
      public void onInvalidate(boolean isInit) {
          initData();
      }
  
      @Override
      public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
          super.onViewStateRestored(savedInstanceState);
  
          initData();
      }
  
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
  
      }
54e5fcd56   chudinhbka@gmail.com   handle top api
390

5520a4b27   Dinh Chu   update
391

7f095a929   chudinhbka@gmail.com   Create GIT Project
392
  }