Skip to content

Commit

Permalink
[CORE] Sparkline optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
OHoussein committed Jun 10, 2024
1 parent 586bb75 commit 218983e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.ohoussein.cryptoapp.crypto.presentation.core

fun List<Double>.averageValues(maxValues: Int = 20): List<Double> {
if (size <= maxValues) {
return this
}

val groupSize = size / maxValues
val averagedPrices = mutableListOf<Double>()

for (i in indices step groupSize) {
val group = subList(i, minOf(i + groupSize, size))
val avgPrice = group.average()
averagedPrices.add(avgPrice)
}

return averagedPrices
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import dev.ohoussein.cryptoapp.core.formatter.PriceFormatter
import dev.ohoussein.cryptoapp.crypto.domain.model.CryptoDetailsModel
import dev.ohoussein.cryptoapp.crypto.domain.model.CryptoModel
import dev.ohoussein.cryptoapp.crypto.domain.model.Locale
import dev.ohoussein.cryptoapp.crypto.presentation.core.averageValues
import dev.ohoussein.cryptoapp.crypto.presentation.model.*
import dev.ohoussein.cryptoapp.designsystem.graph.model.GraphPoint

private const val SPARKLINE_7D_MAX_VALUES = 7 * 4
class DomainModelMapper(
private val priceFormatter: PriceFormatter,
private val percentFormatter: PercentFormatter,
Expand All @@ -31,7 +33,7 @@ class DomainModelMapper(
priceChangePercentIn24h = domain.priceChangePercentIn24h?.let {
LabelValue(it, percentFormatter(it / 100.0))
},
sparkline7d = domain.sparkLine7d?.mapIndexed { index, value ->
sparkline7d = domain.sparkLine7d?.averageValues(SPARKLINE_7D_MAX_VALUES)?.mapIndexed { index, value ->
GraphPoint(index.toDouble(), value)
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.ohoussein.cryptoapp.crypto.presentation.core

import kotlin.test.Test
import kotlin.test.assertEquals

class MathTest {

@Test
fun `Given values less than max value When call averageValues it it should return the same list`() {
val prices = listOf(100.0, 110.0, 105.0)
val result = prices.averageValues(maxValues = 20)
val expected = listOf(100.0, 110.0, 105.0)

assertEquals(expected, result)
}

@Test
fun `Given values When call averageValues it it should return the average values`() {
val prices = (1..40).map { it.toDouble() }
val result = prices.averageValues(maxValues = 20)
val expected = listOf(
1.5, 3.5, 5.5, 7.5, 9.5, 11.5, 13.5, 15.5, 17.5, 19.5,
21.5, 23.5, 25.5, 27.5, 29.5, 31.5, 33.5, 35.5, 37.5, 39.5
)

assertEquals(expected, result)
}

@Test
fun `Given empty values When call averageValues it it should return an empty list`() {
val prices = emptyList<Double>()
val result = prices.averageValues(maxValues = 20)
val expected = emptyList<Double>()

assertEquals(expected, result)
}
}

0 comments on commit 218983e

Please sign in to comment.