From ff106ea1c9adf6ecef70dc96c883680836959817 Mon Sep 17 00:00:00 2001 From: Ahmed El-Helw Date: Wed, 2 Mar 2022 22:54:25 +0400 Subject: [PATCH] Fix incorrect ayah highlight during selection --- .../labs/androidquran/data/QuranDisplayData.kt | 13 ++++--------- .../labs/androidquran/data/SuraAyahIterator.kt | 8 ++++++++ .../quran/ayahtracker/AyahTrackerPresenter.kt | 15 +++++++++------ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/quran/labs/androidquran/data/QuranDisplayData.kt b/app/src/main/java/com/quran/labs/androidquran/data/QuranDisplayData.kt index 4d637f813e..f16f071456 100644 --- a/app/src/main/java/com/quran/labs/androidquran/data/QuranDisplayData.kt +++ b/app/src/main/java/com/quran/labs/androidquran/data/QuranDisplayData.kt @@ -173,17 +173,12 @@ class QuranDisplayData @Inject constructor(private val quranInfo: QuranInfo) { return context.getString(R.string.quran_sura_title, getSuraNameFromPage(context, page)) } - fun getAyahKeysOnPage(page: Int, lowerBound: SuraAyah?, upperBound: SuraAyah?): Set { + fun getAyahKeysOnPage(page: Int): Set { val ayahKeys: MutableSet = LinkedHashSet() val bounds = quranInfo.getPageBounds(page) - var start = SuraAyah(bounds[0], bounds[1]) - var end = SuraAyah(bounds[2], bounds[3]) - if (lowerBound != null) { - start = SuraAyah.max(start, lowerBound) - } - if (upperBound != null) { - end = SuraAyah.min(end, upperBound) - } + val start = SuraAyah(bounds[0], bounds[1]) + val end = SuraAyah(bounds[2], bounds[3]) + val iterator = SuraAyahIterator(quranInfo, start, end) while (iterator.next()) { ayahKeys.add(iterator.sura.toString() + ":" + iterator.ayah.toString()) diff --git a/app/src/main/java/com/quran/labs/androidquran/data/SuraAyahIterator.kt b/app/src/main/java/com/quran/labs/androidquran/data/SuraAyahIterator.kt index 0508d019b3..dbb8622ff8 100644 --- a/app/src/main/java/com/quran/labs/androidquran/data/SuraAyahIterator.kt +++ b/app/src/main/java/com/quran/labs/androidquran/data/SuraAyahIterator.kt @@ -54,4 +54,12 @@ class SuraAyahIterator( } return true } + + fun asSet(): Set { + val suraAyahSet = mutableSetOf() + while (next()) { + suraAyahSet.add("$sura:$ayah") + } + return suraAyahSet + } } diff --git a/app/src/main/java/com/quran/labs/androidquran/presenter/quran/ayahtracker/AyahTrackerPresenter.kt b/app/src/main/java/com/quran/labs/androidquran/presenter/quran/ayahtracker/AyahTrackerPresenter.kt index 5f96dba044..115c1d33e5 100644 --- a/app/src/main/java/com/quran/labs/androidquran/presenter/quran/ayahtracker/AyahTrackerPresenter.kt +++ b/app/src/main/java/com/quran/labs/androidquran/presenter/quran/ayahtracker/AyahTrackerPresenter.kt @@ -17,6 +17,7 @@ import com.quran.data.model.selection.startSuraAyah import com.quran.labs.androidquran.common.LocalTranslation import com.quran.labs.androidquran.common.QuranAyahInfo import com.quran.labs.androidquran.data.QuranDisplayData +import com.quran.labs.androidquran.data.SuraAyahIterator import com.quran.labs.androidquran.presenter.Presenter import com.quran.labs.androidquran.presenter.quran.ayahtracker.AyahTrackerPresenter.AyahInteractionHandler import com.quran.labs.androidquran.ui.PagerActivity @@ -111,13 +112,15 @@ class AyahTrackerPresenter @Inject constructor( highlightAyah(suraAyah.sura, suraAyah.ayah, -1, HighlightTypes.SELECTION, false) } is AyahSelection.AyahRange -> { + val highlightAyatIterator = + SuraAyahIterator(quranInfo, ayahSelection.startSuraAyah, ayahSelection.endSuraAyah) + val highlightedAyat = highlightAyatIterator.asSet() items.forEach { - val elements = quranDisplayData.getAyahKeysOnPage( - it.page, - ayahSelection.startSuraAyah, - ayahSelection.endSuraAyah - ) - it.onHighlightAyat(it.page, elements, HighlightTypes.SELECTION) + val pageAyat = quranDisplayData.getAyahKeysOnPage(it.page) + val elements = pageAyat.intersect(highlightedAyat) + if (elements.isNotEmpty()) { + it.onHighlightAyat(it.page, elements, HighlightTypes.SELECTION) + } } } else -> { /* nothing is selected, and we already cleared */ }