Skip to content

Commit

Permalink
Refactor rememberImageVideoPickerLauncher
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedRejeb committed Jul 25, 2024
1 parent 20460ab commit 65f6122
Showing 1 changed file with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.mohamedrejeb.calf.core.InternalCalfApi
import com.mohamedrejeb.calf.io.KmpFile
import kotlinx.cinterop.BetaInteropApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -149,11 +151,16 @@ private fun rememberImageVideoPickerLauncher(
didFinishPicking: List<*>,
) {
scope.launch {
val results = didFinishPicking.mapNotNull {
val result = it as? PHPickerResult ?: return@mapNotNull null
val results = didFinishPicking
.mapNotNull {
val result = it as? PHPickerResult ?: return@mapNotNull null

result.itemProvider.loadFileRepresentationForTypeIdentifierSuspend()
}
async {
result.itemProvider.loadFileRepresentationForTypeIdentifierSuspend()
}
}
.awaitAll()
.filterNotNull()

withContext(Dispatchers.Main) {
onResult(results)
Expand Down Expand Up @@ -188,29 +195,30 @@ private fun rememberImageVideoPickerLauncher(
}

@OptIn(InternalCalfApi::class)
private suspend fun NSItemProvider.loadFileRepresentationForTypeIdentifierSuspend(): KmpFile? = suspendCancellableCoroutine { continuation ->
val progress = loadFileRepresentationForTypeIdentifier(
typeIdentifier = registeredTypeIdentifiers.firstOrNull() as? String ?: UTTypeImage.identifier
) { url, error ->
if (error != null) {
continuation.resume(null)
return@loadFileRepresentationForTypeIdentifier
}

continuation.resume(
url?.createTempFile()?.let { tempUrl ->
KmpFile(
url = url,
tempUrl = tempUrl,
)
private suspend fun NSItemProvider.loadFileRepresentationForTypeIdentifierSuspend(): KmpFile? =
suspendCancellableCoroutine { continuation ->
val progress = loadFileRepresentationForTypeIdentifier(
typeIdentifier = registeredTypeIdentifiers.firstOrNull() as? String ?: UTTypeImage.identifier
) { url, error ->
if (error != null) {
continuation.resume(null)
return@loadFileRepresentationForTypeIdentifier
}
)
}

continuation.invokeOnCancellation {
progress.cancel()
continuation.resume(
url?.createTempFile()?.let { tempUrl ->
KmpFile(
url = url,
tempUrl = tempUrl,
)
}
)
}

continuation.invokeOnCancellation {
progress.cancel()
}
}
}

private fun createUIDocumentPickerViewController(
delegate: UIDocumentPickerDelegateProtocol,
Expand Down

0 comments on commit 65f6122

Please sign in to comment.