diff --git a/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/domain/image/RetrieveImageUseCase.kt b/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/domain/image/RetrieveImageUseCase.kt index 739f3265..cc2efaa8 100644 --- a/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/domain/image/RetrieveImageUseCase.kt +++ b/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/domain/image/RetrieveImageUseCase.kt @@ -20,11 +20,11 @@ class RetrieveImageUseCase( ): ImageBitmap? { val imageName = "\"${directory.details.fullPath}\"" logger.i { "Starting to get Image $imageName" } -/* + imageCacheDataSource.getImage(directory.details)?.let { logger.i { "$imageName found in cache, using that" } return it - }*/ + } logger.i { "Getting Image Bitmap from File ${directory.name}" } val imageBitmap: ImageBitmap? = directory.image?.getImageBitmap(imageSize) diff --git a/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/common/ImageViewModel.kt b/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/common/ImageViewModel.kt index 9e718b26..37f3400d 100644 --- a/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/common/ImageViewModel.kt +++ b/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/common/ImageViewModel.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import org.koin.core.component.KoinComponent import com.kevinschildhorn.fotopresenter.UseCaseFactory +import kotlinx.coroutines.cancelChildren interface ImageViewModel { var scope: CoroutineScope? @@ -41,7 +42,6 @@ class DefaultImageViewModel(private val logger: Logger? = null) : ImageViewModel private val _uiState = MutableStateFlow(ImageScreenState()) override var scope: CoroutineScope? = null override val imageUiState: StateFlow = _uiState.asStateFlow() - private val jobs: MutableList = mutableListOf() override fun setImageDirectories(directories: List) { _uiState.update { it.copy(imageDirectories = directories) } @@ -83,10 +83,7 @@ class DefaultImageViewModel(private val logger: Logger? = null) : ImageViewModel override fun cancelImageJobs() { logger?.d { "Cancelling Image Jobs" } - jobs.forEach { - it.cancel() - } - jobs.clear() + scope?.coroutineContext?.cancelChildren() } private fun updateSelectedImage() { @@ -106,14 +103,9 @@ class DefaultImageViewModel(private val logger: Logger? = null) : ImageViewModel scope?.launch(Dispatchers.Default) { val retrieveImagesUseCase = UseCaseFactory.retrieveImageUseCase logger?.d { "Retrieving Image" } - retrieveImagesUseCase(imageDirectory) { newState: State -> - logger?.d { "Image State Updated $newState" } - newState.value?.let { imageBitmap -> - _uiState.update { it.copy(selectedImage = imageBitmap) } - } - } - }?.let { - jobs.add(it) + + val imageBitmap = retrieveImagesUseCase(imageDirectory, imageSize = 1024) + _uiState.update { it.copy(selectedImage = imageBitmap) } } } } diff --git a/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/directory/DirectoryViewModel.kt b/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/directory/DirectoryViewModel.kt index 192adf01..0f19cdc9 100644 --- a/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/directory/DirectoryViewModel.kt +++ b/shared/src/commonMain/kotlin/com/kevinschildhorn/fotopresenter/ui/screens/directory/DirectoryViewModel.kt @@ -21,6 +21,8 @@ import com.kevinschildhorn.fotopresenter.ui.screens.common.ImageViewModel import com.kevinschildhorn.fotopresenter.ui.screens.playlist.PlaylistViewModel import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll import kotlinx.coroutines.cancelChildren import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -201,8 +203,8 @@ class DirectoryViewModel( logger.i { "Updating Photos" } val retrieveImagesUseCase: RetrieveImageUseCase = UseCaseFactory.retrieveImageUseCase val imageDirectories: List = imageUiState.value.imageDirectories - imageDirectories.forEachIndexed { index, imageDirectory -> - launch { + imageDirectories.mapIndexed{ index, imageDirectory -> + async { retrieveImagesUseCase( imageDirectory, imageSize = 512, // TODO: Change @@ -226,7 +228,9 @@ class DirectoryViewModel( } } } - } + }.awaitAll() + + // TODO: STORE LARGEST IMAGES } }