Skip to content

Commit

Permalink
Merge pull request #1977 from Adyen/fix/address_lookup_infinite_loading
Browse files Browse the repository at this point in the history
Address Lookup fix infinite loading
  • Loading branch information
OscarSpruit authored Jan 28, 2025
2 parents 1894d7e + 25010bc commit b36053b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.receiveAsFlow
import javax.inject.Inject

class AddressLookupRepository @Inject constructor(
Expand All @@ -37,18 +37,18 @@ class AddressLookupRepository @Inject constructor(
mockAddressLookupOptions = adapter.fromJson(json)?.options.orEmpty()
}

private val addressLookupQueryFlow = MutableStateFlow<String?>(null)
private val addressLookupQueryChannel = Channel<String>(Channel.BUFFERED)

@OptIn(FlowPreview::class)
val addressLookupOptionsFlow: Flow<List<LookupAddress>> = addressLookupQueryFlow
.filterNotNull()
val addressLookupOptionsFlow: Flow<List<LookupAddress>> = addressLookupQueryChannel
.receiveAsFlow()
.debounce(ADDRESS_LOOKUP_QUERY_DEBOUNCE_DURATION)
.map { query ->
queryAddressLookupOptions(query)
}

fun onQuery(query: String) {
addressLookupQueryFlow.tryEmit(query)
addressLookupQueryChannel.trySend(query)
}

suspend fun onAddressLookupCompleted(lookupAddress: LookupAddress): AddressLookupCompletionResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import com.adyen.checkout.components.core.AddressLookupCallback
import com.adyen.checkout.components.core.AddressLookupResult
import com.adyen.checkout.components.core.LookupAddress
import com.adyen.checkout.components.core.internal.ui.model.AddressInputModel
import com.adyen.checkout.ui.core.internal.ui.model.AddressLookupEvent
import com.adyen.checkout.ui.core.internal.ui.model.AddressLookupState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
Expand All @@ -25,7 +23,6 @@ interface AddressLookupDelegate {
val addressDelegate: AddressDelegate

val addressLookupStateFlow: Flow<AddressLookupState>
val addressLookupEventChannel: Channel<AddressLookupEvent>
val addressLookupSubmitFlow: Flow<AddressInputModel>
val addressLookupErrorPopupFlow: Flow<String?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class DefaultAddressLookupDelegate(
private val currentAddressLookupState
get() = mutableAddressLookupStateFlow.value

override val addressLookupEventChannel = bufferedChannel<AddressLookupEvent>()
private val addressLookupEventChannel = bufferedChannel<AddressLookupEvent>()
private val addressLookupEventFlow: Flow<AddressLookupEvent> = addressLookupEventChannel.receiveAsFlow()

override val addressOutputData: AddressOutputData
Expand Down

0 comments on commit b36053b

Please sign in to comment.