Exemplo n.º 1
0
        public RegisterPageViewModel()
        {
            ButtonText      = AppConfigurationModel.Instance.RegisterPageContent.ButtonText1;
            RegisterCommand = new Command(async() =>
            {
                switch (CurrentPage)
                {
                case 0:

                    if (!IsEmailValidationRunning)
                    {
                        if (ValidEmail)
                        {
                            var returnVal = ControlPageValues();

                            if (returnVal.Key == "Ok")
                            {
                                CurrentPage = 1;
                                UI.CallNextPage();
                                ButtonText = AppConfigurationModel.Instance.RegisterPageContent.ButtonText2;
                            }
                            else
                            {
                                UI.DisplayCustomAlert(returnVal);
                            }
                        }
                        else
                        {
                            UI.TriggerInvalidEmailAnimation();
                        }
                    }
                    else
                    {
                        await PopupNavigation.Instance.PushAsync(new ErrorPopup("Lütfen Bekleyiniz"));
                    }
                    break;

                case 1:

                    if (!IsUsernameValidationRunning)
                    {
                        if (ValidUsername)
                        {
                            var returnVal2 = ControlSecondPageValues();

                            if (returnVal2.Key == "Ok")
                            {
                                string errorLog = null;

                                Utility.ShowIndicator = true;

                                await Task.Run(() =>
                                {
                                    EllipticCurve elliptic = new EllipticCurve(true);

                                    var buffer = elliptic.KeyExchange(Username);

                                    if (elliptic.IsSucceeded)
                                    {
                                        Utility.SharedSecret = elliptic.SharedSecret;

                                        var serializedRegisterParameters = JsonConvert.SerializeObject(new
                                        {
                                            Email,
                                            Password,
                                            NameSurname,
                                            phoneNumber = PhoneNumber,
                                        });

                                        try
                                        {
                                            var response = JsonConvert.DeserializeObject <BaseResponseModel>(
                                                Helper.httpPostAsync($"{Utility.BaseURL}/api/user/register?username={Username}", serializedRegisterParameters, IsSecure: true).Result);

                                            if (response.responseVal == 0)
                                            {
                                                var serializedLoginParameters = JsonConvert.SerializeObject(new { password = Password });

                                                var loginResponse = JsonConvert.DeserializeObject <LoginResponseModel>(
                                                    Helper.httpPostAsync($"{Utility.BaseURL}/api/user/login?username={Username}", serializedLoginParameters, IsSecure: true).Result);

                                                if (loginResponse.responseVal == 0)
                                                {
                                                    Utility.ShowIndicator = false;
                                                    using (var con = DependencyService.Get <IDatabase>().GetConnection(ConnectionType.Login))
                                                    {
                                                        User bufferUser = new User()
                                                        {
                                                            accessToken      = loginResponse.accessToken,
                                                            LastLoggedInDate = DateTime.Now,
                                                            ConnectedHomes   = Helper.ConvertCollectionToString(loginResponse.ConnectedHomes, ','),
                                                            CurrentHome      = loginResponse.CurrentHome,
                                                            LoggedIn         = true,
                                                            Username         = Username,
                                                            UserType         = loginResponse.userType,
                                                            UserNameSurname  = loginResponse.nameSurname
                                                        };
                                                        con.Insert(bufferUser);
                                                        Utility.User = bufferUser;
                                                    }
                                                }
                                                else
                                                {
                                                    errorLog = loginResponse.responseText;
                                                }
                                            }
                                            else
                                            {
                                                errorLog = response.responseText;
                                            }
                                        }
                                        catch (Exception)
                                        {
                                            errorLog = Utility.UnexpectedMessage;
                                        }
                                    }
                                    else
                                    {
                                        errorLog = elliptic.Response.responseText;
                                    }
                                });

                                Utility.ShowIndicator = false;

                                if (errorLog != null)
                                {
                                    UI.DisplayCustomAlert(new KeyValuePair <string, string>(errorLog, null), true);
                                }
                                else
                                {
                                    RootPage.IsGestureEnabled = true;
                                    await RootPage.NavigateFromMenu((int)MenuItemType.Home);
                                }
                            }
                            else
                            {
                                UI.DisplayCustomAlert(returnVal2);
                            }
                        }
                        else
                        {
                            UI.TriggerInvalidUsernameAnimation();
                        }
                    }
                    else
                    {
                        await PopupNavigation.Instance.PushAsync(new ErrorPopup("Lütfen bekleyiniz."));
                    }
                    break;
                }
            });
        }