Ejemplo n.º 1
0
        private async void OnSubmitBtnClicked(object sender, EventArgs args)
        {
            DisableUI(); // Disable UI
            var usernameText = UsernameEntry.Text;
            var passwordText = PasswordEntry.Text;
            var confirmText  = ConfirmEntry.Text;
            var language     = items[LanguagePicker.SelectedIndex];

            if (!string.IsNullOrEmpty(usernameText) &&
                !string.IsNullOrEmpty(passwordText) &&
                passwordText == confirmText &&
                !string.IsNullOrEmpty(language))
            {
                ConnectivityHandler conHandler = new ConnectivityHandler();
                bool isConnected = await conHandler.IsConnected();

                if (isConnected)
                {
                    var users = await AppContext.Repository.GetAsync <User>(x => x.Username == usernameText);

                    bool userExists = false;
                    if (users != null)
                    {
                        var oldUser = users.FirstOrDefault();
                        if (oldUser != null)
                        {
                            userExists = true;
                        }
                    }
                    if (userExists)
                    {
                        RegisterCallbackLabel.Text = "User named " + usernameText + " exists";
                        EnableUI(); // Enable UI
                    }
                    else
                    {
                        var newUser = new User();
                        newUser.Username       = usernameText;
                        newUser.Password       = passwordText;
                        newUser.CodingLanguage = language;
                        try
                        {
                            await AppContext.Repository.InsertAsync(newUser);

                            RegisterCallbackLabel.TextColor = Color.Green;
                            RegisterCallbackLabel.Text      = "User has been registered successfully";
                            EnableUI(); // Enable UI
                        }
                        catch (Exception ex)
                        {
                            RegisterCallbackLabel.Text = "User Insert Error: " + ex;
                            EnableUI(); // Enable UI
                        }
                    }
                }
                else
                {
                    RegisterCallbackLabel.Text = "No Internet Connection";
                    EnableUI(); // Enable UI
                }
            }
            else
            {
                RegisterCallbackLabel.Text = "Empty or Invalid Inputs";
                EnableUI(); // Enable UI
            }
        }
Ejemplo n.º 2
0
        private async void OnLoginBtnClicked(object sender, EventArgs args)
        {
            DisableUI(); // Disable UI
            var usernameText = UsernameEntry.Text;
            var passwordText = PasswordEntry.Text;

            if (!string.IsNullOrEmpty(usernameText) && !string.IsNullOrEmpty(passwordText))
            {
                ConnectivityHandler conHandler = new ConnectivityHandler();
                bool isConnected = await conHandler.IsConnected();

                if (isConnected)
                {
                    var users = await AppContext.Repository.GetAsync <User>(x => x.Username == usernameText);

                    if (users == null)
                    {
                        LoginCallbackLabel.Text = "Username doesn't exist";
                        EnableUI(); // Enable UI
                    }
                    else
                    {
                        var user = users.FirstOrDefault();
                        if (user == null)
                        {
                            LoginCallbackLabel.Text = "Username doesn't exist";
                            EnableUI(); // Enable UI
                        }
                        else
                        {
                            if (PasswordEntry.Text == user.Password)
                            {
                                LoginCallbackLabel.TextColor = Color.Green;
                                LoginCallbackLabel.Text      = "Map page loading..";
                                //Set stored data
                                Application.Current.Properties["loggedUser"] = JsonConvert.SerializeObject(user);
                                await Application.Current.SavePropertiesAsync();

                                //GOTO: MapPage
                                Navigation.InsertPageBefore(new MapPage(AppContext), this);
                                Navigation.RemovePage(this);
                                //After login
                                await AppContext.InitializeLoginAsync(user);
                            }
                            else
                            {
                                LoginCallbackLabel.Text = "Wrong password";
                                EnableUI(); // Enable UI
                            }
                        }
                    }
                }
                else
                {
                    LoginCallbackLabel.Text = "No Internet Connection";
                    EnableUI(); // Enable UI
                }
            }
            else
            {
                LoginCallbackLabel.Text = "Username and password must be filled";
                EnableUI(); // Enable UI
            }
        }