Ejemplo n.º 1
0
        private async Task LoadPage(PageViewModel pageModel)
        {
            if (pageModel == null)
            {
                return;
            }

            // Set image
            if (pageModel.ImageSource == null)
            {
                pageModel.ImageSource = FileUtils.GetImageOnlineUri(FileUtils.GetPageFileName(pageModel.PageNumber));
            }

            // Set translation
            if (pageModel.Translations.Count == 0)
            {
                try
                {
                    if (!await HasTranslationFile())
                    {
                        return;
                    }

                    List<QuranAyah> verses = null;
                    using (var db = new QuranDatabaseHandler<QuranAyah>(this.TranslationFile))
                    {
                        verses = await new TaskFactory().StartNew(() => db.GetVerses(pageModel.PageNumber));
                    }

                    FlowDirection flowDirection = FlowDirection.LeftToRight;
                    Regex translationFilePattern = new Regex(@"quran\.([\w-]+)\..*");
                    var fileMatch = translationFilePattern.Match(this.TranslationFile);
                    if (fileMatch.Success)
                    {
                        try
                        {
                            var cultureInfo = new CultureInfo(fileMatch.Groups[1].Value);
                            if (cultureInfo.TextInfo.IsRightToLeft)
                            {
                                flowDirection = FlowDirection.RightToLeft;
                            }
                        }
                        catch (Exception e)
                        {
                            telemetry.TrackException(e, new Dictionary<string, string> { { "Scenario", "ParseTranslationFileForFlowDirection" } });
                        }
                    }

                    List <ArabicAyah> versesArabic = null;
                    if (this.ShowArabicInTranslation && await FileUtils.HaveArabicSearchFile())
                    {
                        try
                        {
                            using (var dbArabic = new QuranDatabaseHandler<ArabicAyah>(FileUtils.QURAN_ARABIC_DATABASE))
                            {
                                versesArabic = await new TaskFactory().StartNew(() => dbArabic.GetVerses(pageModel.PageNumber));
                            }
                        }
                        catch (Exception e)
                        {
                            telemetry.TrackException(e, new Dictionary<string, string> { { "Scenario", "OpenArabicDatabase" } });
                        }
                    }

                    int tempSurah = -1;
                    for (int i = 0; i < verses.Count; i++)
                    {
                        var verse = verses[i];
                        if (verse.Surah != tempSurah)
                        {
                            pageModel.Translations.Add(new VerseViewModel(this)
                            {
                                IsHeader = true,
                                Text = QuranUtils.GetSurahName(verse.Surah, true)
                            });

                            tempSurah = verse.Surah;
                        }

                        var verseViewModel = new VerseViewModel(this)
                        {
                            Surah = verse.Surah,
                            Ayah = verse.Ayah,
                            Text = verse.Text,
                            FlowDirection = flowDirection
                        };
                        if (versesArabic != null)
                        {
                            verseViewModel.ArabicText = versesArabic[i].Text;
                        }

                        pageModel.Translations.Add(verseViewModel);
                    }
                }
                catch (Exception e)
                {
                    // Try delete bad translation file if error is "no such table: verses"
                    try
                    {
                        if (e.Message.StartsWith("no such table:", StringComparison.OrdinalIgnoreCase))
                        {
                            await FileUtils.SafeFileDelete(Path.Combine(FileUtils.GetQuranDatabaseDirectory(),
                                                                   this.TranslationFile));
                        }
                    }
                    catch
                    {
                        // Do nothing
                    }
                    pageModel.Translations.Add(new VerseViewModel(this) { Text = "Error loading translation..." });
                    telemetry.TrackException(e, new Dictionary<string, string> { { "Scenario", "LoadingTranslation" } });
                }
            }
            return;
        }
Ejemplo n.º 2
0
        private async Task LoadPage(PageViewModel pageModel)
        {
            if (pageModel == null)
            {
                return;
            }

            // Set image
            if (pageModel.ImageSource == null)
            {
                pageModel.ImageSource = FileUtils.GetImageOnlineUri(FileUtils.GetPageFileName(pageModel.PageNumber));
            }

            // Set translation
            if (pageModel.Translations.Count == 0)
            {
                try
                {
                    if (!await HasTranslationFile())
                    {
                        return;
                    }

                    List <QuranAyah> versesTranslation = null;
                    using (var db = new QuranDatabaseHandler <QuranAyah>(this.TranslationFile))
                    {
                        versesTranslation = await new TaskFactory().StartNew(() => db.GetVerses(pageModel.PageNumber));
                    }

                    FlowDirection flowDirection          = FlowDirection.LeftToRight;
                    Regex         translationFilePattern = new Regex(@"quran\.([\w-]+)\..*");
                    var           fileMatch = translationFilePattern.Match(this.TranslationFile);
                    if (fileMatch.Success)
                    {
                        try
                        {
                            var cultureName = fileMatch.Groups[1].Value;
                            if (cultureName.Length > 2)
                            {
                                cultureName = cultureName.Substring(0, 2);
                            }
                            var cultureInfo = new CultureInfo(cultureName);
                            if (cultureInfo.TextInfo.IsRightToLeft)
                            {
                                flowDirection = FlowDirection.RightToLeft;
                            }
                        }
                        catch (Exception e)
                        {
                            telemetry.TrackException(e, new Dictionary <string, string> {
                                { "Scenario", "ParseTranslationFileForFlowDirection" }
                            });
                        }
                    }

                    List <ArabicAyah> versesArabic = null;
                    if (SettingsUtils.Get <bool>(Constants.PREF_SHOW_ARABIC_IN_TRANSLATION))
                    {
                        try
                        {
                            using (var dbArabic = new QuranDatabaseHandler <ArabicAyah>(FileUtils.ArabicDatabase))
                            {
                                versesArabic = await new TaskFactory().StartNew(() => dbArabic.GetVerses(pageModel.PageNumber));
                            }
                        }
                        catch (Exception e)
                        {
                            telemetry.TrackException(e, new Dictionary <string, string> {
                                { "Scenario", "OpenArabicDatabase" }
                            });
                        }
                    }

                    int tempSurah = -1;
                    for (int i = 0; i < versesTranslation.Count; i++)
                    {
                        var verse = versesTranslation[i];
                        if (verse.Surah != tempSurah)
                        {
                            pageModel.Translations.Add(new VerseViewModel(this)
                            {
                                IsHeader = true,
                                Text     = QuranUtils.GetSurahName(verse.Surah, true)
                            });

                            tempSurah = verse.Surah;
                        }


                        string translation = verse.Text;
                        if (QuranUtils.HasBismillah(verse.Surah) && verse.Surah != 1 && verse.Ayah == 1)
                        {
                            translation = _bismillahTranslation + " " + translation;
                        }
                        var verseViewModel = new VerseViewModel(this)
                        {
                            Surah         = verse.Surah,
                            Ayah          = verse.Ayah,
                            Text          = translation,
                            FlowDirection = flowDirection
                        };
                        if (versesArabic != null)
                        {
                            verseViewModel.ArabicText = versesArabic[i].Text;
                        }

                        pageModel.Translations.Add(verseViewModel);
                    }
                }
                catch (Exception e)
                {
                    // Try delete bad translation file if error is "no such table: verses"
                    try
                    {
                        if (e.Message.StartsWith("no such table:", StringComparison.OrdinalIgnoreCase))
                        {
                            await FileUtils.SafeFileDelete(Path.Combine(FileUtils.GetQuranDatabaseDirectory(),
                                                                        this.TranslationFile));
                        }
                    }
                    catch
                    {
                        // Do nothing
                    }
                    pageModel.Translations.Add(new VerseViewModel(this)
                    {
                        Text = "Error loading translation..."
                    });
                    telemetry.TrackException(e, new Dictionary <string, string> {
                        { "Scenario", "LoadingTranslation" }
                    });
                }
            }
            return;
        }