Ejemplo n.º 1
0
        public RegistrationViewModel(IRepository repository = null)
        {
            _repository = repository ?? Locator.Current.GetService <IRepository>();

            _nameValidator            = new NameValidator();
            _phoneNumberValidator     = new PhoneNumberValidator();
            _emailValidator           = new EmailValidator();
            _passwordValidator        = new PasswordValidator();
            _confirmPasswordValidator = new ConfirmPasswordValidator();

            _stateStack = new Stack <int>();
            _stateStack.Push(0);

            NameValidationResult            = new ValidationResult();
            PhoneNumberValidationResult     = new ValidationResult();
            EmailValidationResult           = new ValidationResult();
            PasswordValidationResult        = new ValidationResult();
            ConfirmPasswordValidationResult = new ValidationResult();

            GoToPasswordEntryCommand = ReactiveCommand.Create(OnActionButtonClicked, outputScheduler: RxApp.MainThreadScheduler);
            GoToLoginCommand         = ReactiveCommand.Create(ShowLogin, outputScheduler: RxApp.MainThreadScheduler);
            BackCommand = ReactiveCommand.Create(OnBackButton, outputScheduler: RxApp.MainThreadScheduler);
            FormatPhoneNumberCommand = ReactiveCommand.Create((bool needFormat) => FormatPhoneNumber(needFormat), outputScheduler: RxApp.MainThreadScheduler);

            SignUpCommand = ReactiveCommand.CreateFromObservable((RegistrationData data) => _repository.SignUp(data), outputScheduler: RxApp.MainThreadScheduler);
            SignInCommand = ReactiveCommand.CreateFromObservable((LoginData data) => _repository.SignIn(data), outputScheduler: RxApp.MainThreadScheduler);

            this.WhenActivated((CompositeDisposable disposables) =>
            {
                GoToPasswordEntryCommand
                .SubscribeOn(RxApp.TaskpoolScheduler)
                .Subscribe()
                .DisposeWith(disposables);

                GoToPasswordEntryCommand
                .ThrownExceptions
                .Subscribe((ex) =>
                {
                    Debug.WriteLine("Error occurs: " + ex.Message);
                })
                .DisposeWith(disposables);

                this.WhenAnyValue(vm => vm.PhoneEntryFocused)
                .Subscribe(focused =>
                {
                    if (focused && string.IsNullOrWhiteSpace(PhoneNumber))
                    {
                        PhoneNumber = "+380";
                    }
                    else if (string.IsNullOrWhiteSpace(PhoneNumber) || PhoneNumber.Length < 7)
                    {
                        PhoneNumber = null;
                    }
                })
                .DisposeWith(disposables);

                SignUpCommand
                .SubscribeOn(RxApp.TaskpoolScheduler)
                .Subscribe(unit =>
                {
                    var loginData = new LoginData {
                        Username = Email, Password = Password
                    };
                    TryLogin(loginData);
                })
                .DisposeWith(disposables);

                SignUpCommand
                .ThrownExceptions
                .Subscribe(ex =>
                {
                    Debug.WriteLine("Error occurs: " + ex);
                })
                .DisposeWith(disposables);

                SignInCommand
                .SubscribeOn(RxApp.TaskpoolScheduler)
                .Subscribe(token =>
                {
                    NavigateToMain();
                })
                .DisposeWith(disposables);

                SignInCommand
                .ThrownExceptions
                .Subscribe(ex =>
                {
                    Debug.WriteLine("Error occurs: " + ex);
                })
                .DisposeWith(disposables);
            });
        }