Skip to content

Commit

Permalink
cleaning up code
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinSchildhorn committed Feb 9, 2024
1 parent e084533 commit 893e094
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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<ImageScreenState> = _uiState.asStateFlow()
private val jobs: MutableList<Job> = mutableListOf<Job>()

override fun setImageDirectories(directories: List<ImageDirectory>) {
_uiState.update { it.copy(imageDirectories = directories) }
Expand Down Expand Up @@ -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() {
Expand All @@ -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<ImageBitmap> ->
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) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -201,8 +203,8 @@ class DirectoryViewModel(
logger.i { "Updating Photos" }
val retrieveImagesUseCase: RetrieveImageUseCase = UseCaseFactory.retrieveImageUseCase
val imageDirectories: List<ImageDirectory> = imageUiState.value.imageDirectories
imageDirectories.forEachIndexed { index, imageDirectory ->
launch {
imageDirectories.mapIndexed{ index, imageDirectory ->
async {
retrieveImagesUseCase(
imageDirectory,
imageSize = 512, // TODO: Change
Expand All @@ -226,7 +228,9 @@ class DirectoryViewModel(
}
}
}
}
}.awaitAll()

// TODO: STORE LARGEST IMAGES
}
}

Expand Down

0 comments on commit 893e094

Please sign in to comment.