示例#1
0
        /// <summary>
        /// Constructor for the Main view model.
        /// </summary>
        /// <param name="serviceLocator"></param>
        public MainViewModel(IDependencyService serviceLocator)
        {
            this.serviceLocator = serviceLocator;
            diaryService        = serviceLocator.Get <IDiaryService>();

            AddEntry    = new AsyncDelegateCommand(OnAddEntryAsync);
            SaveEntry   = new AsyncDelegateCommand(OnSaveEntryAsync, () => SelectedEntry == null ? false : SelectedEntry.CanSave);
            DeleteEntry = new AsyncDelegateCommand(OnDeleteEntryAsync, de => de != null || (SelectedEntry != null && !SelectedEntry.IsNew));
            Refresh     = new AsyncDelegateCommand(() => deEntries.RefreshAsync());
            SelectEntry = new AsyncDelegateCommand(() => serviceLocator.Get <INavigationService>().NavigateAsync(AppPage.Detail));

            // Lab3: Add logout command
            Logout = new AsyncDelegateCommand(OnClearAuthAsync);

            deEntries = new RefreshingCollection <DiaryEntryViewModel>(LoadDiaryEntriesAsync)
            {
                BeforeRefresh = c =>
                {
                    IsBusy = true;
                    return(SelectedEntry);
                },

                AfterRefresh = (c, o) =>
                {
                    IsBusy        = false;
                    SelectedEntry = (DiaryEntryViewModel)o;
                },

                RefreshFailed = (c, ex) =>
                {
                    IsBusy = false;

                    return(serviceLocator.Get <IMessageVisualizerService>().ShowMessage(
                               "Are you connected?", ex.Flatten(), "OK"));
                }
            };

            // Set the 1st entry as active.
            deEntries.RefreshAsync()
            .ContinueWith(tr =>
            {
                SelectedEntry = deEntries.FirstOrDefault();
            },
                          CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext());
        }
示例#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;
                }
            });
        }