Change WithRememberedState to be a generic helper

This commit is contained in:
Wolf-Martell Montwé 2023-05-10 16:49:18 +02:00
parent 2756ceb694
commit d86210aedc
No known key found for this signature in database
GPG key ID: 6D45B21512ACBF72
2 changed files with 43 additions and 40 deletions

View file

@ -0,0 +1,15 @@
package app.k9mail.ui.catalog.helper
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
@Composable
internal fun <T> WithRememberedState(
input: T,
content: @Composable (state: MutableState<T>) -> Unit,
) {
val state = remember { mutableStateOf(input) }
content(state)
}

View file

@ -1,13 +1,10 @@
package app.k9mail.ui.catalog.items package app.k9mail.ui.catalog.items
import androidx.compose.foundation.lazy.grid.LazyGridScope import androidx.compose.foundation.lazy.grid.LazyGridScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import app.k9mail.core.ui.compose.designsystem.atom.textfield.TextFieldOutlined import app.k9mail.core.ui.compose.designsystem.atom.textfield.TextFieldOutlined
import app.k9mail.core.ui.compose.designsystem.atom.textfield.TextFieldOutlinedEmailAddress import app.k9mail.core.ui.compose.designsystem.atom.textfield.TextFieldOutlinedEmailAddress
import app.k9mail.core.ui.compose.designsystem.atom.textfield.TextFieldOutlinedPassword import app.k9mail.core.ui.compose.designsystem.atom.textfield.TextFieldOutlinedPassword
import app.k9mail.ui.catalog.helper.WithRememberedState
fun LazyGridScope.textFieldItems() { fun LazyGridScope.textFieldItems() {
sectionHeaderItem(text = "Text fields") sectionHeaderItem(text = "Text fields")
@ -19,30 +16,30 @@ fun LazyGridScope.textFieldItems() {
private fun LazyGridScope.textFieldOutlinedItems() { private fun LazyGridScope.textFieldOutlinedItems() {
sectionSubtitleItem(text = "Outlined") sectionSubtitleItem(text = "Outlined")
item { item {
WithRememberedInput(text = "Initial text") { input -> WithRememberedState(input = "Initial text") { state ->
TextFieldOutlined( TextFieldOutlined(
value = input.value, value = state.value,
label = "Label", label = "Label",
onValueChange = { input.value = it }, onValueChange = { state.value = it },
) )
} }
} }
item { item {
WithRememberedInput(text = "Input text with error") { input -> WithRememberedState(input = "Input text with error") { state ->
TextFieldOutlined( TextFieldOutlined(
value = input.value, value = state.value,
label = "Label", label = "Label",
onValueChange = { input.value = it }, onValueChange = { state.value = it },
isError = true, isError = true,
) )
} }
} }
item { item {
WithRememberedInput(text = "Input text disabled") { input -> WithRememberedState(input = "Input text disabled") { state ->
TextFieldOutlined( TextFieldOutlined(
value = input.value, value = state.value,
label = "Label", label = "Label",
onValueChange = { input.value = it }, onValueChange = { state.value = it },
enabled = false, enabled = false,
) )
} }
@ -52,30 +49,30 @@ private fun LazyGridScope.textFieldOutlinedItems() {
private fun LazyGridScope.passwordTextFieldOutlinedItems() { private fun LazyGridScope.passwordTextFieldOutlinedItems() {
sectionSubtitleItem(text = "Password outlined") sectionSubtitleItem(text = "Password outlined")
item { item {
WithRememberedInput(text = "") { input -> WithRememberedState(input = "") { state ->
TextFieldOutlinedPassword( TextFieldOutlinedPassword(
value = input.value, value = state.value,
label = "Password", label = "Password",
onValueChange = { input.value = it }, onValueChange = { state.value = it },
) )
} }
} }
item { item {
WithRememberedInput(text = "Password") { input -> WithRememberedState(input = "Password") { state ->
TextFieldOutlinedPassword( TextFieldOutlinedPassword(
value = input.value, value = state.value,
label = "Password with error", label = "Password with error",
onValueChange = { input.value = it }, onValueChange = { state.value = it },
isError = true, isError = true,
) )
} }
} }
item { item {
WithRememberedInput(text = "Password disabled") { input -> WithRememberedState(input = "Password disabled") { state ->
TextFieldOutlinedPassword( TextFieldOutlinedPassword(
value = input.value, value = state.value,
label = "Password", label = "Password",
onValueChange = { input.value = it }, onValueChange = { state.value = it },
enabled = false, enabled = false,
) )
} }
@ -85,41 +82,32 @@ private fun LazyGridScope.passwordTextFieldOutlinedItems() {
private fun LazyGridScope.emailTextFieldOutlinedItems() { private fun LazyGridScope.emailTextFieldOutlinedItems() {
sectionSubtitleItem(text = "Email outlined") sectionSubtitleItem(text = "Email outlined")
item { item {
WithRememberedInput(text = "") { input -> WithRememberedState(input = "") { state ->
TextFieldOutlinedEmailAddress( TextFieldOutlinedEmailAddress(
value = input.value, value = state.value,
label = "Email address", label = "Email address",
onValueChange = { input.value = it }, onValueChange = { state.value = it },
) )
} }
} }
item { item {
WithRememberedInput(text = "email@example.com") { input -> WithRememberedState(input = "email@example.com") { state ->
TextFieldOutlinedEmailAddress( TextFieldOutlinedEmailAddress(
value = input.value, value = state.value,
label = "Email address with error", label = "Email address with error",
onValueChange = { input.value = it }, onValueChange = { state.value = it },
isError = true, isError = true,
) )
} }
} }
item { item {
WithRememberedInput(text = "email@example.com disabled") { input -> WithRememberedState(input = "email@example.com disabled") { state ->
TextFieldOutlinedEmailAddress( TextFieldOutlinedEmailAddress(
value = input.value, value = state.value,
label = "Email address", label = "Email address",
onValueChange = { input.value = it }, onValueChange = { state.value = it },
enabled = false, enabled = false,
) )
} }
} }
} }
@Composable
private fun WithRememberedInput(
text: String,
content: @Composable (text: MutableState<String>) -> Unit,
) {
val inputText = remember { mutableStateOf(text) }
content(inputText)
}