Exemple #1
0
        public void RegistrateOnApp(object param)
        {
            RegistrateCurrentUser user = new RegistrateCurrentUser();

            if (Name == "" || Password == "" || PasswordSecond == "")
            {
                ErrorMessage = "*Все поля должны быть заполнены";
                return;
            }
            else if (DateBirthday > DateTime.Now)
            {
                ErrorMessage = "*Возраст не может быть больше текущей даты";
                return;
            }
            else if (Password != PasswordSecond)
            {
                ErrorMessage = "*Пароли не совпадают";
                return;
            }


            byte[] tmp = { };

            user.Login     = Name;
            user.Password  = Password;
            user.Gender    = Gender;
            user.UserImage = tmp;

            user.DateBirthday = DateBirthday.ToString();
            UResult result = (UResult)_loginRegister.AddNewUserOnDB(user);

            if (result == UResult.Access)
            {
                OpenLogin.Execute(null);
            }
            else if (result == UResult.UserFailed)
            {
                ErrorMessage = "*Такой пользователь уже существует";
            }
        }
        public AuthenticationViewModel(INavigatableScreen screen = null)
        {
            NavigationScreen = (screen ?? Locator.Current.GetService <INavigatableScreen>());

            var canLogin = this.WhenAny(x => x.Email, x => x.Password,
                                        (e, p) => !string.IsNullOrEmpty(e.Value) && !string.IsNullOrEmpty(p.Value));


            Login = ReactiveCommand
                    .CreateAsyncTask(canLogin, async _ =>
            {
                IsBusy = true;
                AuthenticationStatus = "started logging...";
                Debug.WriteLine(RestofitApiHelper.Address);
                var client = new HttpClient(NetCache.UserInitiated)
                {
                    BaseAddress = new Uri(RestofitApiHelper.Address)
                };
                var api              = RestService.For <IRestaurantApi>(client);
                var token            = await api.GetToken(Email, Password);
                AuthenticationStatus = "started authentication...";
                Context.AuthenticationManager.AuthenticatedClient = new HttpClient(new AuthenticatedHttpClientHandler(token.access_token))
                {
                    BaseAddress = new Uri(RestofitApiHelper.Address)
                };
                var info = await Context.AuthenticationManager.AuthenticatedApi.GetUserInfo();
                return(info);
            });


            Login.
            Subscribe(x => IsBusy = false);

            Login
            .ThrownExceptions
            .Subscribe(ex =>
            {
                UserError.Throw("Invalid login or password!");
                Debug.WriteLine("Error! - " + ex.Message);
                IsBusy = false;
            });


            #region OpenRegester
            OpenRegester = ReactiveCommand.Create();
            OpenRegester.Subscribe(x =>
            {
                var viewModel = Locator.Current.GetService <SignUpViewModel>();
                if (viewModel == null)
                {
                    var regViewModel = new SignUpViewModel(NavigationScreen);
                    Locator.CurrentMutable.RegisterConstant(regViewModel, typeof(SignUpViewModel));
                    NavigationScreen.Navigation.Navigate.Execute(regViewModel);
                }
                else
                {
                    NavigationScreen.Navigation.Navigate.Execute(viewModel);
                }
            });


            OpenLogin = ReactiveCommand.Create();
            OpenLogin.Subscribe(x =>
            {
                var authenViewModel = Locator.Current.GetService <AuthenticationViewModel>();
                NavigationScreen.Navigation.Navigate.Execute(authenViewModel);
            });
            #endregion
        }