Moves to using cancellable coroutines to avoid cancelling the parent group all together.

This commit is contained in:
Vitor Pamplona 2024-12-31 16:20:10 -05:00
parent 160d4722c0
commit a09b8c5862
3 changed files with 8 additions and 8 deletions

View File

@ -23,10 +23,10 @@ package com.vitorpamplona.amethyst
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeoutOrNull
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
/**
* Launches an async coroutine for each item, runs the
@ -59,7 +59,7 @@ suspend inline fun <T> tryAndWait(
crossinline asyncFunc: (Continuation<T>) -> Unit,
): T? =
withTimeoutOrNull(timeoutMillis) {
suspendCoroutine { continuation ->
suspendCancellableCoroutine { continuation ->
asyncFunc(continuation)
}
}

View File

@ -35,11 +35,11 @@ import com.vitorpamplona.amethyst.ui.components.util.MediaCompressorFileUtils
import id.zelory.compressor.Compressor
import id.zelory.compressor.constraint.default
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeoutOrNull
import java.io.File
import java.util.UUID
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
class MediaCompressorResult(
val uri: Uri,
@ -94,7 +94,7 @@ class MediaCompressor {
val result =
withTimeoutOrNull(30000) {
suspendCoroutine { continuation ->
suspendCancellableCoroutine { continuation ->
VideoCompressor.start(
// => This is required
context = applicationContext,

View File

@ -44,9 +44,9 @@ import kotlinx.collections.immutable.ImmutableList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeoutOrNull
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@Stable
open class NewMediaModel : ViewModel() {
@ -138,7 +138,7 @@ open class NewMediaModel : ViewModel() {
// upload each file as an individual nip95 event.
viewModelScope.launch(Dispatchers.IO) {
withTimeoutOrNull(30000) {
suspendCoroutine { continuation ->
suspendCancellableCoroutine { continuation ->
account?.createNip95(it.bytes, headerInfo = it.fileHeader, caption, sensitiveContent) { nip95 ->
account?.consumeAndSendNip95(nip95.first, nip95.second, relayList)
continuation.resume(true)
@ -153,7 +153,7 @@ open class NewMediaModel : ViewModel() {
// upload each file as an individual nip95 event.
viewModelScope.launch(Dispatchers.IO) {
withTimeoutOrNull(30000) {
suspendCoroutine { continuation ->
suspendCancellableCoroutine { continuation ->
account?.sendHeader(
it.url,
it.magnet,
@ -174,7 +174,7 @@ open class NewMediaModel : ViewModel() {
listOf(
viewModelScope.launch(Dispatchers.IO) {
withTimeoutOrNull(30000) {
suspendCoroutine { continuation ->
suspendCancellableCoroutine { continuation ->
account?.sendAllAsOnePictureEvent(
imageUrls,
caption,