示例#1
0
        private async void OnSubmit()
        {
            if (Connectivity.NetworkAccess != NetworkAccess.Internet)
            {
                await PageDialogService.DisplayAlertAsync("No Internet", "Unable to submit feedback without internet, try finding a paper copy", "OK");

                return;
            }

            if (Busy)
            {
                return;
            }


            var requiredQuestions = Questions.Where(x => x.IsRequired);

            if (requiredQuestions.Any(x => string.IsNullOrWhiteSpace(x.Answer)))
            {
                await PageDialogService.DisplayAlertAsync("Missing Fields", "Survey is incomplete, complete highlighted questions", "OK");

                foreach (var item in Questions.Where(x => x.IsRequired))
                {
                    item.TextColor = string.IsNullOrWhiteSpace(item.Answer) ?
                                     Color.Red : (Color)App.Current.Resources["DarkBlue"];
                }

                return;
            }

            Busy = true;

            bool anyErrors = false;

            foreach (var endpoint in _endpoints)
            {
                try
                {
                    var deviceId = await AppCenter.GetInstallIdAsync();

                    var model = new FeedbackPayload
                    {
                        Year          = 2019,
                        DeviceId      = deviceId.ToString(),
                        SurveyAnswers = JsonConvert.SerializeObject(Questions.Select(x => new { x.Question, x.Answer }))
                    };

                    var isSuccessful = await EndpointService.PostAsync(endpoint, model);

                    if (!isSuccessful)
                    {
                        anyErrors = true;
                    }
                }
                catch (Exception ex)
                {
                    anyErrors = true;
                }
            }

            if (anyErrors)
            {
                Busy = false;
                await PageDialogService.DisplayAlertAsync("Unable to Submit", "Find event staff for paper survey", "OK");

                return;
            }

            var messages = await CompleteMessageService.GetAsync();

            var complete = messages
                           .Select(x => new Complete
            {
                Message = x.Title,
                Summary = x.Message
            })
                           .FirstOrDefault();

            var parameters = new NavigationParameters
            {
                { Constants.Navigation.Parameters.GoBackToRoot, true },
                { Constants.Navigation.Parameters.Title, "Survey" },
                { Constants.Navigation.Parameters.Complete, complete }
            };
            await NavigationService.NavigateAsync(Constants.Navigation.CompletePage, parameters);
        }