Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. /**
  2. * Interactor to login the user using username and password
  3. */
  4. public class UserLogin extends UseCase {
  5.  
  6. private final UserRepository userRepository;
  7. private String password;
  8. private String username;
  9.  
  10. public UserLogin(ThreadExecutor threadExecutor,
  11. PostExecutionThread postExecutionThread,
  12. UserRepository userRepository) {
  13. super(threadExecutor, postExecutionThread);
  14. this.userRepository = userRepository;
  15. }
  16.  
  17. /**
  18. * Initializes the interactor with the username and password to use for the authentication.
  19. *
  20. * @param username - username for the user
  21. * @param password - password for the user.
  22. */
  23. public UserLogin init(@NonNull String username, @NonNull String password) {
  24. this.username = username;
  25. this.password = password;
  26.  
  27. return this;
  28. }
  29.  
  30. /**
  31. * Builds the {@link UseCase} observable
  32. * @return an {@link} Observable that will emit the logged in {@link UserProfile}
  33. **/
  34. @Override
  35. public Observable buildUseCaseObservable() {
  36. if (this.username == null || this.password == null) {
  37. throw new IllegalArgumentException("init(username,password) not called, or called with null argument.");
  38. }
  39.  
  40. return Observable.concat(validate(), this.userRepository.user(this.username, this.password));
  41. }
  42.  
  43. private Observable validate() {
  44. return Observable.create(new Observable.OnSubscribe<Object>() {
  45. @Override
  46. public void call(Subscriber<? super Object> subscriber) {
  47. if (UserLogin.this.username.isEmpty()) {
  48. subscriber.onError(new InvalidEmailException());
  49. } else if (UserLogin.this.password.isEmpty()) {
  50. subscriber.onError(new InvalidLoginPasswordException());
  51. } else {
  52. subscriber.onCompleted();
  53. }
  54. }
  55. });
  56. }
  57. }
  58.  
  59. //So you would execute it like:
  60. //this.userLogin.init("myUser", "myPassword").execute(new UserLoginSubscriber())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement