Exemplo n.º 1
0
        /// <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>
        /// 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;
            }
        }