예제 #1
0
        private async Task GetSelectedAnswerAsync()
        {
            var selectedQuestion = GetSelectedQuestion();

            response = !string.IsNullOrEmpty(userName) && selectedQuestion != null
                ? await service.GetResponseForSurveyAsync(selectedQuestion.Id, userName)
                : null;

            SetSelectedAnswer();
        }
예제 #2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="surveyService">Service to retrieve data from</param>
        /// <param name="dependencyService">Service Locator</param>
        public MainViewModel(ISurveyQuestionService surveyService, IDependencyService dependencyService)
        {
            this.surveyService     = surveyService;
            this.dependencyService = dependencyService;

            // Setup our collection of questions. This is an async collection populated by our
            // survey service. The single constructor parameter is an async function to load the data.
            questions = new RefreshingCollection <SurveyQuestion>(surveyService.GetQuestionsAsync)
            {
                // What to do if the refresh fails..
                RefreshFailed = async(s, ex) =>
                {
                    IsLoadingData = false;
                    await Task.Delay(1000);  // get out of the binding.

                    await dependencyService.Get <IMessageVisualizerService>()
                    .ShowMessage("Error", "Failed to get questions: " + ex.Message, "OK");
                },

                // Called before a refresh occurs to save off the current selection.
                BeforeRefresh = s =>
                {
                    IsLoadingData = true;
                    return(SelectedQuestion);
                },

                // Called after a refresh completes successfully; restores the selection.
                AfterRefresh = (s, sq) =>
                {
                    IsLoadingData = false;
                    if (sq == null)
                    {
                        sq = questions.FirstOrDefault();
                    }
                    if (sq != null)
                    {
                        var newSelection = questions.SingleOrDefault(q => q.Id == ((SurveyQuestion)sq).Id);
                        SelectedQuestion = newSelection;
                    }
                }
            };

            // Setup the collection of answers; this is refreshed each time the
            // selected question changes.
            answers = new RefreshingCollection <AnswerViewModel>(async() =>
            {
                IsLoadingData = true;

                try
                {
                    var id            = SelectedQuestion.Id;
                    string [] choices = SelectedQuestion.Answers.Split('|');

                    if (HasName)
                    {
                        response = await surveyService.GetResponseForSurveyAsync(id, Name);
                    }
                    else
                    {
                        response = null;
                    }

                    int answerIndex = response == null ? -1 : response.ResponseIndex;

                    return(Enumerable.Range(0, choices.Length)
                           .Select(i => new AnswerViewModel(choices [i], i)
                    {
                        IsSelected = answerIndex == i
                    }));
                }
                catch (Exception ex)
                {
                    await dependencyService.Get <IMessageVisualizerService>()
                    .ShowMessage("Error", "Failed to get answers/responses: " + ex.Message, "OK");
                    return(null);
                }
                finally
                {
                    IsLoadingData = false;
                }
            });
        }