Created
November 11, 2016 06:36
-
-
Save Vikctar/d9962f372deab4effce4eaccfaa5c775 to your computer and use it in GitHub Desktop.
Showing sign up logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pathways.redwood.activity; | |
import android.app.ProgressDialog; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.android.volley.Request; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.StringRequest; | |
import com.loopj.android.http.AsyncHttpClient; | |
import com.loopj.android.http.AsyncHttpResponseHandler; | |
import com.loopj.android.http.RequestParams; | |
import com.pathways.redwood.R; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.HashMap; | |
import java.util.Map; | |
import helper.SQLiteHandler; | |
import helper.SessionManager; | |
import volley.AppController; | |
import volley.ConfigUrl; | |
public class SignUpActivity extends AppCompatActivity { | |
private static final String TAG = SignUpActivity.class.getSimpleName(); | |
private Button btnRegister; | |
private EditText inputFirstName; | |
private EditText inputLastName; | |
private EditText inputPhone; | |
private EditText inputEmail; | |
private EditText inputPassword; | |
private ProgressDialog progressDialog; | |
private SessionManager sessionManager; | |
private SQLiteHandler db; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_sign_up); | |
inputFirstName = (EditText) findViewById(R.id.first_name); | |
inputLastName = (EditText) findViewById(R.id.last_name); | |
inputPhone = (EditText) findViewById(R.id.phone); | |
inputEmail = (EditText) findViewById(R.id.email); | |
inputPassword = (EditText) findViewById(R.id.input_password); | |
btnRegister = (Button) findViewById(R.id.button_sign_up); | |
// Progress dialog | |
progressDialog = new ProgressDialog(this); | |
progressDialog.setCancelable(false); | |
// Session manager | |
sessionManager = new SessionManager(getApplicationContext()); | |
// SQLite database handler | |
db = new SQLiteHandler(getApplicationContext()); | |
// Check if user is already logged in or not | |
if (sessionManager.isLoggedIn()) { | |
// User is already logged in. Take him to main activity | |
Intent intent = new Intent(SignUpActivity.this, | |
MainActivity.class); | |
startActivity(intent); | |
finish(); | |
} | |
// Register Button Click event | |
btnRegister.setOnClickListener(new View.OnClickListener() { | |
public void onClick(View view) { | |
Log.d(TAG, "Registering.."); | |
String firstName = inputFirstName.getText().toString(); | |
String lastName = inputLastName.getText().toString(); | |
String phone = inputPhone.getText().toString(); | |
String email = inputEmail.getText().toString(); | |
String password = inputPassword.getText().toString(); | |
if (!firstName.isEmpty() && !lastName.isEmpty() && !phone.isEmpty() && !email.isEmpty() && !password.isEmpty()) { | |
registerClient(firstName, lastName, phone, email, password); | |
} else { | |
Toast.makeText(getApplicationContext(), | |
"Please enter your details!", Toast.LENGTH_LONG) | |
.show(); | |
} | |
} | |
}); | |
} | |
// public void register(View view) { | |
// String firstName = inputFirstName.getText().toString(); | |
// String lastName = inputLastName.getText().toString(); | |
// String phone = inputPhone.getText().toString(); | |
// String email = inputEmail.getText().toString(); | |
// String password = inputPassword.getText().toString(); | |
// | |
// AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); | |
// RequestParams requestParams = new RequestParams(); | |
// | |
// requestParams.add("first_name", firstName); | |
// requestParams.add("last_name", lastName); | |
// requestParams.add("phone", phone); | |
// requestParams.add("email", email); | |
// requestParams.add("password", password); | |
// | |
// asyncHttpClient.post("http://10.0.2.2/redwood_new/SignUp.php", requestParams, | |
// new AsyncHttpResponseHandler() { | |
// | |
// @SuppressWarnings("deprecation") | |
// public void onSuccess(String content) { | |
// Toast.makeText(getApplicationContext(), "Sign up successful", Toast.LENGTH_SHORT).show(); | |
// } | |
// }); | |
// } | |
public void login(View view) { | |
startActivity(new Intent(getApplicationContext(), LoginActivity.class)); | |
} | |
/** | |
* Function to store the patient in MYySQL database. | |
* Will post | |
* | |
* @param firstName of the client | |
* @param lastName of the client | |
* @param phone number of the client | |
* @param email of the client | |
* @param password for the client | |
*/ | |
private void registerClient(final String firstName, final String lastName, final String phone, | |
final String email, final String password) { | |
// Tag used to cancel the request | |
String tag_string_req = "req_register"; | |
progressDialog.setMessage("Registering..."); | |
showDialog(); | |
StringRequest stringRequest = new StringRequest(Request.Method.POST, ConfigUrl.URL_REGISTER, new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
Log.d(TAG, "Register response: " + response); | |
hideDialog(); | |
try { | |
JSONObject jsonObject = new JSONObject(response); | |
boolean error = jsonObject.getBoolean("error"); | |
if (!error) { | |
// Client successfully stored in MySQL | |
// Now store the user in SQLite | |
String uid = jsonObject.getString("uid"); | |
JSONObject client = jsonObject.getJSONObject("client"); | |
String firstName = client.getString("first_name"); | |
String lastName = client.getString("last_name"); | |
String phone = client.getString("phone"); | |
String email = client.getString("email"); | |
String created_at = client.getString("created_at"); | |
// Inserting row into client's table | |
db.addClient(firstName, lastName, phone, email, uid, created_at); | |
// Launch login activity | |
Intent intent = new Intent(getApplicationContext(), LoginActivity.class); | |
startActivity(intent); | |
finish(); | |
} else { | |
// Error occurred in registration. Get the error | |
// message | |
String errorMsg = jsonObject.getString("error_msg"); | |
Toast.makeText(getApplicationContext(), | |
errorMsg, Toast.LENGTH_LONG).show(); | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError volleyError) { | |
Log.e(TAG, "Registration Error: " + volleyError.getMessage()); | |
Toast.makeText(getApplicationContext(), "Reg error "+ | |
volleyError.getMessage(), Toast.LENGTH_LONG).show(); | |
hideDialog(); | |
} | |
}) { | |
@Override | |
protected Map<String, String> getParams() { | |
// Posting params to register url | |
Map<String, String> params = new HashMap<>(); | |
params.put("tag", "register"); | |
params.put("first_name", firstName); | |
params.put("last_name", lastName); | |
params.put("phone", phone); | |
params.put("email", email); | |
params.put("password", password); | |
return params; | |
} | |
}; | |
// Adding request to request queue | |
AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req); | |
} | |
private void showDialog() { | |
if (!progressDialog.isShowing()) | |
progressDialog.show(); | |
} | |
private void hideDialog() { | |
if (progressDialog.isShowing()) | |
progressDialog.dismiss(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment