public override void Initialize() { if (!IsInitialized) { SourceDataService.Initialize(); IsInitialized = true; } }
public async Task CheckIfSourceWorking() { IsChecking = true; if (RssUrl != null) { IsWorking = await SourceDataService.IsSourceWorkingAsync(RssUrl.ToString()); } IsChecking = false; }
public override int GetFileSize(Item fileItem) { DemoItem item = fileItem as DemoItem; if (item.Content != null) { DemoBinaryContent content = item.Content as DemoBinaryContent; if (content.IsLoaded) { string filePath = PrepareContentFilePath(content); lock (this.demoDataLocker) { FileInfo file = new FileInfo(filePath); return((int)file.Length); } } return(SourceDataService.GetFileSize(fileItem)); } return(0); }
/// <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(); }
/// <summary> /// Delete the selected source /// </summary> /// <returns>Task Type</returns> private async Task DeleteSource() { if (IsWorking) { return; } IsWorking = true; try { MessageDialog showDialog = new MessageDialog("SourcesViewModelSourceDeleteAllRSSMessageDialog".GetLocalized()); showDialog.Commands.Add(new UICommand("Yes".GetLocalized()) { Id = 0 }); showDialog.Commands.Add(new UICommand("No".GetLocalized()) { Id = 1 }); showDialog.DefaultCommandIndex = 0; showDialog.CancelCommandIndex = 1; var result = await showDialog.ShowAsync(); if ((int)result.Id == 0) { await RSSDataService.DeleteManyFeedsAsync(x => x.PostSource.Id == SelectedSource.Id); } await SourceDataService.DeleteSourceAsync(SelectedSource); Sources.Remove(SelectedSource); ClearPopups(); RefreshSourcesCommand.OnCanExecuteChanged(); } catch (Exception ex) { Debug.WriteLine(ex); } finally { IsWorking = false; } }
/// <summary> /// Delete the selected source /// </summary> /// <returns>Task Type</returns> private async Task DeleteSource() { if (IsWorking) { return; } IsWorking = true; try { await SourceDataService.DeleteSourceAsync(SelectedSource); Sources.Remove(SelectedSource); } catch (Exception ex) { Debug.WriteLine(ex); } finally { IsWorking = false; } }
/// <summary> /// Update selected source (Title - Url - Description) /// </summary> /// <returns>Task Type</returns> private async Task UpdateSource() { if (IsWorking) { return; } IsWorking = true; try { SelectedSource.SiteTitle = SourceTitle; SelectedSource.RssUrl = new Uri(SourceUrl); SelectedSource.Description = SourceDescription; var source = await SourceDataService.UpdateSourceAsync(SelectedSource); if (source == null) { await new MessageDialog("SourcesViewModelSourceInfoNotValidMessageDialog".GetLocalized()).ShowAsync(); return; } Sources[Sources.IndexOf(SelectedSource)] = SelectedSource; await new MessageDialog("SourcesViewModelSourceUpdatedMessageDialog".GetLocalized()).ShowAsync(); ClearPopups(); } catch (Exception ex) { Debug.WriteLine(ex); } finally { IsWorking = false; } }
public override void CloseUnitOfWork() { SourceDataService.CloseUnitOfWork(); }
/// <summary> /// Check if the source exist if not gets its info then add it to the database /// </summary> /// <returns>Task Type</returns> private async Task AddNewSource() { if (IsWorking) { return; } IsWorking = true; try { string trimedUrl = SourceUrl.TrimEnd('/'); var exist = await SourceDataService.SourceExistAsync(trimedUrl); if (exist) { await new MessageDialog("SourcesViewModelSourceExistMessageDialog".GetLocalized()).ShowAsync(); } else { try { var feedString = await RssRequest.GetFeedAsStringAsync(trimedUrl); var xmlSource = feedString.TrimStart(); var source = await SourceDataService.GetSourceInfoFromRssAsync(xmlSource, trimedUrl); if (source == null) { await new MessageDialog("SourcesViewModelSourceInfoNotValidMessageDialog".GetLocalized()).ShowAsync(); return; } Sources.Insert(0, await SourceDataService.AddNewSourceAsync(source)); await new MessageDialog("SourcesViewModelSourceAddedMessageDialog".GetLocalized()).ShowAsync(); ClearPopups(); } catch (HttpRequestException ex) { await new MessageDialog("HttpRequestExceptionMessageDialog".GetLocalized()).ShowAsync(); Debug.WriteLine(ex); } catch (XmlException ex) { await new MessageDialog("XmlExceptionMessageDialog".GetLocalized()).ShowAsync(); Debug.WriteLine(ex); } catch (ArgumentNullException ex) { await new MessageDialog("SourcesViewModelSourceUrlNullExceptionMessageDialog".GetLocalized()).ShowAsync(); Debug.WriteLine(ex); } catch (Exception ex) { Debug.WriteLine(ex); } } } catch (Exception ex) { Debug.WriteLine(ex); } finally { IsWorking = 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 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 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; }
/// <summary> /// Check if the source exist if not gets its info then add it to the database /// </summary> /// <returns>Task Type</returns> private async Task AddNewSource() { if (IsWorking) { return; } IsWorking = true; try { string trimedUrl = SourceUrl.TrimEnd('/'); var exist = await SourceDataService.SourceExistAsync(trimedUrl); if (exist) { await new MessageDialog("SourcesViewModelSourceExistMessageDialog".GetLocalized()).ShowAsync(); } else { try { var feedString = await RssRequest.GetFeedAsStringAsync(trimedUrl, TokenSource.Token); var source = await SourceDataService.GetSourceInfoFromRssAsync(feedString, trimedUrl); if (source == null) { await new MessageDialog("SourcesViewModelSourceInfoNotValidMessageDialog".GetLocalized()).ShowAsync(); return; } Sources.Insert(0, await SourceDataService.AddNewSourceAsync(source)); RefreshSourcesCommand.OnCanExecuteChanged(); await new MessageDialog("SourcesViewModelSourceAddedMessageDialog".GetLocalized()).ShowAsync(); ClearPopups(); } catch (HttpRequestException ex) { if (ex.Message.StartsWith("Response status code does not indicate success: 403")) { await new MessageDialog("HttpRequestException403MessageDialog".GetLocalized()).ShowAsync(); } else { await new MessageDialog("HttpRequestExceptionMessageDialog".GetLocalized()).ShowAsync(); } Debug.WriteLine(ex); } catch (XmlException ex) { await new MessageDialog("XmlExceptionMessageDialog".GetLocalized()).ShowAsync(); Debug.WriteLine(ex); } catch (ArgumentNullException ex) { await new MessageDialog("SourcesViewModelSourceUrlNullExceptionMessageDialog".GetLocalized()).ShowAsync(); Debug.WriteLine(ex); } catch (Exception ex) { Debug.WriteLine(ex); } } } catch (Exception ex) { Debug.WriteLine(ex); } finally { IsWorking = false; } }