Last active
September 1, 2015 23:07
-
-
Save shekibobo/87ec79f44018c56a238a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class LoginActivity extends Activity { | |
// Setup stuff | |
@Override | |
public void onResume() { | |
// Subscribe to the login view submit | |
AppObservable.bindActivity(this, loginView.getUserCredentialsObservable().subscribeOn(mainThread())) | |
.doOnNext(loginView.showProgress(true)) | |
.flatMap(creds -> getTokenObservable(creds)) | |
.subscribe(this::handleSuccess) | |
} | |
private Observable<Token> getTokenObservable(UserCredentials userCredentials) { | |
return apiService.getAuthToken(userCredentials) | |
.observeOn(mainThread()) | |
.onErrorResume(throwable -> { | |
if (throwable instanceof RetrofitError) { | |
handleRetrofitError((RetrofitError) throwable); | |
return new Token(null, null); | |
} | |
loginView.showProgress(false); | |
return Observable.error(throwable); | |
}) | |
.filter(token -> { | |
loginView.showProgress(false); | |
return !TextUtils.isEmpty(token.getAuthToken()); | |
}) | |
.doOnNext(token -> token.setUserName(userCredentials.getEmail())); | |
} | |
} |
This file contains hidden or 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
class LoginView extends LinearLayout { | |
// constructors and stuff | |
public getUserCredentialsObservable() { | |
return ViewObservable.clicks(submitButton).map(onClickEvent -> getUserCredentials()); | |
} | |
public UserCredentials getUserCredentials() { | |
emailField.setError(null); | |
passwordField.setError(null); | |
Observable.combineLatest(validEmail(), validPassword(), UserCredentials::new); | |
} | |
public Observable<String> validEmail() { | |
return Observable.just(emailField.getText().toString()) | |
.filter(email -> { | |
if (TextUtils.isEmpty(email) || !email.contains("@")) { | |
emailField.setError("Invalid email."); | |
return false; | |
} | |
return true; | |
}); | |
} | |
public Observable<String> validPassword() { | |
return Observable.just(passwordField.getText().toString()) | |
.filter(password -> { | |
if (password.length() < 6) { | |
passwordField.setError("Invalid password."); | |
return false; | |
} | |
return true; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment