예제 #1
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>
        /// 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;
            }
        }