Created
December 11, 2016 18:29
-
-
Save rohmanhakim/bce9a114fc906224d1ac3b1e323867f7 to your computer and use it in GitHub Desktop.
Registration Activity Demo for Reactive Programming in Android tutorial ()
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.rohmanhakim.androidreactivedemo; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.List; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Response; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
public class RegistrationDemoActivity extends AppCompatActivity { | |
EditText etEmail; | |
TextView textEmailAlert; | |
EditText etPassword; | |
TextView textPasswordAlert; | |
EditText etPasswordConfirmation; | |
TextView textPasswordConfirmationAlert; | |
Button btnSubmit; | |
Retrofit retrofit; | |
SampleService service; | |
Call<List<String>> emailCalls; | |
TextWatcher emailChangedListener; | |
TextWatcher passwordChangedListener; | |
TextWatcher passwordConfirmationChangedListener; | |
boolean emailValid; | |
boolean passwordValid; | |
boolean passwordConfirmationValid; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_registration_demo); | |
// Inisialisasi form | |
etEmail = (EditText) findViewById(R.id.et_email); | |
textEmailAlert = (TextView) findViewById(R.id.text_email_alert); | |
etPassword = (EditText) findViewById(R.id.et_password); | |
textPasswordAlert = (TextView) findViewById(R.id.text_password_alert); | |
etPasswordConfirmation = (EditText) findViewById(R.id.et_password_confirmation); | |
textPasswordConfirmationAlert = (TextView) findViewById(R.id.text_password_confirmation_alert); | |
btnSubmit = (Button) findViewById(R.id.btn_submit); | |
// Inisialisasi state (keadaan) default form | |
textEmailAlert.setVisibility(View.GONE); | |
textPasswordAlert.setVisibility(View.GONE); | |
textPasswordConfirmationAlert.setVisibility(View.GONE); | |
btnSubmit.setEnabled(false); | |
emailValid = false; | |
passwordValid = false; | |
passwordConfirmationValid = false; | |
// Inisialisasi edittext TextChanged() listener, untuk: | |
// field email, | |
emailChangedListener = new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
validateEmail(); | |
} | |
}; | |
// field password | |
passwordChangedListener = new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
validatePassword(); | |
validatePasswordConfirmation(); | |
} | |
}; | |
// dan field password confirmation | |
passwordConfirmationChangedListener = new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | |
} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
validatePasswordConfirmation(); | |
} | |
}; | |
// Tambahkan text changed listener untuk semua field | |
etEmail.addTextChangedListener(emailChangedListener); | |
etPassword.addTextChangedListener(passwordChangedListener); | |
etPasswordConfirmation.addTextChangedListener(passwordConfirmationChangedListener); | |
// Inisialisasi retrofit | |
retrofit = new Retrofit.Builder() | |
.baseUrl(getString(R.string.base_url)) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); | |
// Inisialisasi service sample | |
service = retrofit.create(SampleService.class); | |
} | |
public void validateEmail(){ | |
// jika user mengetik 3 karakter atau lebih di field email, trigger API call untuk mendapatkan daftar email | |
final String input = etEmail.getText().toString(); | |
emailCalls = service.getEmails(); | |
if(input.length() >= 3){ | |
emailCalls.enqueue(new Callback<List<String>>() { | |
@Override | |
public void onResponse(Call<List<String>> call, Response<List<String>> response) { | |
// jika email sudah tersedia, tampilkan peringatan | |
if(response.body().contains(input.trim())){ | |
showEmailExistAlert(true); | |
emailValid = false; | |
} else { | |
showEmailExistAlert(false); | |
emailValid = true; | |
} | |
validateButton(); | |
} | |
@Override | |
public void onFailure(Call<List<String>> call, Throwable t) { | |
// cukup tampilkan toast jika ada error | |
Toast.makeText(RegistrationDemoActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
} | |
public void validatePassword(){ | |
// jika password < 6 karakter, tampilkan peringatan | |
String input = etPassword.getText().toString(); | |
if(!input.equals("") && input.length() < 6){ | |
showPasswordMinimalAlert(true); | |
passwordValid = false; | |
} else { | |
showPasswordMinimalAlert(false); | |
passwordValid = true; | |
} | |
validateButton(); | |
} | |
public void validatePasswordConfirmation(){ | |
// jika konfirmasi password tidak sesuai tampilkan peringatan | |
String input = etPasswordConfirmation.getText().toString(); | |
if(!input.equals(etPassword.getText().toString())){ | |
showPasswordConfirmationAlert(true); | |
passwordConfirmationValid = false; | |
} else { | |
passwordConfirmationValid = true; | |
showPasswordConfirmationAlert(false); | |
} | |
validateButton(); | |
} | |
public void validateButton(){ | |
// jika semua field sudah terisi, enable button submit | |
if(!etEmail.getText().toString().equals("") | |
&& !etPassword.getText().toString().equals("") | |
&& !etPasswordConfirmation.getText().toString().equals("") | |
&& emailValid | |
&& passwordValid | |
&& passwordConfirmationValid){ | |
btnSubmit.setEnabled(true); | |
} else { | |
btnSubmit.setEnabled(false); | |
} | |
} | |
public void showEmailExistAlert(boolean value){ | |
if(value == true) { | |
textEmailAlert.setText(getString(R.string.email_exist_alert)); | |
textEmailAlert.setVisibility(View.VISIBLE); | |
} else { | |
textEmailAlert.setVisibility(View.GONE); | |
} | |
} | |
public void showPasswordMinimalAlert(boolean value){ | |
if(value == true) { | |
textPasswordAlert.setText(getString(R.string.password_minimal_alert)); | |
textPasswordAlert.setVisibility(View.VISIBLE); | |
} else { | |
textPasswordAlert.setVisibility(View.GONE); | |
} | |
} | |
public void showPasswordConfirmationAlert(boolean value){ | |
if(value == true){ | |
textPasswordConfirmationAlert.setText(getString(R.string.password_confirmation_does_not_match_alert)); | |
textPasswordConfirmationAlert.setVisibility(View.VISIBLE); | |
} else { | |
textPasswordConfirmationAlert.setVisibility(View.GONE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment