Skip to content

Instantly share code, notes, and snippets.

@saurabharora90
Last active November 5, 2023 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saurabharora90/e3aba869495c4349166a7dd304c40e86 to your computer and use it in GitHub Desktop.
Save saurabharora90/e3aba869495c4349166a7dd304c40e86 to your computer and use it in GitHub Desktop.
second approach with state object as key
@Composable
private fun PostHashtags(
hashtags: ImmutableList<String>,
onAddHashTag: (String) -> Unit,
modifier: Modifier
) {
- var inputHashTag by remember(hashtags) { mutableStateOf("") }
+ val inputHashTag = remember(hashtags) { mutableStateOf("") }
- val isValidHashtag: Boolean by remember {
+ val isValidHashtag: Boolean by remember(inputHashTag) {
derivedStateOf {
- !inputHashTag.contains(" ")
+ !inputHashTag.value.contains(" ")
}
}
Column(modifier = modifier) {
OutlinedTextField(
- value = inputHashTag,
+ value = inputHashTag.value,
- onValueChange = { inputHashTag = it },
+ onValueChange = { inputHashTag.value = it },
leadingIcon = { Text(text = "#", fontWeight = FontWeight.Bold) },
placeholder = { Text(text = "Type a hashtag here") },
trailingIcon = {
AnimatedVisibility(
- visible = inputHashTag.isNotEmpty() && isValidHashtag,
+ visible = inputHashTag.value.isNotEmpty() && isValidHashtag,
enter = fadeIn(),
exit = fadeOut()
) {
- IconButton(onClick = { onAddHashTag(inputHashTag) }) {
+ IconButton(onClick = { onAddHashTag(inputHashTag.value) }) {
Icon(imageVector = Icons.Default.AddCircle,
contentDescription = "Add hashtag")
}
}
},
isError = !isValidHashtag,
)
AnimatedVisibility(visible = !isValidHashtag) {
Text(
text = "Hashtag cannot have a space",
style = MaterialTheme.typography.labelSmall,
modifier = Modifier.padding(vertical = 2.dp)
)
}
ReusableVerticalLazyList(content = hashtags)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment