/// <summary>
        /// Gets all the sources from the database and checks if they still works or not
        /// </summary>
        /// <param name="progress"></param>
        /// <param name="ct"></param>
        /// <returns>Task Type</returns>
        public async Task LoadDataAsync(IProgress <int> progress, CancellationToken token)
        {
            if (SystemInformation.IsFirstRun)
            {
                var messageDialog = new MessageDialog("WelcomeMessageForFirstRun".GetLocalized());
                await messageDialog.ShowAsync();
            }

            IsLoadingData = true;
            Sources.Clear();

            var sourcesDataList = await SourceDataService.GetSourcesDataAsync();

            ProgressMax     = sourcesDataList.Count();
            ProgressCurrent = 0;
            int progressCount = 0;

            foreach (var source in sourcesDataList)
            {
                if (token.IsCancellationRequested)
                {
                    IsLoadingData = false;
                    return;
                }

                Sources.Add(source);

                progress.Report(++progressCount);
            }

            foreach (var item in Sources)
            {
                if (token.IsCancellationRequested)
                {
                    IsLoadingData = false;
                    return;
                }

                await item.CheckIfSourceWorking();
            }

            RefreshSourcesCommand.OnCanExecuteChanged();
        }
Exemple #2
0
        /// <summary>
        /// Gets all the feeds from database (with-in limits in settings)
        /// the try to gets all the new stuff from your sources
        /// add the new ones to the database if there is any
        /// then show the latest (with-in limits in settings)
        /// </summary>
        /// <param name="progress"></param>
        /// <param name="ct"></param>
        /// <returns>Task Type</returns>
        public async Task LoadDataAsync(IProgress <int> progress, CancellationToken token)
        {
            IsLoadingData = true;
            FilterSources.Clear();
            Feeds.Clear();
            bool hasLoadedFeedNewItems = false;

            foreach (var rss in await RSSDataService.GetFeedsDataAsync(await ApplicationData.Current.LocalSettings.ReadAsync <int>("FeedsLimit")))
            {
                Feeds.Add(rss);
            }

            SyndicationFeed feed = new SyndicationFeed();

            var sourcesDataList = await SourceDataService.GetSourcesDataAsync();

            ProgressMax     = sourcesDataList.Count();
            ProgressCurrent = 0;
            int progressCount = 0;

            foreach (var source in sourcesDataList)
            {
                FilterSources.Add(source);

                if (token.IsCancellationRequested)
                {
                    IsLoadingData = false;
                    return;
                }
            }
            // if there is no internet just cut our loses and get out of here we already loaded the local data
            if (!new NetworkInformationHelper().HasInternetAccess)
            {
                await new MessageDialog("CheckInternetMessageDialog".GetLocalized()).ShowAsync();
                return;
            }

            foreach (var sourceItem in FilterSources)
            {
                if (token.IsCancellationRequested)
                {
                    IsLoadingData = false;
                    return;
                }

                if (!new NetworkInformationHelper().HasInternetAccess)
                {
                    continue;
                }

                progress.Report(++progressCount);

                //if getting the feed crushed for (internet - not xml rss - other reasons)
                //move to the next source on the list to try it instead of stoping every thing
                try
                {
                    var feedString = await RssRequest.GetFeedAsStringAsync(sourceItem.RssUrl);

                    if (string.IsNullOrWhiteSpace(feedString))
                    {
                        continue;
                    }
                    else
                    {
                        var xmlFeed = feedString.TrimStart();
                        feed.Load(xmlFeed);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                    continue;
                }

                // Iterate through each feed item.
                foreach (SyndicationItem syndicationItem in feed.Items)
                {
                    if (token.IsCancellationRequested)
                    {
                        IsLoadingData = false;
                        return;
                    }

                    //handle edge cases like when they don't send that stuff or misplace them like freaking reddit r/worldnews
                    if (syndicationItem.Title == null)
                    {
                        syndicationItem.Title = new SyndicationText("MainViewModelNoTitleFound".GetLocalized());
                    }
                    if (syndicationItem.Summary == null)
                    {
                        syndicationItem.Summary = new SyndicationText("MainViewModelNoSummaryFound".GetLocalized());
                    }
                    if (syndicationItem.PublishedDate.Year < 2000)
                    {
                        syndicationItem.PublishedDate = syndicationItem.LastUpdatedTime.Year > 2000 ? syndicationItem.LastUpdatedTime : DateTimeOffset.Now;
                    }

                    Uri itemNewUri = syndicationItem.ItemUri;
                    if (itemNewUri == null)
                    {
                        if (syndicationItem.Links.Count > 0)
                        {
                            itemNewUri = syndicationItem.Links.FirstOrDefault().Uri;
                        }
                    }

                    var rss = new RSS
                    {
                        PostTitle   = syndicationItem.Title.Text,
                        Description = syndicationItem.Summary.Text,
                        Authors     = new List <Author>(),
                        URL         = itemNewUri,
                        CreatedAt   = syndicationItem.PublishedDate.DateTime,
                        Guid        = syndicationItem.Id,
                        PostSource  = sourceItem
                    };

                    foreach (var author in syndicationItem.Authors)
                    {
                        rss.Authors.Add(new Author
                        {
                            Name  = author.Name,
                            Email = author.Email,
                            Uri   = author.Uri
                        });
                    }

                    if (!await RSSDataService.FeedExistAsync(rss))
                    {
                        var newRss = await RSSDataService.AddNewFeedAsync(rss);

                        Feeds.Add(newRss);
                        hasLoadedFeedNewItems = true;
                    }
                }

                //shorten the text for windows 10 Live Tile
                Singleton <LiveTileService> .Instance.SampleUpdate(feed.Title.Text, ShortenText(feed.Items.FirstOrDefault()?.Title.Text, 80), ShortenText(feed.Items.FirstOrDefault()?.Summary.Text, 95));
            }

            if (hasLoadedFeedNewItems)
            {
                Feeds.Clear();
                foreach (var rss in await RSSDataService.GetFeedsDataAsync(await ApplicationData.Current.LocalSettings.ReadAsync <int>("FeedsLimit")))
                {
                    Feeds.Add(rss);
                }
            }
            MarkAsReadCommand.OnCanExecuteChanged();
            IsLoadingData = false;
        }
        /// <summary>
        /// Gets all the feeds from database (with-in limits in settings)
        /// the try to gets all the new stuff from your sources
        /// add the new ones to the database if there is any
        /// then show the latest (with-in limits in settings)
        /// </summary>
        /// <param name="progress"></param>
        /// <param name="token"></param>
        /// <returns>Task Type</returns>
        public async Task LoadDataAsync(IProgress <int> progress, CancellationToken token)
        {
            IsLoadingData = true;
            FilterSources.Clear();
            Feeds.Clear();
            ProgressCurrent = 0;
            bool hasLoadedFeedNewItems = false;

            // Shows the user what's new in this version
            await WhatsNewDisplayService.ShowIfAppropriateAsync();

            // Set Httpclient userAgent to the user selected one
            await RssRequest.SetCustomUserAgentAsync();

            foreach (var rss in await RSSDataService.GetFeedsDataAsync(await ApplicationData.Current.LocalSettings.ReadAsync <int>("FeedsLimit")))
            {
                Feeds.Add(rss);
            }

            SyndicationFeed feed = null;

            var sourcesDataList = await SourceDataService.GetSourcesDataAsync();

            ProgressMax = sourcesDataList.Count();
            int progressCount = 0;

            foreach (var source in sourcesDataList)
            {
                FilterSources.Add(source);

                if (token.IsCancellationRequested)
                {
                    IsLoadingData = false;
                    TokenSource   = new CancellationTokenSource();
                    MarkAsReadCommand.OnCanExecuteChanged();
                    return;
                }
            }
            // if there is no internet just cut our loses and get out of here we already loaded the local data
            if (!new NetworkInformationHelper().HasInternetAccess)
            {
                await new MessageDialog("CheckInternetMessageDialog".GetLocalized()).ShowAsync();
                return;
            }
            var WaitAfterLastCheckInMinutes = await ApplicationData.Current.LocalSettings.ReadAsync <int>("WaitAfterLastCheck");

            foreach (var sourceItem in FilterSources)
            {
                bool isFirstItemInFeed = true;

                if (token.IsCancellationRequested)
                {
                    IsLoadingData = false;
                    TokenSource   = new CancellationTokenSource();
                    MarkAsReadCommand.OnCanExecuteChanged();
                    return;
                }

                // don't get source feed if x number of minutes haven't passed since the last one - default is 2 hours
                var checkSourceAfter = sourceItem.LastBuildCheck.AddMinutes(WaitAfterLastCheckInMinutes);

                if (checkSourceAfter >= DateTimeOffset.Now)
                {
                    continue;
                }

                if (!new NetworkInformationHelper().HasInternetAccess)
                {
                    continue;
                }

                progress.Report(++progressCount);

                //if getting the feed crushed for (internet - not xml rss - other reasons)
                //move to the next source on the list to try it instead of stopping every thing
                try
                {
                    var feedString = await RssRequest.GetFeedAsStringAsync(sourceItem.RssUrl, token);

                    feed = new SyndicationFeed();

                    if (string.IsNullOrWhiteSpace(feedString))
                    {
                        continue;
                    }
                    else
                    {
                        feed.Load(feedString);

                        // Saves rss items count and last check time to source
                        sourceItem.CurrentRssItemsCount = feed.Items.Count;
                        sourceItem.LastBuildCheck       = DateTimeOffset.Now;
                        await SourceDataService.UpdateSourceAsync(sourceItem);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                    continue;
                }

                // Iterate through each feed item.
                foreach (SyndicationItem syndicationItem in feed.Items)
                {
                    if (token.IsCancellationRequested)
                    {
                        IsLoadingData = false;
                        TokenSource   = new CancellationTokenSource();
                        MarkAsReadCommand.OnCanExecuteChanged();
                        return;
                    }

                    //handle edge cases like when they don't send that stuff or misplace them like freaking reddit r/worldnews
                    if (syndicationItem.Title == null)
                    {
                        syndicationItem.Title = new SyndicationText("MainViewModelNoTitleFound".GetLocalized());
                    }
                    if (syndicationItem.Summary == null)
                    {
                        syndicationItem.Summary = new SyndicationText("MainViewModelNoSummaryFound".GetLocalized());
                    }
                    if (syndicationItem.PublishedDate.Year < 2000)
                    {
                        syndicationItem.PublishedDate = syndicationItem.LastUpdatedTime.Year > 2000 ? syndicationItem.LastUpdatedTime : DateTimeOffset.Now;
                    }

                    Uri itemNewUri = syndicationItem.ItemUri;
                    if (itemNewUri == null)
                    {
                        if (syndicationItem.Links.Count > 0)
                        {
                            itemNewUri = syndicationItem.Links.FirstOrDefault().Uri;
                        }
                    }
                    if (string.IsNullOrWhiteSpace(syndicationItem.Id))
                    {
                        syndicationItem.Id = itemNewUri.ToString();
                    }

                    var rss = new RSS
                    {
                        PostTitle   = syndicationItem.Title.Text,
                        Description = syndicationItem.Summary.Text,
                        Authors     = new List <Author>(),
                        URL         = itemNewUri,
                        CreatedAt   = syndicationItem.PublishedDate.DateTime,
                        Guid        = syndicationItem.Id,
                        PostSource  = sourceItem
                    };

                    foreach (var author in syndicationItem.Authors)
                    {
                        rss.Authors.Add(new Author
                        {
                            Name  = author.Name,
                            Email = author.Email,
                            Uri   = author.Uri
                        });
                    }

                    if (!await RSSDataService.FeedExistAsync(rss))
                    {
                        var newRss = await RSSDataService.AddNewFeedAsync(rss);

                        Feeds.Add(newRss);
                        hasLoadedFeedNewItems = true;

                        // Add first item in each source feed to Windows Live Tiles
                        if (isFirstItemInFeed)
                        {
                            Singleton <LiveTileService> .Instance.SampleUpdate(newRss.PostSource.SiteTitle, ShortenText(newRss.PostTitle, 80), ShortenText(newRss.Description, 95));
                        }
                        isFirstItemInFeed = false;
                    }
                }
            }

            if (hasLoadedFeedNewItems)
            {
                Feeds.Clear();
                foreach (var rss in await RSSDataService.GetFeedsDataAsync(await ApplicationData.Current.LocalSettings.ReadAsync <int>("FeedsLimit")))
                {
                    Feeds.Add(rss);
                }
            }
            IsLoadingData = false;
            MarkAsReadCommand.OnCanExecuteChanged();
        }
        /// <summary>
        /// Gets all the sources from the database and checks if they still works or not
        /// </summary>
        /// <param name="progress"></param>
        /// <param name="ct"></param>
        /// <returns>Task Type</returns>
        public async Task LoadDataAsync(IProgress <int> progress, CancellationToken token)
        {
            IsLoadingData   = true;
            ProgressCurrent = 0;
            Sources.Clear();

            var sourcesDataList = await SourceDataService.GetSourcesDataAsync();

            ProgressMax = sourcesDataList.Count();
            int progressCount = 0;

            foreach (var source in sourcesDataList)
            {
                if (token.IsCancellationRequested)
                {
                    IsLoadingData = false;
                    TokenSource   = new CancellationTokenSource();
                    return;
                }

                Sources.Add(source);

                progress.Report(++progressCount);
            }

            RefreshSourcesCommand.OnCanExecuteChanged();

            foreach (var item in Sources)
            {
                if (token.IsCancellationRequested)
                {
                    IsLoadingData = false;
                    TokenSource   = new CancellationTokenSource();
                    return;
                }

                try
                {
                    item.IsChecking = true;
                    var task = await SourceDataService.IsSourceWorkingAsync(item.RssUrl.AbsoluteUri);

                    item.IsWorking            = task.Item1;
                    item.LastBuildDate        = task.Item2;
                    item.CurrentRssItemsCount = task.Item3;

                    // Saves latest build date and rss items count to source
                    await SourceDataService.UpdateSourceAsync(item);
                }
                catch (HttpRequestException ex)
                {
                    if (ex.Message.StartsWith("Response status code does not indicate success: 403"))
                    {
                        item.ErrorMessage = "HttpRequestException403MessageDialog".GetLocalized();
                    }
                    else
                    {
                        item.ErrorMessage = "HttpRequestExceptionMessageDialog".GetLocalized();
                    }
                    Debug.WriteLine(ex);
                    item.IsError = true;
                }
                catch (XmlException ex)
                {
                    item.ErrorMessage = "XmlExceptionMessageDialog".GetLocalized();
                    Debug.WriteLine(ex);
                    item.IsError = true;
                }
                catch (ArgumentNullException ex)
                {
                    item.ErrorMessage = "SourcesViewModelSourceUrlNullExceptionMessageDialog".GetLocalized();
                    Debug.WriteLine(ex);
                    item.IsError = true;
                }
                catch (Exception ex)
                {
                    item.ErrorMessage = "SourcesViewModelExceptionMessageDialog".GetLocalized();
                    Debug.WriteLine(ex);
                    item.IsError = true;
                }
                finally
                {
                    item.IsChecking = false;
                }
            }

            IsLoadingData = false;
        }