Skip to content

Instantly share code, notes, and snippets.

@abhimuktheeswarar
Created September 29, 2016 10:05
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 abhimuktheeswarar/4850fcfe42bcccb342c61973b8390cda to your computer and use it in GitHub Desktop.
Save abhimuktheeswarar/4850fcfe42bcccb342c61973b8390cda to your computer and use it in GitHub Desktop.
Usecase for dynamic queries
/**
* 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())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment