Skip to content

Instantly share code, notes, and snippets.

@edBaev
Created November 24, 2014 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edBaev/d4e1e2a6b914426bc789 to your computer and use it in GitHub Desktop.
Save edBaev/d4e1e2a6b914426bc789 to your computer and use it in GitHub Desktop.
package com.blacksquared.changers.fragment.auth;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.blacksquared.changers.App;
import com.blacksquared.changers.R;
import com.blacksquared.changers.event.error.ErrorApiEvent;
import com.blacksquared.changers.event.auth.GetUsernameByEmailEvent;
import com.blacksquared.changers.event.auth.NoUsernameByEmailEvent;
import com.blacksquared.changers.event.auth.ResetPasswordEvent;
import com.blacksquared.changers.fragment.BaseFragment;
import com.blacksquared.changers.interfaces.IResetPass;
import com.blacksquared.changers.utils.Logger;
import com.blacksquared.changers.utils.Utils;
import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Rules;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.annotation.Email;
import com.mobsandgeeks.saripaar.annotation.Required;
import de.greenrobot.event.EventBus;
public class ForgotPasswordFragment extends BaseFragment implements Validator.ValidationListener {
@Required(order = 1, messageResId = R.string.error_enter_email)
// @Email(order = 2, messageResId = R.string.error_invalid_email)
private EditText forgotPassEditText;
private LinearLayout forgotPassLinearLayout;
private TextView forgotPassTextView;
private ProgressBar progressBar;
private Validator validator;
private boolean isInProgress = false;
private IResetPass iResetPass;
public static ForgotPasswordFragment newInstance() {
ForgotPasswordFragment fragment = new ForgotPasswordFragment();
return fragment;
}
public ForgotPasswordFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
iResetPass = (IResetPass) activity;
iChangeTitle.onChangeTitle(getString(R.string.forgot_password_actionbar_text));
} catch (ClassCastException e) {
Logger.e("Should implements IResetPass");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
showActionBar(false);
onControlPaddingSize.onSetABColorListener(R.color.action_bar_color);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_forgot_password, container, false);
initViews(view);
initData();
listeners();
return view;
}
private void initData() {
validator = new Validator(this);
validator.setValidationListener(this);
validator.put(forgotPassEditText,
Rules.regex(getString(R.string.error_invalid_email), Utils.EMAIL_ADDRESS, true));
}
private void initViews(View view) {
forgotPassEditText = (EditText) view.findViewById(R.id.forgotpass_email_editText);
if (App.dataManager.getTempEmail() != null) {
forgotPassEditText.setText(App.dataManager.getTempEmail().toString());
}
forgotPassLinearLayout = (LinearLayout) view.findViewById(R.id.forgotpass_bottomLinear);
forgotPassTextView = (TextView) view.findViewById(R.id.btn_reset_pass);
progressBar = (ProgressBar) view.findViewById(R.id.done_progress_bar);
}
private void listeners() {
forgotPassLinearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validator.validate();
}
});
}
@Override
public void onValidationSucceeded() {
signInButtonStartLoading();
App.apiManager.resetPassword(forgotPassEditText.getText().toString());
}
@Override
public void onValidationFailed(View failedView, Rule<?> failedRule) {
String message = failedRule.getFailureMessage();
if (failedView instanceof EditText) {
failedView.requestFocus();
((EditText) failedView).setError(message);
}
}
private void signInButtonStartLoading() {
forgotPassLinearLayout.setEnabled(false);
forgotPassEditText.setEnabled(false);
forgotPassTextView.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
isInProgress = true;
}
private void signInButtonStopLoading() {
progressBar.setVisibility(View.GONE);
forgotPassTextView.setVisibility(View.VISIBLE);
forgotPassLinearLayout.setEnabled(true);
forgotPassEditText.setEnabled(true);
isInProgress = false;
}
public void onEvent(GetUsernameByEmailEvent getUsernameByEmailEvent) {
EventBus.getDefault().removeStickyEvent(getUsernameByEmailEvent);
App.apiManager.resetPassword(getUsernameByEmailEvent.data);
}
public void onEvent(ResetPasswordEvent resetPasswordEvent) {
EventBus.getDefault().removeStickyEvent(resetPasswordEvent);
signInButtonStopLoading();
iResetPass.openDoneFragment();
}
public void onEvent(ErrorApiEvent errorApiEvent) {
EventBus.getDefault().removeStickyEvent(errorApiEvent);
signInButtonStopLoading();
Toast.makeText(getActivity(), errorApiEvent.message, Toast.LENGTH_SHORT).show();
}
public void onEvent(NoUsernameByEmailEvent errorApiEvent) {
EventBus.getDefault().removeStickyEvent(errorApiEvent);
Toast.makeText(getActivity(), "Email is not registered", Toast.LENGTH_SHORT).show();
signInButtonStopLoading();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (!isInProgress) {
getActivity().onBackPressed();
}
break;
}
return super.onOptionsItemSelected(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment