public void ExecuteDownloadCommand(RssEpisode episode)
        {
            IsBusy = true;

            try
            {
                if (episode != null && episode.Id != null)
                {
                    if (DownloadService.CanDownloadPodcast(episode))
                    {
                        DownloadService.Instance.DownloadPodcast(episode); //fires message center messages
                    }
                    else
                    {
                        Debug.WriteLine("RssEpisodeListViewModel.ExecuteDownloadCommand CanDownload was false ");
                    }
                }
                else
                {
                    Debug.WriteLine("RssEpisodeListViewModel.ExecuteDownloadCommand episode or episode id was null ");
                }
            }
            catch (Exception ex)
            {
                //could not delete item
                Debug.WriteLine("RssEpisodeListViewModel.ExecuteDownloadCommand Could not Download Podcast File " + ex.Message);
            }
            finally
            {
                IsBusy = false;
            }
        }
Example #2
0
        private async Task DownloadPodcastFileAsync(RssEpisode episode)
        {
            try
            {
                episode.PlayPauseDownloadIcon = IconFont.Clock;
                //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.DownloadPodcastFile Could not Update episode");
                }
                //download the file to storage
                var filePath = FileHelper.GetPodcastPath(episode.PodcastFileName);
                var message  = new DownloadMessage
                {
                    Id        = episode.Id.Value, //needed to update RssEpisode when done
                    Url       = episode.EnclosureLink,
                    FilePath  = filePath,
                    QueueType = QueueType.PodcastFile
                };
                MessagingCenter.Send(message, "Download"); //goes to Android project Service, returns thru DownloadFinishedMessage
            }
            catch (Exception ex)
            {
                Debug.WriteLine("DownloadService.DownloadPodcastFile Error " + ex.Message);
            }
        }
Example #3
0
        //************************************** Static Utility Functions ********************************************
        public static bool CanDownloadPodcast(RssEpisode episode)
        {
            var result = false;

            try
            {
                if (episode.Id != null)
                {
                    if (episode.IsDownloaded != IsDownloadedEnum.Downloaded)
                    {
                        var filePath   = FileHelper.GetPodcastPath(episode.PodcastFileName);
                        var fileExists = File.Exists(filePath);
                        if (!fileExists)
                        {
                            result = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("DownloadService.CanDownloadPodcast failed " + ex.Message);
            }
            return(result);
        }
Example #4
0
        public async Task <int> SaveEpisodeItemAsync(RssEpisode item, bool doPublish = false)
        {
            if (episodes == null)
            {
                episodes = new List <RssEpisode>();
            }
            var result = 0;

            if (item.Id != null && item.Id != 0)
            {
                var oldItem = episodes.Where((RssEpisode arg) => arg.Id == item.Id).FirstOrDefault();
                episodes.Remove(oldItem);
                episodes.Add(item);
                result = await Database.UpdateAsync(item);
            }
            else
            {
                var maxId = await GetMaxRssEpisodeId();

                item.Id = maxId + 1;
                episodes.Add(item);
                result = await Database.InsertAsync(item);
            }
            if (result > 0 && doPublish)
            {
                var updateEpisodeMessage = new UpdateEpisodeMessage
                {
                    RssEpisode = item
                };
                MessagingCenter.Send(updateEpisodeMessage, "UpdateEpisodeMessage"); //goes to listening ViewModels that can download
            }
            return(result);
        }
Example #5
0
        private QueueItem FetchQueueItem(RssEpisode episode, QueueType queueType)
        {
            //var foundItem = _downloadQueue.Select(x => (x.QueueDataObject == episode) && (x.ItemType == queueType)).First();
            QueueItem foundItem = null;

            if (_downloadQueue.Count > 0)
            {
                var foundItems = _downloadQueue.Where(x => (((RssEpisode)x.QueueDataObject).Id == episode.Id) && (x.ItemType == queueType)).ToList();
                if (foundItems.Count() > 0)
                {
                    foundItem = foundItems[0];
                }
            }
            return(foundItem);
        }
Example #6
0
        public static List <RssEpisode> CreateRssEpisodeForFeedItem(int feedItemId)
        {
            var episodeList = new List <RssEpisode>();

            for (var i = 0; i < 10; i++)
            {
                var episode = new RssEpisode();
                episode.Title        = "Episode " + i.ToString();
                episode.IsDownloaded = IsDownloadedEnum.NotDownloaded;
                episode.Author       = "Isaac Asimov";
                episode.Description  = "Description " + i.ToString();
                episode.FeedItemId   = feedItemId;
                episodeList.Add(episode);
            }
            return(episodeList);
        }
Example #7
0
        //************************************** Download Functions ******************************************
        public async Task <bool> DownloadPodcast(RssEpisode episode)
        {   //no need to be async, check some things then fire MessageCenter to background task
            var result = false;

            try
            {
                if (CanDownloadPodcast(episode))
                {
                    var foundItem = FetchQueueItem(episode, QueueType.PodcastFile);
                    if (foundItem == null)
                    {
                        episode.PlayPauseDownloadIcon = IconFont.CircleArrowHistory;
                        //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.DownloadPodcastAsync Could not Update episode");
                        }
                        //queue download
                        var queueItem = new QueueItem();
                        queueItem.ID              = _downloadQueue.Count;
                        queueItem.ItemType        = QueueType.PodcastFile;
                        queueItem.QueueDataObject = episode;
                        _downloadQueue.Enqueue(queueItem);
                        //start downloader
                        await StartDownloadAsync();

                        result = true;
                    }
                    else
                    {
                        Debug.WriteLine("DownloadService.DownloadPodcast episode was found in the _downloadQueue already");
                    }
                }
                else
                {
                    Debug.WriteLine("DownloadService.DownloadPodcast CanDownload(episode) was false");
                }
            }
            catch (Exception ex)
            {
                //could not delete item
                Debug.WriteLine("DownloadService.DownloadPodcast Could not Download Podcast File " + ex.Message);
            }
            return(result);
        }
        private void UpdateEpisodeInItemsList(RssEpisode newEpisode)
        {
            RssEpisode foundEpisode = null;
            int        index        = 0;

            foreach (var e in Items)
            {   //find the episode that finished
                if (e.Id == newEpisode.Id)
                {
                    foundEpisode = e;
                    break;
                }
                index++;
            }
            if (foundEpisode != null) //update the one episode
            {
                Items[index] = newEpisode;
            }
        }
Example #9
0
        public static bool CanDownloadImage(RssEpisode episode)
        {
            var result = false;

            try
            {
                var filePath   = FileHelper.GetImagePath(episode.ImageFileName);
                var fileExists = File.Exists(filePath);
                if (!fileExists)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("DownloadService.CanDownloadImage failed " + ex.Message);
            }
            return(result);
        }
        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;
            }
        }
Example #11
0
 //this is not setup correctly yet
 private void DownloadFeedFile(RssEpisode episode)
 {
     try
     {
         //download the file to storage
         var filePath = FileHelper.GetImagePath(episode.ImageLink);
         var message  = new DownloadMessage
         {
             Id        = episode.Id.Value, //needed to update RssEpisode when done
             Url       = episode.ImageLink,
             FilePath  = filePath,
             QueueType = QueueType.RssFeed
         };
         MessagingCenter.Send(message, "Download"); //goes to Android project Service, returns thru DownloadFinishedMessage
     }
     catch (Exception ex)
     {
         Debug.WriteLine("DownloadService.DownloadFeedFile Error " + ex.Message);
     }
 }
Example #12
0
        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;
            }
        }
Example #13
0
        public static bool CanDownloadFeed(RssEpisode episode)
        {
            var result = false;

            /*try
             * {
             *  if (episode.Id != null)
             *  {
             *      var filePath = FileHelper.GetImagePath(episode.ImageFileName);
             *      var fileExists = File.Exists(filePath);
             *      if (!fileExists)
             *          result = true;
             *  }
             * }
             * catch (Exception ex)
             * {
             *  Debug.WriteLine("DownloadService.CanDownloadFeed failed " + ex.Message);
             * }*/
            result = true;
            return(result);
        }
Example #14
0
        public async Task <bool> DownloadFeed(RssEpisode episode)
        {   //no need to be async, check some things then fire MessageCenter to background task
            var result = false;

            try
            {
                if (CanDownloadFeed(episode))
                {
                    if (!string.IsNullOrEmpty(episode.ImageFileName))
                    {
                        //queue download
                        var queueItem = new QueueItem();
                        queueItem.ID              = _downloadQueue.Count;
                        queueItem.ItemType        = QueueType.RssFeed;
                        queueItem.QueueDataObject = episode;
                        _downloadQueue.Enqueue(queueItem);
                        //start downloader
                        await StartDownloadAsync();

                        result = true;
                    }
                    else
                    {
                        Debug.WriteLine("DownloadService.DownloadFeed episode == Downloaded");
                    }
                }
                else
                {
                    Debug.WriteLine("DownloadService.DownloadFeed CanDownload(episode) was false");
                }
            }
            catch (Exception ex)
            {
                //could not download item
                Debug.WriteLine("DownloadService.DownloadFeed Could not Download Podcast File " + ex.Message);
            }
            return(result);
        }
Example #15
0
 private void UpdateEpisodeItem(RssEpisode newEpisode)
 {
     EpisodeItem = newEpisode;
 }
Example #16
0
        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;
                    }
                });
            });
        }