/// <summary>
        /// Saves or Adds a DiaryEntry to the DB.
        /// </summary>
        /// <returns></returns>
        private async Task OnSaveEntryAsync()
        {
            if (SelectedEntry != null && SelectedEntry.CanSave)
            {
                if (SelectedEntry.IsNew)
                {
                    var selEntry = SelectedEntry.Model;
                    await diaryService.AddEntryAsync(selEntry);

                    await deEntries.RefreshAsync();

                    SelectedEntry = Entries.FirstOrDefault(e => e.Model.Id == selEntry.Id);
                }
                else
                {
                    await diaryService.UpdateEntryAsync(SelectedEntry.Model);
                }

                // Saved/updated - turn off save flag.
                if (SelectedEntry != null)
                {
                    SelectedEntry.CanSave = false;
                }
            }
        }
        /// <summary>
        /// Creates a new DiaryEntry in the UI. Note that this does _not_ save
        /// the entry to the DB; that is done by the SaveCommand.
        /// </summary>
        /// <returns></returns>
        private async Task OnAddEntryAsync()
        {
            DiaryEntryViewModel vm = new DiaryEntryViewModel();

            SelectedEntry = vm;
            await serviceLocator.Get <INavigationService>().NavigateAsync(AppPage.Detail);
        }
        // Lab3: Add logout implementation
        /// <summary>
        /// This method is called to logoff and force a re-authentication.
        /// </summary>
        /// <returns></returns>
        private async Task OnClearAuthAsync()
        {
            await diaryService.LogoffAsync();

            await deEntries.RefreshAsync();

            SelectedEntry = deEntries.FirstOrDefault();
        }
        /// <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());
        }
        /// <summary>
        /// Deletes a DiaryEntry from the database and removes it from our collection.
        /// </summary>
        /// <param name="de"></param>
        /// <returns></returns>
        private async Task OnDeleteEntryAsync(object input)
        {
            // Can be invoked either through ActionBar button or ContextAction.
            var de = input as DiaryEntryViewModel;

            if (de == null)
            {
                de = SelectedEntry;
            }
            if (de != null)
            {
                await diaryService.DeleteEntryAsync(SelectedEntry.Model);

                Entries.Remove(de);
                if (SelectedEntry == de)
                {
                    SelectedEntry = Entries.FirstOrDefault();
                }
            }
        }