Created
May 2, 2016 20:12
-
-
Save mandybess/13bf09680b1abbd1cd19ba1ea62953df to your computer and use it in GitHub Desktop.
RxJava + MVP Signup Form Validation
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
public interface BaseView { | |
} |
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
public interface Presenter<V extends BaseView> { | |
void attachView(V mvpView); | |
void detachView(); | |
} |
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
public class SignUpActivity extends Activity implements SignUpView { | |
EditText firstName; | |
EditText lastName; | |
EditText email; | |
EditText zipCode; | |
EditText password; | |
Button submitButton; | |
SignUpPresenter presenter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
presenter = new SignUpPresenterImpl(this); | |
Observable<Boolean> firstNameObservable = | |
RxTextView.afterTextChangeEvents(firstName).map(presenter.isFirstNameValid()); | |
Observable<Boolean> lastNameObservable = | |
RxTextView.afterTextChangeEvents(lastName).map(presenter.isLastNameValid()); | |
Observable<Boolean> emailObservable = | |
RxTextView.afterTextChangeEvents(email).map(presenter.isEmailValid()); | |
Observable<Boolean> zipCodeObservable = | |
RxTextView.afterTextChangeEvents(zipCode).map(presenter.isZipCodeValid()); | |
Observable<Boolean> passwordObservable = | |
RxTextView.afterTextChangeEvents(password).map(presenter.isPasswordValid()); | |
firstNameObservable.subscribe(presenter.updateFirstNameViewState()); | |
lastNameObservable.subscribe(presenter.updateLastNameViewState()); | |
emailObservable.subscribe(presenter.updateEmailViewState()); | |
zipCodeObservable.subscribe(presenter.updateZipCodeViewState()); | |
passwordObservable.subscribe(presenter.updatePasswordViewState()); | |
Observable.combineLatest(firstNameObservable, lastNameObservable, emailObservable, | |
zipCodeObservable, passwordObservable, presenter.isFormValid()) | |
.subscribe(this::updateSubmitButtonViewState); | |
} | |
@Override | |
public void showProgress() { | |
} | |
@Override | |
public void hideProgress() { | |
} | |
@Override | |
public void showFirstNameError(CharSequence error) { | |
firstName.setError(error); | |
} | |
@Override | |
public void showLastNameError(CharSequence error) { | |
lastName.setError(error); | |
} | |
@Override | |
public void showEmailError(CharSequence error) { | |
email.setError(error); | |
} | |
@Override | |
public void showZipCodeError(CharSequence error) { | |
zipCode.setError(error); | |
} | |
@Override | |
public void showPasswordError(CharSequence error) { | |
password.setError(error); | |
} | |
@Override | |
public void navigateToMainScreen() { | |
} | |
@Override | |
public void updateSubmitButtonViewState(boolean enabled) { | |
submitButton.setEnabled(enabled); | |
} | |
} |
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
public interface SignUpPresenter extends Presenter<SignUpView> { | |
Func1<TextViewAfterTextChangeEvent, Boolean> isFirstNameValid(); | |
Action1<Boolean> updateFirstNameViewState(); | |
Func1<TextViewAfterTextChangeEvent, Boolean> isLastNameValid(); | |
Action1<Boolean> updateLastNameViewState(); | |
Func1<TextViewAfterTextChangeEvent, Boolean> isEmailValid(); | |
Action1<Boolean> updateEmailViewState(); | |
Func1<TextViewAfterTextChangeEvent, Boolean> isZipCodeValid(); | |
Action1<Boolean> updateZipCodeViewState(); | |
Func1<TextViewAfterTextChangeEvent, Boolean> isPasswordValid(); | |
Action1<Boolean> updatePasswordViewState(); | |
Func5<Boolean, Boolean, Boolean, Boolean, Boolean, Boolean> isFormValid(); | |
} |
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
public class SignUpPresenterImpl implements SignUpPresenter { | |
private SignUpView signUpView; | |
private final Context context; | |
public SignUpPresenterImpl(Context context) { | |
this.context = context; | |
} | |
@Override | |
public Func1<TextViewAfterTextChangeEvent, Boolean> isFirstNameValid() { | |
return textViewAfterTextChangeEvent -> FIRST_NAME_VALIDITY_CHECK; | |
} | |
@Override | |
public Action1<Boolean> updateFirstNameViewState() { | |
return isValid -> { | |
if (!isValid) { | |
signUpView.showFirstNameError(context.getString(R.string.firstname_error)); | |
} | |
}; | |
} | |
@Override | |
public Func1<TextViewAfterTextChangeEvent, Boolean> isLastNameValid() { | |
return textViewAfterTextChangeEvent -> LAST_NAME_VALIDITY_CHECK; | |
} | |
@Override | |
public Action1<Boolean> updateLastNameViewState() { | |
return isValid -> { | |
if (!isValid) { | |
signUpView.showLastNameError(R.string.lastname_error); | |
} | |
}; | |
} | |
@Override | |
public Func1<TextViewAfterTextChangeEvent, Boolean> isEmailValid() { | |
return textViewAfterTextChangeEvent -> EMAIL_VALIDITY_CHECK; | |
} | |
@Override | |
public Action1<Boolean> updateEmailViewState() { | |
return isValid -> { | |
if (!isValid) { | |
signUpView.showEmailError(R.string.email_error); | |
} | |
}; | |
} | |
@Override | |
public Func1<TextViewAfterTextChangeEvent, Boolean> isZipCodeValid() { | |
return textViewAfterTextChangeEvent -> ZIPCODE_VALIDITY_CHECK; | |
} | |
@Override | |
public Action1<Boolean> updateZipCodeViewState() { | |
return isValid -> { | |
if (!isValid) { | |
signUpView.showZipCodeError(R.string.zipcode_error); | |
} | |
}; | |
} | |
@Override | |
public Func1<TextViewAfterTextChangeEvent, Boolean> isPasswordValid() { | |
return textViewAfterTextChangeEvent -> PASSWORD_VALIDITY_CHECK; | |
} | |
@Override | |
public Action1<Boolean> updatePasswordViewState() { | |
return isValid -> { | |
if (!isValid) { | |
signUpView.showPasswordError(R.string.password_error); | |
} | |
}; | |
} | |
@Override | |
public Func5<Boolean, Boolean, Boolean, Boolean, Boolean, Boolean> isFormValid() { | |
return (isFirsNameValid, isLastNameValid, isEmailValid, isZipCodeValid, isPasswordValid) -> | |
isFirsNameValid | |
&& isLastNameValid | |
&& isEmailValid | |
&& isZipCodeValid | |
&& isPasswordValid; | |
} | |
@Override | |
public void attachView(SignUpView mvpView) { | |
signUpView = mvpView; | |
} | |
@Override | |
public void detachView() { | |
} | |
} |
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
public interface SignUpView extends BaseView { | |
void showProgress(); | |
void hideProgress(); | |
void showFirstNameError(CharSequence error); | |
void showLastNameError(CharSequence error); | |
void showEmailError(CharSequence error); | |
void showZipCodeError(CharSequence error); | |
void showPasswordError(CharSequence error); | |
void updateSubmitButtonViewState(boolean enabled); | |
void navigateToMainScreen(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool but it leaks