Skip to content

Commit

Permalink
Use TextFieldState in BasicTextField where applicable (#1201)
Browse files Browse the repository at this point in the history
Co-authored-by: p
  • Loading branch information
AntsyLich authored Sep 7, 2024
1 parent c4f235a commit bec549c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.clearText
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Close
import androidx.compose.material3.HorizontalDivider
Expand All @@ -28,11 +30,8 @@ import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.NonRestartableComposable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
Expand All @@ -43,7 +42,6 @@ import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -88,7 +86,7 @@ class SettingsSearchScreen : Screen() {
focusRequester.requestFocus()
}

var textFieldValue by rememberSaveable(stateSaver = TextFieldValue.Saver) { mutableStateOf(TextFieldValue()) }
val textFieldState = rememberTextFieldState()
Scaffold(
topBar = {
Column {
Expand All @@ -103,20 +101,19 @@ class SettingsSearchScreen : Screen() {
},
title = {
BasicTextField(
value = textFieldValue,
onValueChange = { textFieldValue = it },
state = textFieldState,
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester)
.runOnEnterKeyPressed(action = focusManager::clearFocus),
textStyle = MaterialTheme.typography.bodyLarge
.copy(color = MaterialTheme.colorScheme.onSurface),
singleLine = true,
lineLimits = TextFieldLineLimits.SingleLine,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
keyboardActions = KeyboardActions(onSearch = { focusManager.clearFocus() }),
onKeyboardAction = { focusManager.clearFocus() },
cursorBrush = SolidColor(MaterialTheme.colorScheme.primary),
decorationBox = {
if (textFieldValue.text.isEmpty()) {
decorator = {
if (textFieldState.text.isEmpty()) {
Text(
text = stringResource(MR.strings.action_search_settings),
color = MaterialTheme.colorScheme.onSurfaceVariant,
Expand All @@ -128,8 +125,8 @@ class SettingsSearchScreen : Screen() {
)
},
actions = {
if (textFieldValue.text.isNotEmpty()) {
IconButton(onClick = { textFieldValue = TextFieldValue() }) {
if (textFieldState.text.isNotEmpty()) {
IconButton(onClick = { textFieldState.clearText() }) {
Icon(
imageVector = Icons.Outlined.Close,
contentDescription = null,
Expand All @@ -144,7 +141,7 @@ class SettingsSearchScreen : Screen() {
},
) { contentPadding ->
SearchResult(
searchKey = textFieldValue.text,
searchKey = textFieldState.text.toString(),
listState = listState,
contentPadding = contentPadding,
) { result ->
Expand Down
23 changes: 11 additions & 12 deletions app/src/main/java/eu/kanade/presentation/track/TrackerSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.clearText
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
import androidx.compose.material.icons.filled.CheckCircle
Expand Down Expand Up @@ -59,7 +61,6 @@ import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.capitalize
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.intl.Locale
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.toLowerCase
Expand All @@ -84,8 +85,7 @@ import tachiyomi.presentation.core.util.secondaryItemAlpha

@Composable
fun TrackerSearch(
query: TextFieldValue,
onQueryChange: (TextFieldValue) -> Unit,
state: TextFieldState,
onDispatchQuery: () -> Unit,
queryResult: Result<List<TrackSearch>>?,
selected: TrackSearch?,
Expand Down Expand Up @@ -115,20 +115,19 @@ fun TrackerSearch(
},
title = {
BasicTextField(
value = query,
onValueChange = onQueryChange,
state = state,
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester)
.runOnEnterKeyPressed(action = dispatchQueryAndClearFocus),
textStyle = MaterialTheme.typography.bodyLarge
.copy(color = MaterialTheme.colorScheme.onSurface),
singleLine = true,
lineLimits = TextFieldLineLimits.SingleLine,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
keyboardActions = KeyboardActions(onSearch = { dispatchQueryAndClearFocus() }),
onKeyboardAction = { dispatchQueryAndClearFocus() },
cursorBrush = SolidColor(MaterialTheme.colorScheme.primary),
decorationBox = {
if (query.text.isEmpty()) {
decorator = {
if (state.text.isEmpty()) {
Text(
text = stringResource(MR.strings.action_search_hint),
color = MaterialTheme.colorScheme.onSurfaceVariant,
Expand All @@ -140,10 +139,10 @@ fun TrackerSearch(
)
},
actions = {
if (query.text.isNotEmpty()) {
if (state.text.isNotEmpty()) {
IconButton(
onClick = {
onQueryChange(TextFieldValue())
state.clearText()
focusRequester.requestFocus()
},
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.kanade.presentation.track

import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.runtime.Composable
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.tooling.preview.datasource.LoremIpsum
import eu.kanade.tachiyomi.data.track.model.TrackSearch
Expand All @@ -13,8 +13,7 @@ internal class TrackerSearchPreviewProvider : PreviewParameterProvider<@Composab
private val fullPageWithSecondSelected = @Composable {
val items = someTrackSearches().take(30).toList()
TrackerSearch(
query = TextFieldValue(text = "search text"),
onQueryChange = {},
state = TextFieldState(initialText = "search text"),
onDispatchQuery = {},
queryResult = Result.success(items),
selected = items[1],
Expand All @@ -25,8 +24,7 @@ internal class TrackerSearchPreviewProvider : PreviewParameterProvider<@Composab
}
private val fullPageWithoutSelected = @Composable {
TrackerSearch(
query = TextFieldValue(text = ""),
onQueryChange = {},
state = TextFieldState(),
onDispatchQuery = {},
queryResult = Result.success(someTrackSearches().take(30).toList()),
selected = null,
Expand All @@ -37,8 +35,7 @@ internal class TrackerSearchPreviewProvider : PreviewParameterProvider<@Composab
}
private val loading = @Composable {
TrackerSearch(
query = TextFieldValue(),
onQueryChange = {},
state = TextFieldState(),
onDispatchQuery = {},
queryResult = null,
selected = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.ButtonDefaults
Expand All @@ -28,7 +29,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import cafe.adriel.voyager.core.model.ScreenModel
import cafe.adriel.voyager.core.model.StateScreenModel
Expand Down Expand Up @@ -666,11 +666,10 @@ data class TrackerSearchScreen(

val state by screenModel.state.collectAsState()

var textFieldValue by remember { mutableStateOf(TextFieldValue(initialQuery)) }
val textFieldState = rememberTextFieldState(initialQuery)
TrackerSearch(
query = textFieldValue,
onQueryChange = { textFieldValue = it },
onDispatchQuery = { screenModel.trackingSearch(textFieldValue.text) },
state = textFieldState,
onDispatchQuery = { screenModel.trackingSearch(textFieldState.text.toString()) },
queryResult = state.queryResult,
selected = state.selected,
onSelectedChange = screenModel::updateSelection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -25,6 +27,7 @@ import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
Expand All @@ -37,7 +40,6 @@ import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -141,9 +143,9 @@ private fun <T> WheelPicker(

var showManualInput by remember { mutableStateOf(false) }
if (showManualInput) {
var value by remember {
val value = rememberSaveable(saver = TextFieldState.Saver) {
val currentString = items[internalIndex].toString()
mutableStateOf(TextFieldValue(text = currentString, selection = TextRange(currentString.length)))
TextFieldState(initialText = currentString, initialSelection = TextRange(currentString.length))
}

val scope = rememberCoroutineScope()
Expand All @@ -164,9 +166,8 @@ private fun <T> WheelPicker(
showManualInput = false
}
},
value = value,
onValueChange = { value = it },
singleLine = true,
state = value,
lineLimits = TextFieldLineLimits.SingleLine,
keyboardOptions = KeyboardOptions(
keyboardType = manualInputType!!,
imeAction = ImeAction.Done,
Expand Down

0 comments on commit bec549c

Please sign in to comment.