Guest User

Untitled

a guest
Dec 2nd, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. public sealed class DirectAuthViewModel : ReactiveObject, IDirectAuthViewModel
  2. {
  3. private readonly ObservableAsPropertyHelper<string> _errorMessage;
  4. private readonly ObservableAsPropertyHelper<bool> _hasErrors;
  5. private readonly ObservableAsPropertyHelper<bool> _isBusy;
  6. private readonly ReactiveCommand<Unit, Unit> _login;
  7.  
  8. public DirectAuthViewModel(
  9. IScheduler currentThread,
  10. IScheduler mainThread,
  11. IProvider provider)
  12. {
  13. var nameValid = this
  14. .WhenAnyValue(x => x.Username)
  15. .Select(name => !string.IsNullOrWhiteSpace(name));
  16.  
  17. var passwordValid = this
  18. .WhenAnyValue(x => x.Password)
  19. .Select(name => !string.IsNullOrWhiteSpace(name));
  20.  
  21. var canLogin = nameValid
  22. .CombineLatest(passwordValid, (name, password) => name && password)
  23. .DistinctUntilChanged();
  24.  
  25. _login = ReactiveCommand.CreateFromTask(
  26. () => provider.DirectAuth(Username, Password),
  27. canLogin, mainThread);
  28.  
  29. _errorMessage = _login
  30. .ThrownExceptions
  31. .Select(exception => exception.Message)
  32. .ToProperty(this, x => x.ErrorMessage, scheduler: currentThread);
  33.  
  34. _hasErrors = _login
  35. .ThrownExceptions
  36. .Select(exception => true)
  37. .Merge(_login.Select(unit => false))
  38. .ToProperty(this, x => x.HasErrors, scheduler: currentThread);
  39.  
  40. _isBusy = _login
  41. .IsExecuting
  42. .ToProperty(this, x => x.IsBusy, scheduler: currentThread);
  43.  
  44. _login.Subscribe(x => Username = string.Empty);
  45. _login.Subscribe(x => Password = string.Empty);
  46. }
  47.  
  48. [Reactive] public string Username { get; set; }
  49.  
  50. [Reactive] public string Password { get; set; }
  51.  
  52. public string ErrorMessage => _errorMessage.Value;
  53.  
  54. public bool HasErrors => _hasErrors.Value;
  55.  
  56. public bool IsBusy => _isBusy.Value;
  57.  
  58. public ICommand Login => _login;
  59. }
Add Comment
Please, Sign In to add comment