Skip to content

Commit

Permalink
refactor: Use a callback for password field to avoid recomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Nov 29, 2024
1 parent 2ef05f1 commit 9aee3bb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private fun TransferDetailsScreen(
)
PasswordBottomSheet(
isVisible = { showPasswordBottomSheet },
transferPassword = getTransfer().password,
transferPassword = { getTransfer().password },
closeBottomSheet = { showPasswordBottomSheet = false },
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ import com.infomaniak.swisstransfer.ui.utils.copyText
import kotlinx.coroutines.launch

@Composable
fun PasswordBottomSheet(isVisible: () -> Boolean, transferPassword: String?, closeBottomSheet: () -> Unit) {
fun PasswordBottomSheet(isVisible: () -> Boolean, transferPassword: () -> String?, closeBottomSheet: () -> Unit) {

if (!isVisible() || transferPassword == null) return
val password = transferPassword()

if (!isVisible() || password == null) return

val context = LocalContext.current
val scope = rememberCoroutineScope()
Expand All @@ -57,7 +59,7 @@ fun PasswordBottomSheet(isVisible: () -> Boolean, transferPassword: String?, clo
imageVector = AppIcons.DocumentOnDocument,
onClick = {
context.copyText(
text = transferPassword,
text = password,
showSnackbar = { scope.launch { snackbarHostState.showSnackbar(it) } },
)
closeBottomSheet()
Expand All @@ -79,7 +81,7 @@ fun PasswordBottomSheet(isVisible: () -> Boolean, transferPassword: String?, clo
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = Margin.Medium),
initialValue = transferPassword,
initialValue = password,
isPassword = true,
isReadOnly = true,
)
Expand All @@ -93,7 +95,7 @@ private fun Preview() {
Surface {
PasswordBottomSheet(
isVisible = { true },
transferPassword = "toto42",
transferPassword = { "toto42" },
closeBottomSheet = {},
)
}
Expand Down

0 comments on commit 9aee3bb

Please sign in to comment.