Skip to content

Commit

Permalink
fix: self-deleting asset message timer [WPB-1807]
Browse files Browse the repository at this point in the history
  • Loading branch information
saleniuk committed Sep 25, 2024
1 parent 9db5fc7 commit a8a387a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ import androidx.lifecycle.repeatOnLifecycle
import com.wire.android.R
import com.wire.android.ui.home.conversations.model.ExpirationStatus
import com.wire.android.ui.home.conversations.model.UIMessage
import com.wire.android.ui.home.conversations.model.UIMessageContent
import com.wire.kalium.logic.data.asset.AssetTransferStatus
import com.wire.kalium.logic.data.asset.isSaved
import com.wire.kalium.logic.data.message.Message
import kotlinx.coroutines.delay
import kotlinx.datetime.Clock
Expand Down Expand Up @@ -250,62 +247,9 @@ class SelfDeletionTimerHelper(private val stringResourceProvider: StringResource
}

@Composable
fun startDeletionTimer(
message: UIMessage,
assetTransferStatus: AssetTransferStatus?,
onStartMessageSelfDeletion: (UIMessage) -> Unit
) {
if (assetTransferStatus != null) {
when (message.messageContent) {
is UIMessageContent.AssetMessage -> startAssetDeletion(
onSelfDeletingMessageRead = { onStartMessageSelfDeletion(message) },
transferStatus = assetTransferStatus
)

is UIMessageContent.AudioAssetMessage -> startAssetDeletion(
onSelfDeletingMessageRead = { onStartMessageSelfDeletion(message) },
transferStatus = assetTransferStatus
)

is UIMessageContent.ImageMessage -> startAssetDeletion(
onSelfDeletingMessageRead = { onStartMessageSelfDeletion(message) },
transferStatus = assetTransferStatus
)

else -> startRegularDeletion(message = message, onStartMessageSelfDeletion = onStartMessageSelfDeletion)
}
} else {
startRegularDeletion(message = message, onStartMessageSelfDeletion = onStartMessageSelfDeletion)
}
}

@Composable
private fun startAssetDeletion(onSelfDeletingMessageRead: () -> Unit, transferStatus: AssetTransferStatus) {
LaunchedEffect(transferStatus) {
if (transferStatus.isSaved()) {
onSelfDeletingMessageRead()
}
}
LaunchedEffect(key1 = timeLeft, key2 = transferStatus) {
if (transferStatus.isSaved()) {
if (timeLeft != ZERO) {
delay(updateInterval())
recalculateTimeLeft()
}
}
}
val lifecycleOwner = LocalLifecycleOwner.current
LaunchedEffect(lifecycleOwner) {
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
recalculateTimeLeft()
}
}
}

@Composable
private fun startRegularDeletion(message: UIMessage, onStartMessageSelfDeletion: (UIMessage) -> Unit) {
fun StartDeletionTimer(message: UIMessage, onSelfDeletingMessageRead: (UIMessage) -> Unit) {
LaunchedEffect(Unit) {
onStartMessageSelfDeletion(message)
onSelfDeletingMessageRead(message)
}
LaunchedEffect(timeLeft) {
if (timeLeft != ZERO) {
Expand All @@ -322,7 +266,7 @@ class SelfDeletionTimerHelper(private val stringResourceProvider: StringResource
}
}

object NotExpirable : SelfDeletionTimerState()
data object NotExpirable : SelfDeletionTimerState()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ fun MessageContainerItem(
!message.isPending &&
!message.sendingFailed
) {
selfDeletionTimerState.startDeletionTimer(
selfDeletionTimerState.StartDeletionTimer(
message = message,
assetTransferStatus = assetStatus,
onStartMessageSelfDeletion = onSelfDeletingMessageRead
onSelfDeletingMessageRead = onSelfDeletingMessageRead
)
}
Row(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fun RegularMessageItem(
Spacer(modifier = Modifier.height(dimensions().spacing4x))
}
if (selfDeletionTimerState is SelfDeletionTimerHelper.SelfDeletionTimerState.Expirable) {
MessageExpireLabel(messageContent, assetStatus, selfDeletionTimerState.timeLeftFormatted)
MessageExpireLabel(messageContent, selfDeletionTimerState.timeLeftFormatted)

// if the message is marked as deleted and is [SelfDeletionTimer.SelfDeletionTimerState.Expirable]
// the deletion responsibility belongs to the receiver, therefore we need to wait for the receiver
Expand Down Expand Up @@ -473,52 +473,16 @@ fun EphemeralMessageExpiredLabel(
}

@Composable
fun MessageExpireLabel(messageContent: UIMessageContent?, assetTransferStatus: AssetTransferStatus?, timeLeft: String) {
fun MessageExpireLabel(messageContent: UIMessageContent?, timeLeft: String) {
when (messageContent) {
is UIMessageContent.Location,
is UIMessageContent.AssetMessage,
is UIMessageContent.AudioAssetMessage,
is UIMessageContent.ImageMessage,
is UIMessageContent.TextMessage -> {
StatusBox(statusText = stringResource(R.string.self_deleting_message_time_left, timeLeft))
}

is UIMessageContent.AssetMessage -> {
StatusBox(
statusText = if (assetTransferStatus.isSaved()) {
stringResource(
R.string.self_deleting_message_time_left,
timeLeft
)
} else {
stringResource(R.string.self_deleting_message_label, timeLeft)
}
)
}

is UIMessageContent.AudioAssetMessage -> {
StatusBox(
statusText = if (assetTransferStatus.isSaved()) {
stringResource(
R.string.self_deleting_message_time_left,
timeLeft
)
} else {
stringResource(R.string.self_deleting_message_label, timeLeft)
}
)
}

is UIMessageContent.ImageMessage -> {
StatusBox(
statusText = if (assetTransferStatus.isSaved()) {
stringResource(
R.string.self_deleting_message_time_left,
timeLeft
)
} else {
stringResource(R.string.self_deleting_message_label, timeLeft)
}
)
}

is UIMessageContent.Deleted -> {
val context = LocalContext.current

Expand Down

0 comments on commit a8a387a

Please sign in to comment.