From 2daba11e39364f7cd4c41c45ccc2db775d524e6d Mon Sep 17 00:00:00 2001 From: fabio-insolia-cko <123394131+fabio-insolia-cko@users.noreply.github.com> Date: Wed, 26 Jun 2024 14:04:09 +0100 Subject: [PATCH] feature/PIMOB-2639_Fix_for_phone_numeber_on_RTL_languages (#278) * 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 --- .../addresssummary/AddressSummaryViewModel.kt | 5 +++-- .../extensions/BillingAddressExtensions.kt | 17 +++++++++-------- .../AddressSummaryViewModelTest.kt | 11 +++++++++-- .../extensions/BillingAddressExtensionsTest.kt | 9 ++++++++- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/frames/src/main/java/com/checkout/frames/component/addresssummary/AddressSummaryViewModel.kt b/frames/src/main/java/com/checkout/frames/component/addresssummary/AddressSummaryViewModel.kt index 4ab187b23..6f5f38249 100644 --- a/frames/src/main/java/com/checkout/frames/component/addresssummary/AddressSummaryViewModel.kt +++ b/frames/src/main/java/com/checkout/frames/component/addresssummary/AddressSummaryViewModel.kt @@ -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 @@ -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 { "" } diff --git a/frames/src/main/java/com/checkout/frames/utils/extensions/BillingAddressExtensions.kt b/frames/src/main/java/com/checkout/frames/utils/extensions/BillingAddressExtensions.kt index 8aff57f3d..839ea387f 100644 --- a/frames/src/main/java/com/checkout/frames/utils/extensions/BillingAddressExtensions.kt +++ b/frames/src/main/java/com/checkout/frames/utils/extensions/BillingAddressExtensions.kt @@ -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 @@ -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") } } diff --git a/frames/src/test/java/com/checkout/frames/component/addresssummary/AddressSummaryViewModelTest.kt b/frames/src/test/java/com/checkout/frames/component/addresssummary/AddressSummaryViewModelTest.kt index 3306dcd1f..6cba87a01 100644 --- a/frames/src/test/java/com/checkout/frames/component/addresssummary/AddressSummaryViewModelTest.kt +++ b/frames/src/test/java/com/checkout/frames/component/addresssummary/AddressSummaryViewModelTest.kt @@ -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 @@ -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 @@ -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 @@ -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 @@ -145,7 +152,7 @@ internal class AddressSummaryViewModelTest { spyPaymentStateManager.isBillingAddressEnabled.value = false // When - viewModel.prepare() + viewModel.prepare(bidiFormatter) testScheduler.advanceUntilIdle() // Then diff --git a/frames/src/test/java/com/checkout/frames/utils/extensions/BillingAddressExtensionsTest.kt b/frames/src/test/java/com/checkout/frames/utils/extensions/BillingAddressExtensionsTest.kt index 021697ba1..44e1e17d9 100644 --- a/frames/src/test/java/com/checkout/frames/utils/extensions/BillingAddressExtensionsTest.kt +++ b/frames/src/test/java/com/checkout/frames/utils/extensions/BillingAddressExtensionsTest.kt @@ -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 @@ -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", ) @@ -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)