public void GetArticles(Channel c)
        {
            if (c.LastUpdate.HasValue)
            {
                DateTime last = c.LastUpdate.Value;
                DateTime now = DateTime.Now;
                if (now.Minute == last.Minute && (now - last).TotalMinutes < 5) return;
            }

            c.LastUpdate = Convert.ToDateTime(MinDBTime);
            var client = new WebClient();

            client.OpenReadCompleted += (sender, e) =>
                {
                    if (e.Error != null||e.Result == null)
                    {
                        c.LastUpdate = Convert.ToDateTime(MinDBUpdated);
                        return;
                    }

                    try
                    {
                        Read(GetEncodedString(e.Result), c, _model);
                        _model.SubmitChanges();
                        _model.RefreshArticles();
                    }
                    catch (Exception ex)
                    {
                        ToastPrompt toast = new ToastPrompt();
                        toast.Foreground = App.WhiteColor;
                        toast.MillisecondsUntilHidden = 1500;
                        toast.Message = AppResources.add_message_error;
                        toast.Show();
                        c.LastUpdate = Convert.ToDateTime(MinDBUpdated);
                        return;
                    }
                };
            try
            {
                client.OpenReadAsync(new Uri(c.URL, UriKind.Absolute));
            }
            catch
            {
                c.LastUpdate = Convert.ToDateTime(MinDBUpdated);
            }
        }
        public void GetArticles(Channel c)
        {
            if (c.LastUpdate.HasValue)
            {
                DateTime last = c.LastUpdate.Value;
                DateTime now = DateTime.Now;
                if (now.Minute == last.Minute && (now - last).TotalMinutes < 5) return;
            }

            c.LastUpdate = Convert.ToDateTime("1900/01/01");
            var client = new WebClient();

            client.OpenReadCompleted += (sender, e) =>
                {
                    if (e.Error != null)
                    {
                        c.LastUpdate = Convert.ToDateTime("1901/01/01");
                        return;
                    }

                    var str = e.Result;
                    try
                    {
                        Read(str, c, _model);
                        _model.SubmitChanges();
                        _model.RefreshArticles();
                    }
                    catch (Exception)
                    {
                        return;
                    }
                    finally
                    {
                        str.Close();
                    }
                };
            try
            {
                client.OpenReadAsync(new Uri(c.URL, UriKind.Absolute));
            }
            catch
            {
                App.ViewModel.DeleteChannel(c);
            }
        }
        private void Read(Stream stream, Channel c, RssViewModel m)
        {
            var feedFormatter = new Atom10FeedFormatter();
            var rssFormater = new Rss20FeedFormatter();

            XmlReader atomReader = XmlReader.Create(stream);
            SyndicationFeedFormatter f = null;

            if (feedFormatter.CanRead(atomReader))
            {
                feedFormatter.ReadFrom(atomReader);
                atomReader.Close();
                f = feedFormatter;
            }
            else
                if (rssFormater.CanRead(atomReader))
                {
                    rssFormater.ReadFrom(atomReader);
                    atomReader.Close();
                    f = rssFormater;
                }

            if (f == null) return;

            SyndicationFeed feed = f.Feed;
            c.Title = feed.Title.Text;

            Article[] articles = feed.Items.Select(item => new Article
                {
                    PubDate = item.PublishDate.DateTime,
                    Description = item.Summary.Text,
                    Link = item.Links[0].Uri.OriginalString,
                    Title = item.Title.Text,
                }).Select(art => m.AddArticle(art, c)).ToArray();
            m.DeleteArticle(c.Articles.Where(x => !x.IsStared).Except(articles).ToArray(), c);
            c.LastUpdate = DateTime.Now;
        }
 public void AddChannel(Channel c)
 {
     GetArticles(c);
 }
        private void Read(string xml, Channel c, RssViewModel m)
        {
            xml = xml.Replace(RemoveLastBuildDate, String.Empty);

            var b = Encoding.UTF8.GetBytes(xml);

            var settings = new XmlReaderSettings
            {
                ConformanceLevel = ConformanceLevel.Fragment,
                IgnoreWhitespace = true,
                IgnoreComments = true
            };

            var feed = SyndicationFeed.Load(XmlReader.Create(new MemoryStream(b), settings));

            if (feed == null) return;
            c.Title = feed.Title.Text;
            c.Image = feed.ImageUrl.OriginalString;

            var articles = feed.Items.Select(item => new Article
                {
                    PubDate = item.PublishDate.DateTime,
                    AddDate = DateTime.Now,
                    //The reason for the 8,060-byte limit
                    Description = item.Summary.Text.Length < 1000 ? item.Summary.Text : item.Summary.Text.Substring(0, 1000),
                    Link = item.Links[0].Uri.OriginalString,
                    Title = item.Title.Text,
                }).Select(art => m.AddArticle(art, c)).ToArray();

            m.DeleteArticle(c.Articles.Where(x => !x.IsStared).Except(articles).ToArray(), c);
            c.LastUpdate = DateTime.Now;
        }
 private void DoAddChanel(object data)
 {
     var rss = data as string;
     if (String.IsNullOrEmpty(rss)) return;
     var c = new Channel { URL = rss, Title = rss };
     c = AddChannel(c) ? c : Channels.First(x => x.URL == c.URL);
     _parseRss.AddChannel(c);
 }
 public void UpdateChannel(Channel ch)
 {
     _parseRss.GetArticles(ch);
 }
 public void RefreshChannel(Channel ch)
 {
     UpdateChannel(ch);
        RefreshArticles();
 }
        // Remove a to-do task item from the database and collections.
        public void DeleteChannel(Channel channel)
        {
            // Remove the to-do item from the "all" observable collection.
            Channels.Remove(channel);

            foreach (Article article in channel.Articles)
            {
                AllArticles.Remove(article);
                if (!article.IsRead)
                {
                    NewArticles.Remove(article);
                }
                _rssDb.Articles.DeleteOnSubmit(article);
            }

            // Remove the to-do item from the data context.
            _rssDb.Channels.DeleteOnSubmit(channel);
        }
        // Remove a to-do task item from the database and collections.
        public void DeleteArticle(Article article, Channel channel)
        {
            // Remove the to-do item from the "all" observable collection.
            AllArticles.Remove(article);
            NewArticles.Remove(article);

            channel.Articles.Remove(article);
            // Remove the to-do item from the data context.
            _rssDb.Articles.DeleteOnSubmit(article);
        }
 // Remove a to-do task item from the database and collections.
 public void DeleteArticle(Article[] articles, Channel channel)
 {
     foreach (Article art in articles)
     {
         DeleteArticle(art, channel);
     }
 }
        // Add a to-do item to the database and collections.
        public bool AddChannel(Channel channel)
        {
            if (!_rssDb.Channels.Any(o => o.URL == channel.URL))
            {
                // Add a to-do item to the data context.
                _rssDb.Channels.InsertOnSubmit(channel);

                // Add a to-do item to the "all" observable collection.
                channel.Index = Channels.Count;
                Channels.Add(channel);

                return true;
            }
            return false;
        }
 // Add a to-do item to the database and collections.
 public Article AddArticle(Article article, Channel channel)
 {
     Article art = channel.Articles.FirstOrDefault(x => x.Link == article.Link);
     if (art == null)
     {
         channel.Articles.Add(article);
         // Add a to-do item to the data context.
         _rssDb.Articles.InsertOnSubmit(article);
         return article;
     }
     return art;
 }
        /// <summary>
        /// The Command function.
        /// </summary>
        public void MoveChannelUp(Channel ch)
        {
            int index = Channels.IndexOf(ch) - 1;
            if (index < 0) return;

            Channels.Remove(ch);
            Channels.Insert(index, ch);
            ch.Index = index;
        }
        public void MoveChannelDown(Channel ch)
        {
            int index = Channels.IndexOf(ch) + 1;
            if (index == Channels.Count) return;

            Channels.Remove(ch);
            Channels.Insert(index, ch);
            ch.Index = index;
        }
        private void PinChannelToStart(Channel channel)
        {
            string url = string.Format("/ChannelPage.xaml?ID={0}", channel.ID);
            ShellTile tileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(url));

            var secTileData = new StandardTileData
                {
                    Title = channel.Title,
                    Count = channel.NewCount,
                    BackgroundImage = new Uri("/Background.png", UriKind.RelativeOrAbsolute),
                    BackTitle = "Not read articles",
                    BackContent = channel.Title
                };

            // If the Tile was found, then update the background image.
            if (tileToFind != null)
            {
                tileToFind.Update(secTileData);
            }
            else
            {
                ShellTile.Create(new Uri(url, UriKind.RelativeOrAbsolute), secTileData);
            }
        }
        private void PinChannelToStart(Channel channel)
        {
            if (channel.LastUpdate.Value == Convert.ToDateTime(MinDBUpdated))
            {
                ToastPrompt toast = new ToastPrompt();
                toast.Foreground = App.WhiteColor;
                toast.MillisecondsUntilHidden = 1500;
                toast.Message = AppResources.tile_not_allow;
                toast.Show();

                return;
            }

            string url = string.Format("/ChannelPage.xaml?ID={0}", channel.ID);
            ShellTile tileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains(url));
            var secTileData = App.AddTile(channel.NewCount, channel.ID.ToString(), channel.Title);//channel.GetImage, channel.Title);

            // If the Tile was found, then update the background image.
            if (tileToFind != null)
            {
                tileToFind.Update(secTileData);
            }
            else
            {
                ShellTile.Create(new Uri(url, UriKind.RelativeOrAbsolute), secTileData);
            }
        }
        /// <summary>
        /// The Command function.
        /// </summary>
        public void MoveChannel(Channel ch, int indexTo)
        {
            int index = Channels.IndexOf(ch);

            if (index == indexTo || index == Channels.Count || index < 0)
            {
                return;
            }

            //indexTo = (index < indexTo) ? indexTo-1 : indexTo;

            Channels.Remove(ch);
            Channels.Insert(indexTo, ch);
            ch.Index = indexTo;
        }