From 2427475b1b0efc3e35733255fd836ee19f933aaf Mon Sep 17 00:00:00 2001 From: Tomasz Kowalczyk Date: Sun, 14 Feb 2016 12:55:46 +0100 Subject: [PATCH] close #31 --- .../DevTalkMobile/Helpers/ExtensionMethods.cs | 20 +++++++++++ .../ModernHttpClient/FeedRepository.cs | 14 ++++---- .../ViewModels/PodcastPlayViewModel.cs | 34 ++++++++++++++++++- Application/DevTalkMobile/Views/HomeView.cs | 2 +- .../DevTalkMobile/Views/PodcastPlayView.cs | 5 ++- 5 files changed, 65 insertions(+), 10 deletions(-) diff --git a/Application/DevTalkMobile/Helpers/ExtensionMethods.cs b/Application/DevTalkMobile/Helpers/ExtensionMethods.cs index f349ea1..4ddd236 100644 --- a/Application/DevTalkMobile/Helpers/ExtensionMethods.cs +++ b/Application/DevTalkMobile/Helpers/ExtensionMethods.cs @@ -30,6 +30,26 @@ public static List GetImagesInHTMLString(string htmlString) { return images; } + public static string GetLinkToMp3File(this string input) + { + return (from link in GetLinksFromHTMLString(input) + where link.Contains(".mp3") + select link).FirstOrDefault(); + } + + public static List GetLinksFromHTMLString(string htmlString) { + List links = new List(); + string pattern = @"(?<=href="").*?(?="")"; + + Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase); + MatchCollection matches = rgx.Matches(htmlString); + + for(int i=0, l=matches.Count; i GetPartners(string htmlContent) { diff --git a/Application/DevTalkMobile/Services/ModernHttpClient/FeedRepository.cs b/Application/DevTalkMobile/Services/ModernHttpClient/FeedRepository.cs index fed497c..80ae163 100644 --- a/Application/DevTalkMobile/Services/ModernHttpClient/FeedRepository.cs +++ b/Application/DevTalkMobile/Services/ModernHttpClient/FeedRepository.cs @@ -23,7 +23,6 @@ public FeedRepository () public async Task> GetAll(string rss) { XNamespace content = "http://purl.org/rss/1.0/modules/content/"; -// XNamespace feedburner = "http://rssnamespace.org/feedburner/ext/1.0"; var httpClient = new HttpClient (new NativeMessageHandler ()); @@ -34,8 +33,6 @@ public async Task> GetAll(string rss) var xdoc = XDocument.Parse(responseString); var id = 0; return (from item in xdoc.Descendants("item") -// let enclosure = item.Element("enclosure") -// where enclosure != null select new FeedItem { Title = (string)item.Element("title"), @@ -44,7 +41,7 @@ public async Task> GetAll(string rss) FileImage = (string)item.Element(content.GetName("encoded")).Value.GetImageFile(), Link = (string)item.Element("link"), PublishDate = (string)item.Element("pubDate"), -// Mp3Url = (string)enclosure.Attribute("url"), + Mp3Url = (string)item.Element(content.GetName("encoded")).Value.GetLinkToMp3File(), BlogPost = (string)item.Element("link"), Id = id++ }).ToList(); @@ -53,6 +50,8 @@ public async Task> GetAll(string rss) public async Task> GetFilteredFeed(string rss, string filter) { + XNamespace content = "http://purl.org/rss/1.0/modules/content/"; + var httpClient = new HttpClient(new NativeMessageHandler()); var responseString = await httpClient.GetStringAsync(rss); @@ -62,15 +61,16 @@ public async Task> GetFilteredFeed(string rss, string filter) var xdoc = XDocument.Parse(responseString); var id = 0; return (from item in xdoc.Descendants("item") - let enclosure = item.Element("enclosure") - where enclosure != null select new FeedItem { Title = (string)item.Element("title"), Description = (string)item.Element("description"), + DescriptionLongHtml = (string)item.Element("description"), + FileImage = (string)item.Element(content.GetName("encoded")).Value.GetImageFile(), Link = (string)item.Element("link"), PublishDate = (string)item.Element("pubDate"), - Mp3Url = (string)enclosure.Attribute("url"), + Mp3Url = (string)item.Element(content.GetName("encoded")).Value.GetLinkToMp3File(), + BlogPost = (string)item.Element("link"), Id = id++ }).ToList().Where(item => item.Title.ToLower().Contains(filter.ToLower())).ToList(); }); diff --git a/Application/DevTalkMobile/ViewModels/PodcastPlayViewModel.cs b/Application/DevTalkMobile/ViewModels/PodcastPlayViewModel.cs index d3446e1..3cd2216 100644 --- a/Application/DevTalkMobile/ViewModels/PodcastPlayViewModel.cs +++ b/Application/DevTalkMobile/ViewModels/PodcastPlayViewModel.cs @@ -64,6 +64,27 @@ public string PodcastDuration get { return podcastDuration; } set { podcastDuration = value; OnPropertyChanged("PodcastDuration"); } } + + private bool isPlayEnabled = true; + public bool IsPlayEnabled + { + get { return isPlayEnabled; } + set { isPlayEnabled = value; OnPropertyChanged("IsPlayEnabled"); } + } + + private bool isStopEnabled = false; + public bool IsStopEnabled + { + get { return isStopEnabled; } + set { isStopEnabled = value; OnPropertyChanged("IsStopEnabled"); } + } + + private bool isPauseEnabled = false; + public bool IsPauseEnabled + { + get { return isPauseEnabled; } + set { isPauseEnabled = value; OnPropertyChanged("IsPauseEnabled"); } + } #endregion #region Commands @@ -103,18 +124,29 @@ public Command GetSelectedItemInfoCommand #region Private Methods private async Task ExecutePlayCommand(string pathToFile) { - if(!string.IsNullOrWhiteSpace(pathToFile)) + if (!string.IsNullOrWhiteSpace (pathToFile)) + { await _soundService.Play (pathToFile); + IsPlayEnabled = false; + IsStopEnabled = true; + IsPauseEnabled = true; + } } private void ExecutePauseCommand() { _soundService.Pause (); + IsPlayEnabled = true; + IsStopEnabled = false; + IsPauseEnabled = false; } private void ExecuteStopCommand() { _soundService.Stop (); + IsPlayEnabled = true; + IsStopEnabled = false; + IsPauseEnabled = false; } private void ExecuteGetSelectedItemInfoCommand(FeedItem item) diff --git a/Application/DevTalkMobile/Views/HomeView.cs b/Application/DevTalkMobile/Views/HomeView.cs index 463197e..cc46430 100644 --- a/Application/DevTalkMobile/Views/HomeView.cs +++ b/Application/DevTalkMobile/Views/HomeView.cs @@ -16,7 +16,7 @@ public HomeView () this.Title = "DevTalk"; var buttonPlay = new Button () { - Text = "Play", + Text = "Show", BackgroundColor = Color.FromHex ("#FF6600"), WidthRequest = 150 }; diff --git a/Application/DevTalkMobile/Views/PodcastPlayView.cs b/Application/DevTalkMobile/Views/PodcastPlayView.cs index 7b0f205..1168439 100644 --- a/Application/DevTalkMobile/Views/PodcastPlayView.cs +++ b/Application/DevTalkMobile/Views/PodcastPlayView.cs @@ -57,13 +57,14 @@ public PodcastPlayView (FeedItem selectedFeedItem) Button playButton = new Button () { Text = "Play", - Image = "ic_play.png", + Image = "ic_play.png" }; playButton.Clicked += (sender, e) => { ViewModel.PlayCommand.Execute(_selectedFeedItem.Mp3Url); }; + playButton.SetBinding (Button.IsEnabledProperty, "IsPlayEnabled"); Button stopButton = new Button () { @@ -75,6 +76,7 @@ public PodcastPlayView (FeedItem selectedFeedItem) { ViewModel.StopCommand.Execute(null); }; + stopButton.SetBinding (Button.IsEnabledProperty, "IsStopEnabled"); Button pauseButton = new Button () { @@ -86,6 +88,7 @@ public PodcastPlayView (FeedItem selectedFeedItem) { ViewModel.PauseCommand.Execute(null); }; + pauseButton.SetBinding (Button.IsEnabledProperty, "IsPauseEnabled"); #endregion WebView webPage = new WebView ()