diff --git a/app/src/main/java/org/thoughtcrime/securesms/onboarding/recoverypassword/RecoveryPasswordActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/onboarding/recoverypassword/RecoveryPasswordActivity.kt index 4c70cfe455..e57a93a519 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/onboarding/recoverypassword/RecoveryPasswordActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/onboarding/recoverypassword/RecoveryPasswordActivity.kt @@ -6,9 +6,6 @@ import android.graphics.Bitmap import android.os.Bundle import androidx.activity.viewModels import androidx.compose.animation.AnimatedVisibility -import androidx.compose.animation.Crossfade -import androidx.compose.animation.core.animateValue -import androidx.compose.animation.core.rememberInfiniteTransition import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.border @@ -17,7 +14,6 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer -import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width @@ -31,9 +27,11 @@ import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.MutableState +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.ColorFilter @@ -45,7 +43,7 @@ import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.unit.dp -import kotlinx.coroutines.Job +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import network.loki.messenger.R @@ -53,6 +51,7 @@ import org.thoughtcrime.securesms.BaseActionBarActivity import org.thoughtcrime.securesms.showSessionDialog import org.thoughtcrime.securesms.ui.AppTheme import org.thoughtcrime.securesms.ui.CellWithPaddingAndMargin +import org.thoughtcrime.securesms.ui.LaunchedEffectAsync import org.thoughtcrime.securesms.ui.LocalExtraColors import org.thoughtcrime.securesms.ui.OutlineButton import org.thoughtcrime.securesms.ui.PreviewTheme @@ -197,26 +196,23 @@ fun RecoveryPasswordCell(seed: String = "", qrBitmap: Bitmap? = null, copySeed:( AnimatedVisibility(!showQr.value) { Row(horizontalArrangement = Arrangement.spacedBy(32.dp)) { - val scope = rememberCoroutineScope() - val revertCopiedTextJob = remember { mutableStateOf(null) } - val copied = remember { mutableStateOf(false) } + var copied by remember { mutableStateOf(false) } + if (copied) LaunchedEffectAsync { + delay(2.seconds) + copied = false + } OutlineButton( modifier = Modifier.weight(1f), color = MaterialTheme.colors.onPrimary, onClick = { copySeed() - revertCopiedTextJob.value?.cancel() - revertCopiedTextJob.value = scope.launch { - copied.value = true - delay(2.seconds) - copied.value = false - } + copied = true } ) { - AnimatedVisibility(!copied.value) { + AnimatedVisibility(!copied) { Text(stringResource(R.string.copy)) } - AnimatedVisibility(copied.value) { + AnimatedVisibility(copied) { Text(stringResource(R.string.copied)) } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/ui/Components.kt b/app/src/main/java/org/thoughtcrime/securesms/ui/Components.kt index 262b2e6e59..8845c69629 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ui/Components.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/ui/Components.kt @@ -33,6 +33,8 @@ import androidx.compose.material.RadioButton import androidx.compose.material.Text import androidx.compose.material.TextButton import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha @@ -55,6 +57,8 @@ import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.compose.ui.viewinterop.AndroidView +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.launch import network.loki.messenger.R import org.session.libsession.utilities.recipients.Recipient import org.session.libsession.utilities.runIf @@ -428,3 +432,8 @@ fun RowScope.SessionShieldIcon() { .wrapContentSize(unbounded = true) ) } + +@Composable +fun LaunchedEffectAsync(block: suspend CoroutineScope.() -> Unit) { + rememberCoroutineScope().apply { LaunchedEffect(Unit) { launch { block() } } } +}