Ejemplo n.º 1
0
        /// <summary>
        /// A recursive method to generate a new username based on the first and last names.
        /// </summary>
        /// <param name="firstName">The user's first name.</param>
        /// <param name="lastName">The user's last name.</param>
        /// <param name="userNumber">The number appended to the end of the user's username.</param>
        /// <param name="source">A cancellation token source to generate CancellationToken objects as needed.</param>
        /// <returns>An awaitable task who's result is the user's username as a string.</returns>
        private async Task <string> GenerateUsername(string firstName, string lastName, int userNumber)
        {
            // First we get their first three of the first and last names (or how many of the letters we can fit if there are less than three).
            string userName;

            if (lastName.Length < 3)
            {
                userName = lastName.ToUpper();
            }
            else
            {
                userName = lastName.Substring(0, 3).ToUpper();
            }
            if (firstName.Length < 3)
            {
                userName += firstName.ToUpper();
            }
            else
            {
                userName += firstName.Substring(0, 3).ToUpper();
            }
            userName += "_" + userNumber.ToString("00");    // The format will always be 01, 02, 03, etc.
            // Try to get the user from the database via an API GET call.
            try
            {
                await client.GetUser(userName);                                      // Call the API.

                return(await GenerateUsername(firstName, lastName, userNumber + 1)); // If we got someone back, call this function again but with a different user number.
            }
            catch (ValidationApiException)
            {
                return(userName);    // We got an error? That's actually a good thing. Show them the money!
            }
            catch (ApiException)
            {
                return(userName);    // Same here. This is the one that will more than likely be the reason we return.
            }
        }
        /// <summary>
        /// This is called when the submit button is pressed.
        /// </summary>
        /// <param name="sender">The object that causes the event.</param>
        /// <param name="e">Event arguments.</param>
        private async void Submit_Clicked(object sender, EventArgs e)
        {
            // First, ensure that the user either logged in or they are submitting the request anonymously.
            if (!chkAnonymous.IsChecked && !Application.Current.Properties.ContainsKey("Username"))
            {
                UserDialogs.Instance.Toast(new ToastConfig("You are not logged in! Please log in or check anonymous request and try again.")
                {
                    BackgroundColor = App.toastColor
                });
            }
            // Now, make sure that there is text in the prayer request text area.
            else if (txtDescription.Text == null || txtDescription.Text.Equals(string.Empty))
            {
                UserDialogs.Instance.Toast(new ToastConfig("Prayer request is empty!")
                {
                    BackgroundColor = App.toastColor
                });
            }
            // If they passed those simple validations, call here.
            else
            {
                try
                {
                    // Let the user know we are creating the prayer request.
                    UserDialogs.Instance.Toast(new ToastConfig("Creating prayer request...")
                    {
                        BackgroundColor = App.toastColor, Duration = TimeSpan.FromMilliseconds(App.timeoutTime)
                    });
                    // Build the actual prayer request model using the information provided.
                    Models.PrayerRequest prayerRequest = new Models.PrayerRequest()
                    {
                        PrayerDate        = DateTime.Now,
                        PrayerDescription = txtDescription.Text,
                        UserId            = (chkAnonymous.IsChecked ? -1 : (await client.GetUser(Application.Current.Properties["Username"].ToString())).UserId) // If anonymous is selected, use the -1 user ID (the special anonymous user).
                    };
                    await client.CreatePrayerRequest(prayerRequest);                                                                                             // Actually post the new prayer request.

                    // Let the user know the request was created.
                    UserDialogs.Instance.Toast(new ToastConfig("Prayer request posted!")
                    {
                        BackgroundColor = App.toastColor
                    });
                    // Reset the form.
                    txtDescription.Text    = "";
                    chkAnonymous.IsChecked = false;
                }
                catch (Refit.ValidationApiException)    // Bad input.
                {
                    UserDialogs.Instance.Toast(new ToastConfig("Database rejected the post! Try again later.")
                    {
                        BackgroundColor = App.toastColor
                    });
                }
                catch (Refit.ApiException)              // User was deleted.
                {
                    UserDialogs.Instance.Toast(new ToastConfig("Database rejected the post! Try again later.")
                    {
                        BackgroundColor = App.toastColor
                    });
                }
                catch (System.Net.Http.HttpRequestException)    // Lost connection.
                {
                    UserDialogs.Instance.Toast(new ToastConfig("Connection blocked! Try again later.")
                    {
                        BackgroundColor = App.toastColor
                    });
                }
                catch (TaskCanceledException)           // Request timed out.
                {
                    UserDialogs.Instance.Toast(new ToastConfig("Request timed out... Try again later.")
                    {
                        BackgroundColor = App.toastColor
                    });
                }
            }
        }