private async Task PodcastFileCompletedAsync(DownloadFinishedMessage message) { try { //refetch rssEpisode in case it's different from the currently viewed one var episode = await DataStore.GetEpisodeItemByIdAsync(message.Id); if (episode != null) { episode.IsPlaying = IsPlayingEnum.NotStarted; episode.IsDownloaded = IsDownloadedEnum.Downloaded; episode.PlayPauseDownloadIcon = IconFont.PlayArrow; RssEpisodeManager.UpdateRssEpisodeWithFileInfo(ref episode); //save download status to the database var resultSave = await DataStore.SaveEpisodeItemAsync(episode, true); //returns the number of items changed if (resultSave != 1) { Debug.WriteLine("DownloadService.PodcastFileCompletedAsync Could not Update episode"); } } else { Debug.WriteLine("DownloadService.PodcastFileCompletedAsync episode was null from GetEpisodeItemByIdAsync"); } _downloadStatus = DownloadStatus.NotStarted; await StartDownloadAsync(); //start next download } catch (Exception ex) { Debug.WriteLine("DownloadService.PodcastFileCompletedAsync Error " + ex.Message); } }
public RssEpisodeListViewModel(FeedItem item = null) { Title = item?.Text; FeedItem = item; ListItemSize = 10; Items = new ObservableCollection <RssEpisode>(); LoadItemsCommand = new Command(async() => await ExecuteLoadInitialItemsCommand()); RefreshCommand = new Command(async() => await ExecuteRefreshCommand()); DeleteCommand = new Command(async() => await ExecuteDeleteCommand()); MessagingCenter.Subscribe <UpdateEpisodeMessage>(this, "UpdateEpisodeMessage", message => { Device.BeginInvokeOnMainThread(() => { //receive the result from DownloadService //check that this ViewModel is the correct one for the downloaded podcast if (message.RssEpisode.FeedItemId == FeedItem.Id) { //check the Feed Id first UpdateEpisodeInItemsList(message.RssEpisode); } }); }); MessagingCenter.Subscribe <UpdateEpisodeMessage>(this, "PlaybackEnded", message => { Device.BeginInvokeOnMainThread(async() => { //receive the result from DownloadService if (message.RssEpisode.FeedItemId == FeedItem.Id) { //check that this ViewModel is the correct one for the downloaded podcast await RssEpisodeManager.FinishedEpisodeAsync(message.RssEpisode); UpdateEpisodeInItemsList(message.RssEpisode); } }); }); }
public async Task ExecuteDeleteCommand() { IsBusy = true; try { EpisodeItem = await RssEpisodeManager.DeletePodcastFile(EpisodeItem, true); } catch (Exception ex) { //could not delete item Debug.WriteLine("Could not Delete Podcast File " + ex.Message); } finally { IsBusy = false; } }
public async Task ExecutePlayCommandAsync(RssEpisode episode) { IsBusy = true; try { var updatedEpisode = await RssEpisodeManager.PlayEpisodeAsync(episode); UpdateEpisodeInItemsList(updatedEpisode); //do this at the end to avoid CollectionView refreshing } catch (Exception ex) { //could not delete item Debug.WriteLine("RssEpisodeListViewModel.ExecutePlayCommandAsync Could not Play Podcast File " + ex.Message); } finally { IsBusy = false; } }
public async Task ExecutePauseCommandAsync(RssEpisode episode) { IsBusy = true; try { var updatedEpisode = await RssEpisodeManager.PauseEpisodeAsync(episode); UpdateEpisodeItem(updatedEpisode); //do this at the end to avoid CollectionView refreshing SliderMaximum = (int)PodcastPlayer.Instance.Duration; } catch (Exception ex) { //could not delete item Debug.WriteLine("RssEpisodeDetailViewModel.ExecutePauseCommandAsync Could not Pause Podcast File " + ex.Message); } finally { IsBusy = false; } }
public RssEpisodeDetailViewModel(FeedItem feedItem = null, RssEpisode episodeItem = null) { Title = feedItem?.Text; FeedItem = feedItem; EpisodeItem = episodeItem; DownloadCommand = new Command(async() => await ExecuteDeleteCommand()); //Page actually uses the _Clicks DeleteCommand = new Command(async() => await ExecuteDeleteCommand()); //if player is already playing if (PodcastPlayer.Instance.IsPlaying) { PlayPauseIcon = IconFont.Pause; if ((int)EpisodeItem.Duration > 0) { SliderMaximum = (int)PodcastPlayer.Instance.Duration; } Device.StartTimer(TimeSpan.FromSeconds(0.7), UpdatePosition); } else { if (EpisodeItem.IsDownloaded == IsDownloadedEnum.Downloaded && !string.IsNullOrEmpty(EpisodeItem.EnclosureLink)) { PodcastPlayer.Instance.Episode = EpisodeItem; if ((int)EpisodeItem.Duration > 0) { SliderMaximum = (int)EpisodeItem.Duration; } } } MessagingCenter.Subscribe <UpdateEpisodeMessage>(this, "UpdateEpisodeMessage", message => { Device.BeginInvokeOnMainThread(() => { //receive the result from DownloadService if (EpisodeItem.Id == message.RssEpisode.Id) { //check that this ViewModel is the correct one for the downloaded podcast DownloadButtonEnabled = true; EpisodeItem = message.RssEpisode; } }); }); MessagingCenter.Subscribe <UpdateEpisodeMessage>(this, "StartEpisodePlaying", message => { Device.BeginInvokeOnMainThread(() => { //receive the result from DownloadService if (EpisodeItem.Id == message.RssEpisode.Id) { //check that this ViewModel is the correct one for the downloaded podcast PlayPauseState = true; PlayPauseIcon = IconFont.Pause; EpisodeItem = message.RssEpisode; } }); }); MessagingCenter.Subscribe <UpdateEpisodeMessage>(this, "StopEpisodePlaying", message => { Device.BeginInvokeOnMainThread(() => { //receive the result from DownloadService if (EpisodeItem.Id == message.RssEpisode.Id) { //check that this ViewModel is the correct one for the downloaded podcast PlayPauseState = false; PlayPauseIcon = IconFont.PlayArrow; EpisodeItem = message.RssEpisode; } }); }); MessagingCenter.Subscribe <UpdateEpisodeMessage>(this, "PlaybackEnded", message => { Device.BeginInvokeOnMainThread(async() => { //receive the result from DownloadService if (EpisodeItem.Id == message.RssEpisode.Id) { //check that this ViewModel is the correct one for the downloaded podcast await RssEpisodeManager.FinishedEpisodeAsync(message.RssEpisode); PlayPauseState = false; PlayPauseIcon = IconFont.PlayArrow; EpisodeItem = message.RssEpisode; } }); }); }
public async Task ExecuteNewFeedItemCommand() { IsBusy = true; try { var doc = await RssFeedHelper.GetXDocFromLinkAsync(Link); if (doc != null) { var newFeedItem = FeedItemManager.GetFeedItemFromXDoc(doc, Link); if (newFeedItem != null) { var successFeedImage = await FileHelper.DownloadImageFile(newFeedItem.ImageLink); if (!successFeedImage) { Debug.WriteLine("Could not download feed image file " + newFeedItem.ImageLink); } var result = await DataStore.SaveFeedItemAsync(newFeedItem); if (newFeedItem.Id != null) { //defensive programming, delete any existing records for this new FeedItem.Id //this is because Ids get reused and not all the episodes in and Id might get deleted try { result = await DataStore.DeleteAllEpisodesByFeedIdAsync(newFeedItem.Id); //don't care about the result } catch (Exception ex) { Debug.WriteLine("Info Error Deleting Episodes before adding new Feed Item " + ex.Message); } var episodeList = RssEpisodeManager.GetRssEpisodesFromXDoc(doc, newFeedItem.Id.Value); //get the episodes result = await DataStore.SaveEpisodeListAsync(episodeList); //save the episodes //get the images var count = 0; var lastImageLink = ""; foreach (var episode in episodeList) { if (lastImageLink != episode.ImageLink) { //don't try to download when the image is the same from episode to episode lastImageLink = episode.ImageLink; await DownloadService.Instance.DownloadImageAsync(episode); } count++; //if (count > 20) break; //only get 20, not like 1000 } MessagingCenter.Send(newFeedItem, "AddFeedItem"); } else { //newFeedItem.Id was null Debug.WriteLine("newFeedItem.Id was null"); } } else { //newFeedItem was null Debug.WriteLine("newFeedItem was null"); } } else { //problem getting feed await Application.Current.MainPage.DisplayAlert("Alert", "Could not get RSS Feed from Link", "OK"); } } catch (Exception ex) { //could not delete item Debug.WriteLine("Could not Add Podcast Feed Item " + ex.Message); } finally { IsBusy = false; } }