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 header = new HashMap<>(); header.put("token", APIResponse.getInstance().getToken()); Map 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() { @Override public void onResponse(Call call, Response 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 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(); } }