Blame view

app/src/main/java/com/dinhcv/lifelogpedometer/activity/SnsCreateGroupFragment.java 6.32 KB
7f095a929   chudinhbka@gmail.com   Create GIT Project
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
  package com.dinhcv.lifelogpedometer.activity;
  
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.support.v7.app.AlertDialog;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ToggleButton;
  
  import com.dinhcv.lifelogpedometer.R;
  import com.dinhcv.lifelogpedometer.network.ApiService;
  import com.dinhcv.lifelogpedometer.network.ApiUtils;
  import com.dinhcv.lifelogpedometer.portal.APIResponse;
  import com.dinhcv.lifelogpedometer.utils.Debug;
  import com.dinhcv.lifelogpedometer.utils.Utils;
  import com.google.gson.JsonObject;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import butterknife.BindView;
  import butterknife.ButterKnife;
  import butterknife.OnClick;
  import retrofit2.Call;
  import retrofit2.Callback;
  import retrofit2.Response;
  
  public class SnsCreateGroupFragment extends FragmentBase {
      private View mRootView;
      @BindView(R.id.edt_name)
      EditText mEdtName;
      @BindView(R.id.toggle_walk)
      ToggleButton mToggleWalk;
      @BindView(R.id.toggle_bike)
      ToggleButton mToggleBike;
      @BindView(R.id.toggle_run)
      ToggleButton mToggleRun;
      @BindView(R.id.toggle_step)
      ToggleButton mToggleStep;
      @BindView(R.id.toggle_gym)
      ToggleButton mToggleGym;
      @BindView(R.id.toggle_beginner)
      ToggleButton mToggleBeginner;
      @BindView(R.id.edt_goal)
      EditText mEdtGoal;
      @BindView(R.id.edt_walk_goal)
      EditText mEdtWalkGoal;
      @BindView(R.id.edt_run_goal)
      EditText mEdtRunGoal;
      @BindView(R.id.edt_bike_goal)
      EditText mEdtBikeGoal;
      @BindView(R.id.btn_create_group)
      Button mBtnCreateGroup;
  
      private String mName = "";
      private String mGoal = "";
      private int mWalkActive = 1;
      private int mBikeActive = 1;
      private int mRunActive = 1;
      private int mStepActive = 1;
      private int mGymActive = 1;
      private int mBeginnerActive = 1;
      private int mWalkGoal = 0;
      private int mRunGoal = 0;
      private int mBikeGoal = 0;
  
      private ApiService mService;
  
      public static final String TAG = "SnsCreateGroupFragment";
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          mRootView = inflater.inflate(R.layout.fragment_sns_create_group, container, false);
          ButterKnife.bind(this, mRootView);
  
          return mRootView;
      }
  
      private void saveDate() {
          mName = mEdtName.getText().toString().trim();
          mGoal = mEdtGoal.getText().toString().trim();
          mWalkActive = mToggleWalk.isChecked() ? 1 : 0;
          mBikeActive = mToggleBike.isChecked() ? 1 : 0;
          mRunActive = mToggleRun.isChecked() ? 1 : 0;
          mStepActive = mToggleStep.isChecked() ? 1 : 0;
          mGymActive = mToggleGym.isChecked() ? 1 : 0;
          mBeginnerActive = mToggleBeginner.isChecked() ? 1 : 0;
          mWalkGoal = Utils.parseString2Int(mEdtWalkGoal.getText().toString().trim(), 0);
          mRunGoal = Utils.parseString2Int(mEdtRunGoal.getText().toString().trim(), 0);
          mBikeGoal = Utils.parseString2Int(mEdtBikeGoal.getText().toString().trim(), 0);
      }
  
      private boolean isValidInputData() {
          if (mEdtName.getText().toString().isEmpty()) {
              mEdtName.setError(getString(R.string.error_empty_text));
              return false;
          }
          if (mEdtGoal.getText().toString().isEmpty()) {
              mEdtGoal.setError(getString(R.string.error_empty_text));
              return false;
          }
          return true;
      }
  
      private void handleCreateGroup() {
          showLoadingDialog();
          Map<String, String> header = new HashMap<>();
          header.put("token", APIResponse.getInstance().getToken());
  
          Map<String, String> params = new HashMap<>();
          params.put("group_name", mName);
          params.put("walk_mode_active", mWalkActive + "");
          params.put("run_mode_active", mRunActive + "");
          params.put("bike_mode_active", mBikeActive + "");
          params.put("step_mode_active", mStepActive + "");
          params.put("gym_mode_active", mGymActive + "");
          params.put("beginer_mode_active", mBeginnerActive + "");
          params.put("goal", mGoal);
  //        params.put("map_id", "0");
          params.put("walk_mode_goal", mWalkGoal + "");
          params.put("run_mode_goal", mRunGoal + "");
          params.put("bike_mode_goal", mBikeGoal + "");
  
          mService = ApiUtils.getApiService();
          mService.createGroup(header, params).enqueue(new Callback<JsonObject>() {
              @Override
              public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                  if (response.isSuccessful()) {
                      handleShowStatusAlert(true);
                      dismissLoadingDialog();
                  } else {
                      int statusCode = response.code();
                      // handle request errors depending on status code
                      Log.i(TAG, "Fail 1" + statusCode);
                      handleShowStatusAlert(false);
                      dismissLoadingDialog();
                  }
              }
  
              @Override
              public void onFailure(Call<JsonObject> call, Throwable t) {
                  Log.i(TAG, "Fail 2" + t);
                  handleShowStatusAlert(false);
                  dismissLoadingDialog();
              }
          });
      }
  
      private void handleShowStatusAlert(boolean isSuccess) {
          String title = getString(R.string.msg_create_group_title);
          String message = "";
          if (isSuccess) {
              message = getString(R.string.msg_create_group_success);
          } else {
              message = getString(R.string.msg_create_group_fail);
          }
          AlertDialog.Builder builder = getAlert(title, message);
          builder.setPositiveButton(getString(R.string.msg_ok), new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  //Do nothing
              }
          }).show();
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
          Debug.normal("SonLT", "On DeAttack");
      }
  
      @OnClick(R.id.btn_create_group)
      public void createGroup() {
          saveDate();
          if (!isValidInputData()){
              return;
          }
          handleCreateGroup();
      }
  }