Fix crash when navigating back from the details screen before embedded images load

When navigating back coroutines are cancelled and a CancellationException is thrown.
This exception needs to be swallowed and doesn't need to be handled in a specific way.

MAILANDR-729
This commit is contained in:
Stefanija Boshkovska
2023-07-21 15:25:10 +02:00
parent 1582d716eb
commit 74fee462f6
3 changed files with 17 additions and 16 deletions
@@ -67,7 +67,6 @@ import com.google.accompanist.web.WebView
import com.google.accompanist.web.rememberWebViewStateWithHTMLData
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import me.proton.core.compose.component.ProtonSolidButton
import me.proton.core.compose.theme.ProtonDimens
import me.proton.core.compose.theme.ProtonTheme
@@ -128,9 +127,9 @@ internal fun MessageBodyWebView(
return if (!messageBodyUiModel.shouldShowRemoteContent && request?.isRemoteContent() == true) {
WebResourceResponse("", "", null)
} else if (messageBodyUiModel.shouldShowEmbeddedImages && request?.isEmbeddedImage() == true) {
runBlocking {
actions.loadEmbeddedImage(messageId, "<${request.url.schemeSpecificPart}>")
}?.let { WebResourceResponse(it.mimeType, "", ByteArrayInputStream(it.data)) }
actions.loadEmbeddedImage(messageId, "<${request.url.schemeSpecificPart}>")?.let {
WebResourceResponse(it.mimeType, "", ByteArrayInputStream(it.data))
}
} else {
super.shouldInterceptRequest(view, request)
}
@@ -264,7 +263,7 @@ object MessageBody {
val onMessageBodyLinkClicked: (uri: Uri) -> Unit,
val onShowAllAttachments: () -> Unit,
val onAttachmentClicked: (attachmentId: AttachmentId) -> Unit,
val loadEmbeddedImage: suspend (messageId: MessageId?, contentId: String) -> GetEmbeddedImageResult?
val loadEmbeddedImage: (messageId: MessageId?, contentId: String) -> GetEmbeddedImageResult?
)
}
@@ -349,7 +349,7 @@ object MessageDetailScreen {
val onAttachmentClicked: (attachmentId: AttachmentId) -> Unit,
val openAttachment: (values: OpenAttachmentIntentValues) -> Unit,
val showFeatureMissingSnackbar: () -> Unit,
val loadEmbeddedImage: suspend (contentId: String) -> GetEmbeddedImageResult?
val loadEmbeddedImage: (contentId: String) -> GetEmbeddedImageResult?
) {
companion object {
@@ -386,7 +386,7 @@ object MessageDetailContent {
val onShowAllAttachmentsClicked: () -> Unit,
val onAttachmentClicked: (attachmentId: AttachmentId) -> Unit,
val showFeatureMissingSnackbar: () -> Unit,
val loadEmbeddedImage: suspend (contentId: String) -> GetEmbeddedImageResult?
val loadEmbeddedImage: (contentId: String) -> GetEmbeddedImageResult?
)
}
@@ -39,13 +39,15 @@ class GetEmbeddedImageAvoidDuplicatedExecution @Inject constructor(
messageId: MessageId,
contentId: String,
coroutineContext: CoroutineContext
): GetEmbeddedImageResult? = withContext(coroutineContext) {
if (loadEmbeddedImageJobMap[contentId]?.isActive == true) {
loadEmbeddedImageJobMap[contentId]
} else {
async { getEmbeddedImage(userId, messageId, contentId).getOrNull() }.apply {
loadEmbeddedImageJobMap[contentId] = this
}
}?.await()
}
): GetEmbeddedImageResult? = runCatching {
withContext(coroutineContext) {
if (loadEmbeddedImageJobMap[contentId]?.isActive == true) {
loadEmbeddedImageJobMap[contentId]
} else {
async { getEmbeddedImage(userId, messageId, contentId).getOrNull() }.apply {
loadEmbeddedImageJobMap[contentId] = this
}
}?.await()
}
}.getOrNull()
}