Kevin S
10/11/2024, 2:09 PMuli
10/11/2024, 2:37 PMKevin S
10/11/2024, 2:50 PMuli
10/11/2024, 2:52 PMuli
10/11/2024, 2:52 PMKevin S
10/11/2024, 2:53 PMuli
10/11/2024, 2:53 PMKevin S
10/11/2024, 2:55 PMuli
10/11/2024, 2:57 PMuli
10/11/2024, 2:58 PMKevin S
10/11/2024, 2:59 PMuli
10/11/2024, 3:00 PMKevin S
10/11/2024, 3:00 PMuli
10/11/2024, 3:06 PMuli
10/11/2024, 3:17 PMdata class ImageLoadingRequest(val fileName: String, val onLoad: (Image) -> Unit)
fun CoroutineScope.loadImagesAsync(request: Iterable<ImageLoadingRequest>): Job {
return launch {
request.asFlow().flatMapMerge(concurrency = DEFAULT_CONCURRENCY) { request ->
flow {
// Find the file
// Get the input stream
// download the image
// convert to Image representation
emit(Pair(convertedImage, request.onLoad))
}
}.collect { (image, onLoad) ->
withContext(Dispatchers.Main) {
onLoad(image)
}
}
}
}
Consider this a sketch.In the real world you will need nicer data structures, error handling, …uli
10/11/2024, 3:45 PMprivate fun updateImage(index: Int, loadedImage: Image) {
mutableViewState.update { viewState ->
val updatedImageList = viewState.images.mapIndexed { i, originalImage ->
if (i == index) loadedImage else originalImage
}
viewState.copy(images = updatedImageList)
}
}
val requests = filenames.mapIndexed { index, fileName ->
ImageLoadingRequest(fileName) { loadedImage ->
updateImage(index, loadedImage)
}
}
loadImagesAsync(requests)
uli
10/13/2024, 10:32 PMKevin S
10/13/2024, 10:33 PM