Commit 86ee629d1c1927c72d9bdeeaf23808f0684c3e9c

Authored by chudinhbka@gmail.com
1 parent 54e5fcd562
Exists in master and in 1 other branch development

update top api

Showing 5 changed files with 302 additions and 16 deletions Side-by-side Diff

app/src/main/java/com/dinhcv/lifelogpedometer/activity/TopFragment.java
... ... @@ -25,6 +25,8 @@
25 25 import com.dinhcv.lifelogpedometer.feature.Database;
26 26 import com.dinhcv.lifelogpedometer.interfaces.LLAPIManagerListener;
27 27 import com.dinhcv.lifelogpedometer.model.StepModel;
  28 +import com.dinhcv.lifelogpedometer.model.structure.top.StepHourInfo;
  29 +import com.dinhcv.lifelogpedometer.model.structure.top.TagetTopInfo;
28 30 import com.dinhcv.lifelogpedometer.portal.LLAPIManager;
29 31 import com.dinhcv.lifelogpedometer.utils.Const;
30 32 import com.dinhcv.lifelogpedometer.utils.DayAxisValueFormatter;
... ... @@ -42,6 +44,7 @@
42 44 import org.eazegraph.lib.charts.PieChart;
43 45 import org.eazegraph.lib.models.PieModel;
44 46 import org.json.JSONArray;
  47 +import org.json.JSONException;
45 48 import org.json.JSONObject;
46 49  
47 50 import java.text.NumberFormat;
... ... @@ -93,6 +96,10 @@
93 96 private int stepRemain = 0;
94 97 private int stepCount = 0;
95 98  
  99 + private Date mFromDate;
  100 + private Date mToDate;
  101 +
  102 + private TagetTopInfo mTagetTopInfo = new TagetTopInfo();
96 103 private BarChart mChart;
97 104  
98 105 private List<String> dateList;
... ... @@ -418,7 +425,7 @@
418 425 private void getTopData(Date date, Const.STEP_TYPE stepType){
419 426  
420 427 showDialog(mContext);
421   - LLAPIManager.homePage(date, stepType, new LLAPIManagerListener() {
  428 + LLAPIManager.topInfo(date, stepType, new LLAPIManagerListener() {
422 429 @Override
423 430 public void onError(Error error) {
424 431 Debug.error("Get data history error");
425 432  
426 433  
427 434  
428 435  
... ... @@ -458,29 +465,29 @@
458 465 if (targetInf != null){
459 466 String target = targetInf.optString("target_step");
460 467 Debug.normal("Target: "+ target);
461   - mTagetInfo.setTaget(targetInf.optString("target_step"));
462   - mTagetInfo.setSteps(targetInf.optString("num_step"));
463   - mTagetInfo.setStepRemain(targetInf.optString("remaining_step"));
464   - mTagetInfo.setCompletePercent(targetInf.optString("complete_percent"));
  468 + mTagetTopInfo.setTaget(targetInf.optString("target_step"));
  469 + mTagetTopInfo.setSteps(targetInf.optString("num_step"));
  470 + mTagetTopInfo.setStepRemain(targetInf.optString("remaining_step"));
  471 + mTagetTopInfo.setCompletePercent(targetInf.optString("complete_percent"));
465 472 }
466 473  
467 474 if (listNotice != null && listNotice.length() > 0) {
468   - List<NoticeInfo> infoLists = new ArrayList<>();
  475 + List<StepHourInfo> infoLists = new ArrayList<>();
469 476 for (int i = 0; i < listNotice.length(); i++){
470   - NoticeInfo noticeInfo = new NoticeInfo();
  477 + StepHourInfo noticeInfo = new StepHourInfo();
471 478 JSONObject ob = (JSONObject) listNotice.get(i);
472 479 noticeInfo.setId(ob.optInt("id"));
473 480 noticeInfo.setContent(ob.optString("notice_content"));
474 481 infoLists.add(noticeInfo);
475 482 }
476 483  
477   - mTagetInfo.setNoticeList(infoLists);
  484 + mTagetTopInfo.setStepHourList(infoLists);
478 485 }
479 486  
480 487 }
481 488 } catch (JSONException e) {
482 489 e.printStackTrace();
483   - mTagetInfo = new TagetInfo();;
  490 + mTagetTopInfo = new TagetTopInfo();;
484 491 }
485 492  
486 493 loadUI();
... ... @@ -492,7 +499,7 @@
492 499 tvRemain.setText(String.valueOf(mTagetInfo.getStepRemain()));
493 500 tvRateDone.setText(getResources().getString(R.string.percent_unit, mTagetInfo.getCompletePercent()));
494 501  
495   - List<NoticeInfo> infoLists = mTagetInfo.getNoticeList();
  502 + List<StepHourInfo> infoLists = mTagetTopInfo.getStepHourList();
496 503 if (infoLists != null && infoLists.size() >0){
497 504 mNoticeAdapter = new NoticeAdapter(mContext, infoLists);
498 505 lvNotice.setAdapter(mNoticeAdapter);
499 506  
500 507  
... ... @@ -573,13 +580,13 @@
573 580 @Override
574 581 public void onDestroy() {
575 582 super.onDestroy();
576   -
577   -
  583 + // create log step
  584 + creatLogStep();
578 585 }
579 586  
580   - private void creatLogStep(Date date, Const.STEP_TYPE stepType){
  587 + private void creatLogStep(){
581 588  
582   - LLAPIManager.homePage(date, stepType, new LLAPIManagerListener() {
  589 + LLAPIManager.createLog(stepType, stepCount, mFromDate, mToDate, new LLAPIManagerListener() {
583 590 @Override
584 591 public void onError(Error error) {
585 592 Debug.error("Get data history error");
... ... @@ -588,8 +595,6 @@
588 595 @Override
589 596 public void onSuccess(String json) {
590 597 Debug.error("Get data history success");
591   - hiddenDialog();
592   - loadDataDone(json);
593 598 }
594 599  
595 600 @Override
app/src/main/java/com/dinhcv/lifelogpedometer/model/structure/top/StepHourInfo.java
  1 +package com.dinhcv.lifelogpedometer.model.structure.top;
  2 +
  3 +public class StepHourInfo {
  4 + private int hour;
  5 + private int step;
  6 +
  7 + public int getHour() {
  8 + return hour;
  9 + }
  10 +
  11 + public void setHour(int hour) {
  12 + this.hour = hour;
  13 + }
  14 +
  15 + public int getStep() {
  16 + return step;
  17 + }
  18 +
  19 + public void setStep(int step) {
  20 + this.step = step;
  21 + }
  22 +
  23 +}
app/src/main/java/com/dinhcv/lifelogpedometer/model/structure/top/TagetTopInfo.java
  1 +package com.dinhcv.lifelogpedometer.model.structure.top;
  2 +
  3 +import com.dinhcv.lifelogpedometer.model.structure.home.NoticeInfo;
  4 +
  5 +import java.util.List;
  6 +
  7 +public class TagetTopInfo {
  8 + private String taget;
  9 + private String steps;
  10 + private String stepRemain;
  11 + private String completePercent;
  12 + private String calo;
  13 + private String distance;
  14 + private String time;
  15 +
  16 + private List<StepHourInfo> stepHourList;
  17 +
  18 +
  19 + public String getTaget() {
  20 + return taget;
  21 + }
  22 +
  23 + public void setTaget(String taget) {
  24 + this.taget = taget;
  25 + }
  26 +
  27 + public String getSteps() {
  28 + return steps;
  29 + }
  30 +
  31 + public void setSteps(String steps) {
  32 + this.steps = steps;
  33 + }
  34 +
  35 + public String getStepRemain() {
  36 + return stepRemain;
  37 + }
  38 +
  39 + public void setStepRemain(String stepRemain) {
  40 + this.stepRemain = stepRemain;
  41 + }
  42 +
  43 + public String getCompletePercent() {
  44 + return completePercent;
  45 + }
  46 +
  47 + public void setCompletePercent(String completePercent) {
  48 + this.completePercent = completePercent;
  49 + }
  50 +
  51 + public String getCalo() {
  52 + return calo;
  53 + }
  54 +
  55 + public void setCalo(String calo) {
  56 + this.calo = calo;
  57 + }
  58 +
  59 + public String getDistance() {
  60 + return distance;
  61 + }
  62 +
  63 + public void setDistance(String distance) {
  64 + this.distance = distance;
  65 + }
  66 +
  67 + public String getTime() {
  68 + return time;
  69 + }
  70 +
  71 + public void setTime(String taget) {
  72 + this.time = time;
  73 + }
  74 +
  75 + public void setStepHourList(List<StepHourInfo> stepHourList){
  76 + this.stepHourList = stepHourList;
  77 + }
  78 +
  79 + public List<StepHourInfo> getStepHourList(){
  80 + return stepHourList;
  81 + }
  82 +
  83 +}
app/src/main/java/com/dinhcv/lifelogpedometer/portal/LLAPIManager.java
... ... @@ -61,8 +61,13 @@
61 61 public static String URL_FORGET_PASS_CONFIRM_INFO = "forgetPass/confirm";
62 62 // Refesh Token
63 63 public static String URL_REFESH_TOKEN_INFO = "refreshToken";
  64 + // Top info
  65 + public static String URL_TOP_INFO = "api/top";
  66 + // Refesh Token
  67 + public static String URL_CREATE_LOG_INFO = "api/createLog";
64 68  
65 69  
  70 +
66 71 private static int deviationValue = 1;
67 72  
68 73 public static void login(final Context context, final String email, final String pass, final LLAPIManagerListener action) {
... ... @@ -703,6 +708,171 @@
703 708 .header("Content-Type", "application/json")
704 709 .addHeader("token", APIResponse.getInstance().getToken())
705 710 .get();
  711 +
  712 + OkHttpClient httpClient = new OkHttpClient.Builder()
  713 + .readTimeout(30, TimeUnit.SECONDS)
  714 + .writeTimeout(30, TimeUnit.SECONDS)
  715 + .connectTimeout(30, TimeUnit.SECONDS)
  716 + .build();
  717 +
  718 + String jsonString = null;
  719 + try {
  720 + Response response = httpClient.newCall(request.build()).execute();
  721 + if (response == null) {
  722 + action.onError(null);
  723 + Debug.error("Response is null");
  724 + return null;
  725 + }
  726 + jsonString = getResponseData(response.body());
  727 +
  728 + } catch (IOException e) {
  729 + Debug.normal("Error %s", e.getMessage());
  730 + }
  731 + return jsonString;
  732 + }
  733 +
  734 + @Override
  735 + protected void onCancelled() {
  736 + super.onCancelled();
  737 + action.onError(null);
  738 + }
  739 +
  740 + @Override
  741 + protected void onPostExecute(String jsonString) {
  742 + super.onPostExecute(jsonString);
  743 + if ((jsonString == null) || (jsonString.isEmpty())) {
  744 + action.onError(null);
  745 + return;
  746 + }
  747 + Debug.normal("String body: "+jsonString);
  748 +
  749 + try {
  750 + JSONObject jsonObject = new JSONObject(jsonString);
  751 + int status = jsonObject.optInt("status");
  752 + if (status == 1) {
  753 + action.onSuccess(jsonString);
  754 + } else {
  755 + action.onError(new Error(jsonObject.getString("message")));
  756 + }
  757 + } catch (JSONException e) {
  758 + action.onError(null);
  759 + }
  760 + }
  761 + }.execute();
  762 + }
  763 +
  764 + public static void topInfo(final Date date, final Const.STEP_TYPE stepType, final LLAPIManagerListener action) {
  765 + new AsyncTask<Void, Void, String>() {
  766 +
  767 +
  768 + @Override
  769 + protected void onPreExecute() {
  770 + super.onPreExecute();
  771 + }
  772 +
  773 + @Override
  774 + protected String doInBackground(Void... voids) {
  775 +
  776 + String dateStr = Utils.dateToStringFormatDayMonthYear(date);
  777 + Debug.normal("Token: "+ APIResponse.getInstance().getToken());
  778 + HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_TOP_INFO + "/"+ stepType.value +"/"+ dateStr);
  779 + Debug.normal("URL: "+baseUrl.toString());
  780 + final Request.Builder request = new Request.Builder()
  781 + .url(baseUrl)
  782 + .header("User-Agent", Utils.getCustomUA())
  783 + .header("Content-Type", "application/json")
  784 + .addHeader("token", APIResponse.getInstance().getToken())
  785 + .get();
  786 +
  787 + OkHttpClient httpClient = new OkHttpClient.Builder()
  788 + .readTimeout(30, TimeUnit.SECONDS)
  789 + .writeTimeout(30, TimeUnit.SECONDS)
  790 + .connectTimeout(30, TimeUnit.SECONDS)
  791 + .build();
  792 +
  793 + String jsonString = null;
  794 + try {
  795 + Response response = httpClient.newCall(request.build()).execute();
  796 + if (response == null) {
  797 + action.onError(null);
  798 + Debug.error("Response is null");
  799 + return null;
  800 + }
  801 + jsonString = getResponseData(response.body());
  802 +
  803 + } catch (IOException e) {
  804 + Debug.normal("Error %s", e.getMessage());
  805 + }
  806 + return jsonString;
  807 + }
  808 +
  809 + @Override
  810 + protected void onCancelled() {
  811 + super.onCancelled();
  812 + action.onError(null);
  813 + }
  814 +
  815 + @Override
  816 + protected void onPostExecute(String jsonString) {
  817 + super.onPostExecute(jsonString);
  818 + if ((jsonString == null) || (jsonString.isEmpty())) {
  819 + action.onError(null);
  820 + return;
  821 + }
  822 + Debug.normal("String body: "+jsonString);
  823 +
  824 + try {
  825 + JSONObject jsonObject = new JSONObject(jsonString);
  826 + int status = jsonObject.optInt("status");
  827 + if (status == 1) {
  828 + action.onSuccess(jsonString);
  829 + } else {
  830 + action.onError(new Error(jsonObject.getString("message")));
  831 + }
  832 + } catch (JSONException e) {
  833 + action.onError(null);
  834 + }
  835 + }
  836 + }.execute();
  837 + }
  838 +
  839 + public static void createLog(final Const.STEP_TYPE stepType, final int stepCount, final Date startTime,
  840 + final Date endTime, final LLAPIManagerListener action) {
  841 + new AsyncTask<Void, Void, String>() {
  842 +
  843 +
  844 + @Override
  845 + protected void onPreExecute() {
  846 + super.onPreExecute();
  847 + }
  848 +
  849 + @Override
  850 + protected String doInBackground(Void... voids) {
  851 + HttpUrl baseUrl = HttpUrl.parse(URL_ROOT + URL_CREATE_LOG_INFO);
  852 + JSONObject jsonObject = new JSONObject();
  853 + String startT = Utils.convertDate2DateTimeString(startTime);
  854 + String endT = Utils.convertDate2DateTimeString(endTime);
  855 + try {
  856 + jsonObject.put("mode", stepType.value);
  857 + jsonObject.put("numStep", stepCount);
  858 + jsonObject.put("startTime", startT);
  859 + jsonObject.put("endTime", endT);
  860 +
  861 + Debug.warn("Json data: " + jsonObject.toString());
  862 + } catch (JSONException e) {
  863 + Debug.normal("Error ", e.getMessage());
  864 + }
  865 +
  866 + MediaType JSON
  867 + = MediaType.parse("application/json");
  868 + RequestBody requestBody = RequestBody.create(JSON, jsonObject.toString());
  869 +
  870 + final Request.Builder request = new Request.Builder()
  871 + .url(baseUrl)
  872 + .header("User-Agent", Utils.getCustomUA())
  873 + .header("Content-Type", "application/json")
  874 + .addHeader("token", APIResponse.getInstance().getToken())
  875 + .post(requestBody);
706 876  
707 877 OkHttpClient httpClient = new OkHttpClient.Builder()
708 878 .readTimeout(30, TimeUnit.SECONDS)
app/src/main/java/com/dinhcv/lifelogpedometer/utils/Utils.java
... ... @@ -255,6 +255,11 @@
255 255 return date;
256 256 }
257 257  
  258 + public static String convertDate2DateTimeString(Date date) {
  259 + DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  260 + return df.format(date);
  261 + }
  262 +
258 263 public static String convertDate2DayString(Date date) {
259 264 DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
260 265 return df.format(date);