Skip to content

Commit

Permalink
Fix multiple instances of quran data source info being created
Browse files Browse the repository at this point in the history
  • Loading branch information
aabdurrahman committed Mar 8, 2022
1 parent 6808154 commit 5e28376
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ constructor(
suspend fun migrateBookmarksData(sourcePageType: String, destinationPageType: String) {
val source = pageTypes[sourcePageType]?.getDataSource()
val destination = pageTypes[destinationPageType]?.getDataSource()
if (source != null && destination != null && source.getNumberOfPages() != destination.getNumberOfPages()) {
val sourcePageSuraStart = source.getSuraForPageArray()
val sourcePageAyahStart = source.getAyahForPageArray()
if (source != null && destination != null && source.numberOfPages != destination.numberOfPages) {
val sourcePageSuraStart = source.suraForPageArray
val sourcePageAyahStart = source.ayahForPageArray
val destinationQuranInfo = QuranInfo(destination)

val suraAyahFromPage = { page: Int ->
Expand Down
10 changes: 5 additions & 5 deletions app/src/test/java/com/quran/data/core/QuranInfoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class QuranInfoTest {
@Test
fun testCorrectJuzBounds() {
val quranInfo = QuranInfo(MadaniDataSource())
val data = MadaniDataSource().getPageForJuzArray()
val data = MadaniDataSource().pageForJuzArray
data.forEachIndexed { index, value ->
assertThat(quranInfo.getJuzFromPage(value)).isEqualTo(index + 1)
}
Expand All @@ -18,7 +18,7 @@ class QuranInfoTest {
@Test
fun testCorrectJuzWithinJuz() {
val quranInfo = QuranInfo(MadaniDataSource())
val data = MadaniDataSource().getPageForJuzArray()
val data = MadaniDataSource().pageForJuzArray
data.forEachIndexed { index, value ->
// juz' x page plus 10 pages should still be juz' x
assertThat(quranInfo.getJuzFromPage(value + 10)).isEqualTo(index + 1)
Expand All @@ -31,7 +31,7 @@ class QuranInfoTest {
fun testFirstJuz() {
val quranInfo = QuranInfo(MadaniDataSource())
val dataSource = MadaniDataSource()
val firstPageOfSecondJuz = dataSource.getPageForJuzArray()[1]
val firstPageOfSecondJuz = dataSource.pageForJuzArray[1]
for (i in 1 until firstPageOfSecondJuz) {
assertThat(quranInfo.getJuzFromPage(i)).isEqualTo(1)
}
Expand All @@ -41,8 +41,8 @@ class QuranInfoTest {
fun testThirtiethJuz() {
val quranInfo = QuranInfo(MadaniDataSource())
val dataSource = MadaniDataSource()
val firstPageOfLastJuz = dataSource.getPageForJuzArray()[29]
val lastPageOfMushaf = dataSource.getNumberOfPages()
val firstPageOfLastJuz = dataSource.pageForJuzArray[29]
val lastPageOfMushaf = dataSource.numberOfPages
for (i in firstPageOfLastJuz until lastPageOfMushaf) {
assertThat(quranInfo.getJuzFromPage(i)).isEqualTo(30)
}
Expand Down
22 changes: 11 additions & 11 deletions common/data/src/main/java/com/quran/data/core/QuranInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import javax.inject.Inject
import kotlin.math.abs

class QuranInfo @Inject constructor(quranDataSource: QuranDataSource) {
private val suraPageStart = quranDataSource.getPageForSuraArray()
private val pageSuraStart = quranDataSource.getSuraForPageArray()
private val pageAyahStart = quranDataSource.getAyahForPageArray()
private val juzPageStart = quranDataSource.getPageForJuzArray()
private val juzPageOverride: Map<Int, Int> = quranDataSource.getJuzDisplayPageArrayOverride()
private val pageRub3Start = quranDataSource.getQuarterStartByPage()
private val suraNumAyahs = quranDataSource.getNumberOfAyahsForSuraArray()
private val suraIsMakki = quranDataSource.getIsMakkiBySuraArray()
val quarters = quranDataSource.getQuartersArray()

val numberOfPages = quranDataSource.getNumberOfPages()
private val suraPageStart = quranDataSource.pageForSuraArray
private val pageSuraStart = quranDataSource.suraForPageArray
private val pageAyahStart = quranDataSource.ayahForPageArray
private val juzPageStart = quranDataSource.pageForJuzArray
private val juzPageOverride: Map<Int, Int> = quranDataSource.juzDisplayPageArrayOverride
private val pageRub3Start = quranDataSource.quarterStartByPage
private val suraNumAyahs = quranDataSource.numberOfAyahsForSuraArray
private val suraIsMakki = quranDataSource.isMakkiBySuraArray
val quarters = quranDataSource.quartersArray

val numberOfPages = quranDataSource.numberOfPages
val numberOfPagesDual = numberOfPages / 2

fun getStartingPageForJuz(juz: Int): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import com.quran.data.model.SuraAyah
import com.quran.data.source.QuranDataSource

open class MadaniDataSource : QuranDataSource {
override fun getNumberOfPages() = 604
override val numberOfPages = 604

override fun getPageForSuraArray() = intArrayOf(
override val pageForSuraArray = intArrayOf(
/* 1 - 10 */ 1, 2, 50, 77, 106, 128, 151, 177, 187, 208,
/* 11 - 20 */ 221, 235, 249, 255, 262, 267, 282, 293, 305, 312,
/* 21 - 30 */ 322, 332, 342, 350, 359, 367, 377, 385, 396, 404,
Expand All @@ -20,7 +20,7 @@ open class MadaniDataSource : QuranDataSource {
/* 101 - 110 */ 600, 600, 601, 601, 601, 602, 602, 602, 603, 603,
/* 111 - 114 */ 603, 604, 604, 604)

override fun getSuraForPageArray() = intArrayOf(1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
override val suraForPageArray = intArrayOf(1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
Expand Down Expand Up @@ -49,7 +49,7 @@ open class MadaniDataSource : QuranDataSource {
70, 70, 71, 72, 72, 73, 73, 74, 74, 75, 76, 76, 77, 78, 78, 79, 80, 81, 82, 83,
83, 85, 86, 87, 89, 89, 91, 92, 95, 97, 98, 100, 103, 106, 109, 112)

override fun getAyahForPageArray() = intArrayOf(
override val ayahForPageArray = intArrayOf(
1, 1, 6, 17, 25, 30, 38, 49, 58, 62, 70, 77, 84, 89,
94, 102, 106, 113, 120, 127, 135, 142, 146, 154, 164, 170, 177, 182,
187, 191, 197, 203, 211, 216, 220, 225, 231, 234, 238, 246, 249, 253,
Expand Down Expand Up @@ -95,14 +95,14 @@ open class MadaniDataSource : QuranDataSource {
35, 1, 1, 16, 1, 24, 1, 15, 1, 1, 8, 10, 1, 1,
1, 1)

override fun getPageForJuzArray() = intArrayOf(
override val pageForJuzArray = intArrayOf(
/* 1 - 10 */ 1, 22, 42, 62, 82, 102, 121, 142, 162, 182,
/* 11 - 20 */ 201, 222, 242, 262, 282, 302, 322, 342, 362, 382,
/* 21 - 30 */ 402, 422, 442, 462, 482, 502, 522, 542, 562, 582)

override fun getJuzDisplayPageArrayOverride() = mapOf(121 to 6, 201 to 10)
override val juzDisplayPageArrayOverride = mapOf(121 to 6, 201 to 10)

override fun getNumberOfAyahsForSuraArray() = intArrayOf(
override val numberOfAyahsForSuraArray = intArrayOf(
/* 1 - 14 */ 7, 286, 200, 176, 120, 165, 206, 75, 129, 109, 123, 111, 43, 52,
/* 15 - 28 */ 99, 128, 111, 110, 98, 135, 112, 78, 118, 64, 77, 227, 93, 88,
/* 29 - 42 */ 69, 60, 34, 30, 73, 54, 45, 83, 182, 88, 75, 85, 54, 53,
Expand All @@ -113,7 +113,7 @@ open class MadaniDataSource : QuranDataSource {
/* 99 - 114 */ 8, 11, 11, 8, 3, 9, 5, 4, 7, 3, 6, 3, 5, 4, 5, 6
)

override fun getIsMakkiBySuraArray() = booleanArrayOf(
override val isMakkiBySuraArray = booleanArrayOf(
/* 1 - 10 */ true, false, false, false, false, true, true, false, false, true,
/* 11 - 20 */ true, true, false, true, true, true, true, true, true, true,
/* 21 - 30 */ true, false, true, false, true, true, true, true, true, true,
Expand All @@ -128,7 +128,7 @@ open class MadaniDataSource : QuranDataSource {
/* 111 - 114 */ true, true, true, true
)

override fun getQuarterStartByPage() = intArrayOf(-1, -1, -1, -1, 1, -1, 2, -1, 3,
override val quarterStartByPage = intArrayOf(-1, -1, -1, -1, 1, -1, 2, -1, 3,
-1, 4, -1, -1, 5, -1, -1, 6, -1, 7, -1, -1, 8, -1, 9, -1, -1, 10, -1, 11, -1,
-1, 12, -1, 13, -1, -1, 14, -1, 15, -1, -1, 16, -1, 17, -1, 18, -1, -1, 19,
-1, 20, -1, -1, 21, -1, 22, -1, -1, 23, -1, -1, 24, -1, 25, -1, -1, 26, -1,
Expand Down Expand Up @@ -164,7 +164,7 @@ open class MadaniDataSource : QuranDataSource {
-1, 233, -1, 234, -1, 235, -1, 236, -1, -1, 237, -1, 238, -1, -1, 239, -1, -1,
-1, -1, -1)

override fun getQuartersArray() = arrayOf(
override val quartersArray = arrayOf(
/* hizb 1 */ SuraAyah(1, 1), SuraAyah(2, 26), SuraAyah(2, 44), SuraAyah(2, 60),
/* hizb 2 */ SuraAyah(2, 75), SuraAyah(2, 92), SuraAyah(2, 106), SuraAyah(2, 124),
/* hizb 3 */ SuraAyah(2, 142), SuraAyah(2, 158), SuraAyah(2, 177), SuraAyah(2, 189),
Expand Down
20 changes: 10 additions & 10 deletions common/data/src/main/java/com/quran/data/source/QuranDataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package com.quran.data.source
import com.quran.data.model.SuraAyah

interface QuranDataSource {
fun getNumberOfPages() : Int
fun getPageForSuraArray() : IntArray
fun getSuraForPageArray() : IntArray
fun getAyahForPageArray() : IntArray
fun getPageForJuzArray() : IntArray
fun getJuzDisplayPageArrayOverride(): Map<Int, Int>
fun getNumberOfAyahsForSuraArray() : IntArray
fun getIsMakkiBySuraArray() : BooleanArray
fun getQuarterStartByPage() : IntArray
fun getQuartersArray() : Array<SuraAyah>
val numberOfPages: Int
val pageForSuraArray: IntArray
val suraForPageArray: IntArray
val ayahForPageArray: IntArray
val pageForJuzArray: IntArray
val juzDisplayPageArrayOverride: Map<Int, Int>
val numberOfAyahsForSuraArray: IntArray
val isMakkiBySuraArray: BooleanArray
val quarterStartByPage: IntArray
val quartersArray: Array<SuraAyah>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import com.quran.data.source.PageSizeCalculator
import com.quran.labs.androidquran.pages.madani.R

class MadaniPageProvider : PageProvider {
private val baseUrl = "https://android.quran.com/data"
private val dataSource = MadaniDataSource()
companion object {
private val baseUrl = "https://android.quran.com/data"
private val dataSource by lazy { MadaniDataSource() }
}

override fun getDataSource() = dataSource

Expand Down

0 comments on commit 5e28376

Please sign in to comment.