Blame view

app/src/main/java/com/dinhcv/lifelogpedometer/activity/DateFragment.java 11 KB
7f095a929   chudinhbka@gmail.com   Create GIT Project
1
2
3
4
5
6
  package com.dinhcv.lifelogpedometer.activity;
  
  import android.app.DatePickerDialog;
  import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
54e5fcd56   chudinhbka@gmail.com   handle top api
7
8
9
10
  import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  import android.hardware.SensorManager;
7f095a929   chudinhbka@gmail.com   Create GIT Project
11
12
13
14
15
16
17
18
19
20
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  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;
54e5fcd56   chudinhbka@gmail.com   handle top api
21
  import android.widget.Toast;
7f095a929   chudinhbka@gmail.com   Create GIT Project
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
164
165
166
167
168
169
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
  
  import com.dinhcv.lifelogpedometer.R;
  import com.dinhcv.lifelogpedometer.adapter.NoticeAdapter;
  import com.dinhcv.lifelogpedometer.customview.ExpandableListCustomView;
  import com.dinhcv.lifelogpedometer.feature.Database;
  import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener;
  import com.dinhcv.lifelogpedometer.model.structure.history.HistoryInfo;
  import com.dinhcv.lifelogpedometer.model.structure.home.NoticeInfo;
  import com.dinhcv.lifelogpedometer.model.structure.home.TagetInfo;
  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;
  
  import static com.dinhcv.lifelogpedometer.utils.Utils.getStandarDate;
  
  public class DateFragment extends FragmentBase implements SettingFragmentPresenter {
  
      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();
  
      private ExpandableListCustomView lvNotice;
      private NoticeAdapter mNoticeAdapter;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
              savedInstanceState) {
          // Inflate the layout for this fragment
          View rootView = inflater.inflate(R.layout.fragment_date, container, false);
          mContext = getActivity();
          initView(rootView);
          initData();
          return rootView;
      }
  
  
      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);
  
          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);
              }
          });
  
          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);
              }
          });
  
  
          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);
              }
          });
  
      }
  
      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);
              }
          }, 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);
      }
  
  
      private void getHomePage(Date date, Const.STEP_TYPE stepType){
  
          showDialog(mContext);
          LLAPIManager.homePage(date, stepType, new LLAPIManagerListener() {
              @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
287
          loadUI();
7f095a929   chudinhbka@gmail.com   Create GIT Project
288
      }
54e5fcd56   chudinhbka@gmail.com   handle top api
289
290
291
      private void loadUI(){
          tvStep.setText(String.valueOf(mTagetInfo.getSteps()));
          tvRemain.setText(String.valueOf(mTagetInfo.getStepRemain()));
7f095a929   chudinhbka@gmail.com   Create GIT Project
292
293
294
295
296
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
332
333
334
335
336
337
          tvRateDone.setText(getResources().getString(R.string.percent_unit, mTagetInfo.getCompletePercent()));
  
          List<NoticeInfo> infoLists = mTagetInfo.getNoticeList();
          if (infoLists != null && infoLists.size() >0){
              mNoticeAdapter = new NoticeAdapter(mContext, infoLists);
              lvNotice.setAdapter(mNoticeAdapter);
              lvNotice.setExpanded(true);
          }
  
      }
  
      private void addDataToObject(){
  
      }
  
  
      @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
338

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