Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address Lookup fix infinite loading #1977

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading