Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Interactor to login the user using username and password
- */
- public class UserLogin extends UseCase {
- private final UserRepository userRepository;
- private String password;
- private String username;
- public UserLogin(ThreadExecutor threadExecutor,
- PostExecutionThread postExecutionThread,
- UserRepository userRepository) {
- super(threadExecutor, postExecutionThread);
- this.userRepository = userRepository;
- }
- /**
- * Initializes the interactor with the username and password to use for the authentication.
- *
- * @param username - username for the user
- * @param password - password for the user.
- */
- public UserLogin init(@NonNull String username, @NonNull String password) {
- this.username = username;
- this.password = password;
- return this;
- }
- /**
- * Builds the {@link UseCase} observable
- * @return an {@link} Observable that will emit the logged in {@link UserProfile}
- **/
- @Override
- public Observable buildUseCaseObservable() {
- if (this.username == null || this.password == null) {
- throw new IllegalArgumentException("init(username,password) not called, or called with null argument.");
- }
- return Observable.concat(validate(), this.userRepository.user(this.username, this.password));
- }
- private Observable validate() {
- return Observable.create(new Observable.OnSubscribe<Object>() {
- @Override
- public void call(Subscriber<? super Object> subscriber) {
- if (UserLogin.this.username.isEmpty()) {
- subscriber.onError(new InvalidEmailException());
- } else if (UserLogin.this.password.isEmpty()) {
- subscriber.onError(new InvalidLoginPasswordException());
- } else {
- subscriber.onCompleted();
- }
- }
- });
- }
- }
- //So you would execute it like:
- //this.userLogin.init("myUser", "myPassword").execute(new UserLoginSubscriber())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement