Skip to content

Commit

Permalink
NTV-189: Wrong bonus update pledge (#1407)
Browse files Browse the repository at this point in the history
(cherry picked from commit 31285ac)
  • Loading branch information
Arkariang committed Sep 24, 2021
1 parent 48e1545 commit 6c3390f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/com/kickstarter/libs/utils/NumberUtils.java
Expand Up @@ -113,6 +113,10 @@ public static double parse(final @NonNull String input) {
return parse(input, Locale.getDefault());
}

/**
* @deprecated Use instead com.kickstarter.libs.utils.extensions.StringExt#parseToDouble(java.lang.String)
*/
@Deprecated
public static double parse(final @NonNull String input, final @NonNull Locale locale) {
try {
return NumberFormat.getNumberInstance(locale).parse(input).doubleValue();
Expand Down
Expand Up @@ -58,3 +58,29 @@ fun String.wrapInParentheses() = "($this)"
fun String.parseHtmlTag(): String {
return Jsoup.parse(this).text()
}

/**
* Takes an optional String and returns a Double
* NOTE: NumberUtils.parse(String, Locale)
* - Depending on the Locale the decimal separator 0.99 or 0,99
* - Depending on the Locale the Character used for thousand separator can change 9.999 or 9,999
*
* We've compiled several Regex to account for use cases as not all the languages are listed as Default Locale
* as example Spanish or Polish, take a look at
* @see <a href="https://github.com/frohoff/jdk8u-jdk/blob/da0da73ab82ed714dc5be94acd2f0d00fbdfe2e9/src/share/classes/java/util/Locale.java#L484">Locale.java</a>
*
* The Strings will be modified to use "." as a decimal separator, and "" as the thousand separator
*
* - In case something wrong, it will return 0.0
*/
fun String?.parseToDouble(): Double {
val numToParse = this?.let { numToParse ->
return@let when {
"[0-9]+,[0-9]+".toRegex().matches(numToParse) -> numToParse.replace(",", ".")
"[0-9]+.[0-9]{3}".toRegex().matches(numToParse) -> numToParse.replace(".", "")
"[0-9]+.[0-9]{3},[0-9]+".toRegex().matches(numToParse) -> numToParse.replace(".", "").replace(",", ".")
else -> numToParse
}
}
return numToParse?.toDoubleOrNull() ?: 0.0
}
Expand Up @@ -27,6 +27,7 @@ import com.kickstarter.libs.utils.ProjectUtils
import com.kickstarter.libs.utils.ProjectViewUtils
import com.kickstarter.libs.utils.RefTagUtils
import com.kickstarter.libs.utils.RewardUtils
import com.kickstarter.libs.utils.extensions.parseToDouble
import com.kickstarter.models.Backing
import com.kickstarter.models.Checkout
import com.kickstarter.models.Project
Expand Down Expand Up @@ -697,7 +698,8 @@ interface PledgeFragmentViewModel {

val backingAmount = Observable.merge(backingAmountNR, backingAmountRW)

val pledgeInput = Observable.merge(initialAmount, this.pledgeInput.map { NumberUtils.parse(it) }, backingAmount)
val pledgeInput = Observable.merge(initialAmount, this.pledgeInput.map { it.parseToDouble() }, backingAmount)
.map { it }
.distinctUntilChanged()

pledgeInput
Expand Down Expand Up @@ -736,7 +738,7 @@ interface PledgeFragmentViewModel {
val bonusMinimum = Observable.just(0.0)
val bonusStepAmount = Observable.just(1.0)

val bonusInput = Observable.merge(bonusMinimum, this.bonusInput.map { NumberUtils.parse(it) })
val bonusInput = Observable.merge(bonusMinimum, this.bonusInput.map { it.parseToDouble() })

bonusMinimum
.map { NumberUtils.format(it.toInt()) }
Expand Down Expand Up @@ -1338,21 +1340,14 @@ interface PledgeFragmentViewModel {
.compose(bindToLifecycle())
.subscribe { this.pledgeButtonCTA.onNext(it) }

// Tracking
val projectAndTotal = project
.compose<Pair<Project, Double>>(combineLatestPair(total))

val projectAndTotalForInitialPledges = pledgeReason
.filter { it == PledgeReason.PLEDGE }
.compose<Pair<PledgeReason, Pair<Project, Double>>>(combineLatestPair(projectAndTotal))
.map { it.second }

val checkoutAndPledgeData =
Observable.combineLatest<Double, Double, String, CheckoutData>(
shippingAmountSelectedRw,
total,
this.bonusAmount
) { s, t, b -> checkoutData(s, t, NumberUtils.parse(b), null) }
) { s, t, b ->
checkoutData(s, t, b.parseToDouble(), null)
}
.compose<Pair<CheckoutData, PledgeData>>(combineLatestPair(pledgeData))

checkoutAndPledgeData
Expand Down
Expand Up @@ -122,6 +122,33 @@ class StringExtKtTest : KSRobolectricTestCase() {
assertEquals(EMPTY_STRING.wrapInParentheses(), "()")
}

@Test
fun parseStringToDouble() {
val number = "90"
val smallNumber = "0.3"

assertTrue(number.parseToDouble() == 90.0)
assertTrue(smallNumber.parseToDouble() == 0.3)

val notNumber: String? = null
assertTrue(notNumber.parseToDouble() == 0.0)

val alsoNotNumber = "Hola 9"
assertTrue(alsoNotNumber.parseToDouble() == 0.0)

assertEquals(0.0, "".parseToDouble())
assertEquals(1.0, "1".parseToDouble())
assertEquals(10.0, "10.0".parseToDouble())
assertEquals(1.5, "1,5".parseToDouble())
assertEquals(100.0, "100".parseToDouble())
assertEquals(100.5, "100,50".parseToDouble())
assertEquals(1000.0, "1000.0".parseToDouble())
assertEquals(1000.0, "1.000".parseToDouble())
assertEquals(1000.5, "1.000,50".parseToDouble())
assertEquals(10000.0, "10.000".parseToDouble())
assertEquals(10000.5, "10.000,50".parseToDouble())
}

companion object {
private const val VALID_EMAIL = "hello@kickstarter.com"
private const val INVALID_EMAIL = "hello@kickstarer"
Expand Down

0 comments on commit 6c3390f

Please sign in to comment.