Skip to content

Commit

Permalink
feature/PIMOB-2639_Fix_for_phone_numeber_on_RTL_languages (#278)
Browse files Browse the repository at this point in the history
* feature/PIMOB-2639_Fix_for_phone_numeber_on_RTL_languages

* Fix for unit test

* Fix for unit test round 2

* Revert for vanniktech

* Nit for bidiFormatter

* Removing unused import
  • Loading branch information
fabio-insolia-cko authored Jun 26, 2024
1 parent ad6a6f8 commit 2daba11
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.checkout.frames.component.addresssummary

import android.text.BidiFormatter
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
Expand All @@ -26,13 +27,13 @@ internal class AddressSummaryViewModel @Inject constructor(
val componentState = provideState(style)
val componentStyle = provideViewStyle(style)

fun prepare() = viewModelScope.launch {
fun prepare(bidiFormatter: BidiFormatter = BidiFormatter.getInstance()) = viewModelScope.launch {
paymentStateManager.billingAddress.collect { billingAddress ->
componentState.addressPreviewState.text.value =
if (paymentStateManager.billingAddress.value.isEdited() &&
paymentStateManager.isBillingAddressEnabled.value
) {
billingAddress.summary()
billingAddress.summary(bidiFormatter)
} else {
""
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.checkout.frames.utils.extensions

import android.text.BidiFormatter
import android.text.TextDirectionHeuristics
import com.checkout.frames.screen.billingaddress.billingaddressdetails.models.BillingAddress
import java.util.Locale

internal fun BillingAddress.summary(): String {
internal fun BillingAddress.summary(bidiFormatter: BidiFormatter): String {
val strBuilder = StringBuilder()

// Full name
Expand All @@ -23,13 +25,12 @@ internal fun BillingAddress.summary(): String {
// Phone
this.phone?.let { phone ->
if (phone.number.isNotEmpty()) {
strBuilder.append(
if (phone.country?.dialingCode?.isNotEmpty() == true) {
"\n+${phone.country?.dialingCode} ${phone.number}"
} else {
"\n${phone.number}"
},
)
val phoneText = if (phone.country?.dialingCode?.isNotEmpty() == true) {
bidiFormatter.unicodeWrap("+${phone.country?.dialingCode} ${phone.number}", TextDirectionHeuristics.LTR)
} else {
bidiFormatter.unicodeWrap(phone.number, TextDirectionHeuristics.LTR)
}
strBuilder.append("\n$phoneText")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.checkout.frames.component.addresssummary

import android.annotation.SuppressLint
import android.text.BidiFormatter
import android.text.TextDirectionHeuristics
import com.checkout.base.mapper.Mapper
import com.checkout.base.model.Country
import com.checkout.frames.mapper.BillingFormAddressToBillingAddressMapper
Expand All @@ -25,8 +27,10 @@ import com.checkout.frames.style.view.InternalButtonViewStyle
import com.checkout.frames.style.view.addresssummary.AddressSummaryComponentViewStyle
import com.checkout.tokenization.model.Address
import com.checkout.tokenization.model.Phone
import io.mockk.every
import io.mockk.impl.annotations.SpyK
import io.mockk.junit5.MockKExtension
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand All @@ -46,6 +50,8 @@ import org.junit.jupiter.api.extension.ExtendWith
@ExtendWith(MockKExtension::class)
internal class AddressSummaryViewModelTest {

private val bidiFormatter: BidiFormatter = mockk()

@SpyK
private lateinit var spyBillingFormAddressToBillingAddressMapper: Mapper<BillingFormAddress?, BillingAddress>

Expand Down Expand Up @@ -125,11 +131,12 @@ internal class AddressSummaryViewModelTest {
),
phone = Phone("123", country),
)
every { bidiFormatter.unicodeWrap(any(), TextDirectionHeuristics.LTR) } returns "+44 123"
val expectedAddressPreview = "LINE 1\nLINE 2\nssdfsdf\nUnited Kingdom\n+44 123"
spyPaymentStateManager.billingAddress.value = testAddress

// When
viewModel.prepare()
viewModel.prepare(bidiFormatter)
testScheduler.advanceUntilIdle()

// Then
Expand All @@ -145,7 +152,7 @@ internal class AddressSummaryViewModelTest {
spyPaymentStateManager.isBillingAddressEnabled.value = false

// When
viewModel.prepare()
viewModel.prepare(bidiFormatter)
testScheduler.advanceUntilIdle()

// Then
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.checkout.frames.utils.extensions

import android.annotation.SuppressLint
import android.text.BidiFormatter
import android.text.TextDirectionHeuristics
import com.checkout.base.model.Country
import com.checkout.frames.screen.billingaddress.billingaddressdetails.models.BillingAddress
import com.checkout.tokenization.model.Address
import com.checkout.tokenization.model.Phone
import io.mockk.every
import io.mockk.junit5.MockKExtension
import io.mockk.mockk
import org.amshove.kluent.internal.assertEquals
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
Expand All @@ -17,6 +21,8 @@ import java.util.stream.Stream
@ExtendWith(MockKExtension::class)
internal class BillingAddressExtensionsTest {

private val bidiFormatter: BidiFormatter = mockk()

@ParameterizedTest(
name = "When summary of billing address {0} is requested then addressPreview {1} is provided",
)
Expand All @@ -26,7 +32,8 @@ internal class BillingAddressExtensionsTest {
expectedAddressPreview: String,
) {
// When
val result = billingAddress.summary()
every { bidiFormatter.unicodeWrap(any(), TextDirectionHeuristics.LTR) } returns "+44 123"
val result = billingAddress.summary(bidiFormatter)

// Then
assertEquals(expectedAddressPreview, result)
Expand Down

0 comments on commit 2daba11

Please sign in to comment.