bluhalcyon

RegisterUserActivity

Jan 31st, 2017
140
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.projects.mikes.temperaturelogger;
  2.  
  3. import android.app.AlertDialog;
  4. import android.app.ProgressDialog;
  5. import android.support.design.widget.TextInputEditText;
  6. import android.support.v4.text.TextUtilsCompat;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.text.TextUtils;
  10. import android.util.Log;
  11. import android.view.View;
  12.  
  13. import com.eyro.mesosfer.Mesosfer;
  14. import com.eyro.mesosfer.MesosferException;
  15. import com.eyro.mesosfer.MesosferUser;
  16. import com.eyro.mesosfer.RegisterCallback;
  17.  
  18. import java.util.Locale;
  19.  
  20. public class RegisterUser extends AppCompatActivity {
  21.     private TextInputEditText txtEmail,txtPassword,txtFirstName,txtLastName;
  22.     private String email,password,firstName,lastName;
  23.     private ProgressDialog pdLoading;
  24.     private AlertDialog adDialog;
  25.  
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_register_user);
  30.         email = "";
  31.         password = "";
  32.         firstName = "";
  33.         lastName = "";
  34.         if(getSupportActionBar() != null){
  35.             getSupportActionBar().setTitle("Register User");
  36.         }
  37.  
  38.         txtEmail = (TextInputEditText)findViewById(R.id.txtEmail);
  39.         txtPassword = (TextInputEditText)findViewById(R.id.txtPwd);
  40.         txtFirstName = (TextInputEditText)findViewById(R.id.txtFirstName);
  41.         txtLastName = (TextInputEditText)findViewById(R.id.txtLastName);
  42.  
  43.         pdLoading = new ProgressDialog(this);
  44.         pdLoading.setIndeterminate(true);
  45.         pdLoading.setCancelable(false);
  46.         pdLoading.setCanceledOnTouchOutside(false);
  47.     }
  48.  
  49.     public void handleRegister(View view){
  50.         email = txtEmail.getText().toString();
  51.         password = txtPassword.getText().toString();
  52.         firstName = txtFirstName.getText().toString();
  53.         lastName = txtLastName.getText().toString();
  54.  
  55.         if(!isInputValid()){
  56.             return;
  57.         }
  58.  
  59.         registerNewUser();
  60.  
  61.     }
  62.  
  63.     private boolean isInputValid(){
  64.         if(TextUtils.isEmpty(email)){
  65.             txtEmail.setError("Email must be filled");
  66.             return false;
  67.         }
  68.         if(TextUtils.isEmpty(password)){
  69.             txtPassword.setError("Password must be filled");
  70.             return false;
  71.         }
  72.         if(TextUtils.isEmpty(firstName)){
  73.             txtFirstName.setError("First Name must be filled");
  74.             return false;
  75.         }
  76.         if(TextUtils.isEmpty(lastName)){
  77.             txtLastName.setError("Last Name must be filled");
  78.             return false;
  79.         }
  80.         return true;
  81.     }
  82.     private void registerNewUser(){
  83.         pdLoading.setMessage("Registering new user, please wait...");
  84.         pdLoading.show();
  85.  
  86.         MesosferUser newUser = MesosferUser.createUser();
  87.  
  88.         newUser.setEmail(email);
  89.         newUser.setPassword(password);
  90.         newUser.setFirstName(firstName);
  91.         newUser.setLastName(lastName);
  92.  
  93.         newUser.registerAsync(new RegisterCallback() {
  94.             @Override
  95.             public void done(MesosferException e) {
  96.                 // hide progress dialog loading
  97.                 pdLoading.dismiss();
  98.  
  99.                 // setup alert dialog builder
  100.                 AlertDialog.Builder builder = new AlertDialog.Builder(RegisterUser.this);
  101.                 builder.setNegativeButton(android.R.string.ok, null);
  102.  
  103.                 // check if there is an exception happen
  104.                 if (e != null) {
  105.                     builder.setTitle("Error Occured");
  106.                     builder.setMessage(
  107.                             String.format(Locale.getDefault(), "Error code: %d\nDescription: %s",
  108.                                     e.getCode(), e.getMessage())
  109.                     );
  110.                     adDialog = builder.show();
  111.                     return;
  112.                 }
  113.  
  114.                 builder.setTitle("Register Success!");
  115.                 builder.setMessage("Thank you for registering!");
  116.                 adDialog = builder.show();
  117.             }
  118.         });
  119.     }
  120. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×