Skip to content
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 @@ -145,6 +145,7 @@ internal class InlineAutocompleteController(
limit = AutocompleteViewModel.MAX_DISPLAYED_RESULTS,
)
currentCoroutineContext().ensureActive()
if (_inlinePredictionsState.value == AutocompleteAddressInteractor.InlinePredictionsState.Idle) return
Comment on lines 147 to +148

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc, we call ensureActive() somewhere else. Should we combine these two check and make it a helper? Ignore this comment if that is not true.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they serve different purposes, one checks selectionJob cancellation (no Idle guard needed), line 147 checks collectLatest cancellation + the Idle-state race guard so they can't be merged

result.fold(
onSuccess = { handleFindPredictionsSuccess(query, it) },
onFailure = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ package com.stripe.android.uicore.elements

import androidx.annotation.RestrictTo
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import com.stripe.android.core.strings.ResolvableString
import com.stripe.android.uicore.R
import com.stripe.android.uicore.forms.FormFieldEntry
Expand Down Expand Up @@ -76,19 +68,12 @@ class AddressTextFieldController(
lastTextFieldIdentifier: IdentifierSpec?
) {
if (inlineAutocompleteHandler != null) {
var fieldWidthDp by remember { mutableStateOf(0.dp) }
val density = LocalDensity.current

val onClear = remember { { _inlineQuery.value = "" } }
val onClear = {
_inlineQuery.value = ""
inlineAutocompleteHandler.onDismissed()
}

Box(
modifier = modifier
.wrapContentSize(Alignment.TopStart)
.onSizeChanged { size ->
val newWidth = with(density) { size.width.toDp() }
if (newWidth != fieldWidthDp) fieldWidthDp = newWidth
}
) {
Column(modifier = modifier) {
AddressTextFieldUI(
controller = this@AddressTextFieldController,
enabled = enabled,
Expand All @@ -100,9 +85,7 @@ class AddressTextFieldController(
state = predictionsState,
attributionDrawable = inlineAutocompleteHandler
.getAttributionDrawable(isDarkTheme),
fieldWidthDp = fieldWidthDp,
onPredictionSelected = inlineAutocompleteHandler::onPredictionSelected,
onDismiss = inlineAutocompleteHandler::onDismissed,
onClear = onClear,
onEnterManually = inlineAutocompleteHandler::onEnterManually,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.Card
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Divider
import androidx.compose.material.DropdownMenu
import androidx.compose.material.LocalContentColor
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
Expand All @@ -25,10 +25,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.PopupProperties
import com.stripe.android.uicore.R
import com.stripe.android.uicore.stripeColors
import com.stripe.android.uicore.text.annotatedStringResource
Expand All @@ -38,22 +35,19 @@ import com.stripe.android.uicore.text.annotatedStringResource
fun InlineAddressPredictionsUI(
state: AutocompleteAddressInteractor.InlinePredictionsState,
attributionDrawable: Int?,
fieldWidthDp: Dp,
onPredictionSelected: (String) -> Unit,
onDismiss: () -> Unit,
onClear: () -> Unit,
onEnterManually: (() -> Unit)? = null,
onEnterManually: (() -> Unit)?,
) {
DropdownMenu(
expanded = shouldShowPredictionsDropdown(state),
onDismissRequest = onDismiss,
offset = DpOffset(x = (-1).dp, y = 0.dp),
modifier = if (fieldWidthDp > 0.dp) {
Modifier.width(fieldWidthDp + 2.dp)
} else {
Modifier.fillMaxWidth()
},
properties = PopupProperties(focusable = false),
if (!shouldShowPredictionsDropdown(state)) {
return
}

Card(
elevation = 4.dp,
modifier = Modifier
.fillMaxWidth()
.padding(top = 4.dp),
) {
InlineAddressPredictionsContent(
state = state,
Expand Down
Loading