Skip to content

Commit

Permalink
adding temp code
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinSchildhorn committed Oct 11, 2024
1 parent 0e44daf commit 933fe8c
Show file tree
Hide file tree
Showing 20 changed files with 304 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import com.kevinschildhorn.fotopresenter.startKoin
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModelTwo
import com.kevinschildhorn.fotopresenter.ui.screens.login.LoginViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.PlaylistViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.slideshow.SlideshowViewModel
Expand All @@ -15,7 +16,7 @@ import org.koin.core.component.KoinComponent
class MainActivity : AppCompatActivity(), KoinComponent {

private val loginViewModel by viewModel<LoginViewModel>()
private val directoryViewModel by viewModel<DirectoryViewModel>()
private val directoryViewModel by viewModel<DirectoryViewModelTwo>()
private val slideshowViewModel by viewModel<SlideshowViewModel>()
private val playlistViewModel by viewModel<PlaylistViewModel>()

Expand Down
17 changes: 15 additions & 2 deletions desktopApp/src/jvmMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import androidx.compose.ui.window.application
import co.touchlab.kermit.Logger
import com.kevinschildhorn.fotopresenter.UseCaseFactory
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModelTwo
import com.kevinschildhorn.fotopresenter.ui.screens.login.LoginViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.PlaylistViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.slideshow.SlideshowViewModel
Expand All @@ -12,7 +13,19 @@ object KoinPurse {
val loginViewModel =
LoginViewModel(Logger.withTag("LoginViewModel"), UseCaseFactory.credentialsRepository)
val directoryViewModel =
DirectoryViewModel(UseCaseFactory.playlistRepository, Logger.withTag("DirectoryViewModel"))
DirectoryViewModel(
UseCaseFactory.playlistRepository,
Logger.withTag("DirectoryViewModel"),
UseCaseFactory.directoryRepository,
UseCaseFactory.directoryEtc,
)
val directoryViewModelTwo =
DirectoryViewModelTwo(
UseCaseFactory.playlistRepository,
Logger.withTag("DirectoryViewModel"),
UseCaseFactory.directoryRepository,
UseCaseFactory.directoryEtc,
)
val slideshowViewModel = SlideshowViewModel(Logger.withTag("SlideshowViewModel"))
val playlistViewModel =
PlaylistViewModel(UseCaseFactory.playlistRepository, Logger.withTag("PlaylistViewModel"))
Expand All @@ -25,7 +38,7 @@ fun main() = application {
) {
MainView(
KoinPurse.loginViewModel,
KoinPurse.directoryViewModel,
KoinPurse.directoryViewModelTwo,
KoinPurse.slideshowViewModel,
KoinPurse.playlistViewModel,
)
Expand Down
3 changes: 2 additions & 1 deletion shared/src/androidMain/kotlin/Main.android.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import androidx.compose.runtime.Composable
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModelTwo
import com.kevinschildhorn.fotopresenter.ui.screens.login.LoginViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.PlaylistViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.slideshow.SlideshowViewModel
Expand All @@ -9,7 +10,7 @@ actual fun getPlatformName(): String = "Android"
@Composable
fun MainView(
loginViewModel: LoginViewModel,
directoryViewModel: DirectoryViewModel,
directoryViewModel: DirectoryViewModelTwo,
slideshowViewModel: SlideshowViewModel,
playlistViewModel: PlaylistViewModel,
) = App(loginViewModel, directoryViewModel, slideshowViewModel, playlistViewModel)
5 changes: 3 additions & 2 deletions shared/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.kevinschildhorn.fotopresenter.ui.atoms.fotoShapes
import com.kevinschildhorn.fotopresenter.ui.screens.common.Screen
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryScreen
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModelTwo
import com.kevinschildhorn.fotopresenter.ui.screens.login.LoginScreen
import com.kevinschildhorn.fotopresenter.ui.screens.login.LoginViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.PlaylistScreen
Expand All @@ -25,7 +26,7 @@ import com.kevinschildhorn.fotopresenter.ui.screens.slideshow.SlideshowViewModel
@Composable
fun App(
loginViewModel: LoginViewModel,
directoryViewModel: DirectoryViewModel,
directoryViewModel: DirectoryViewModelTwo,
slideshowViewModel: SlideshowViewModel,
playlistViewModel: PlaylistViewModel,
navController: NavHostController = rememberNavController()
Expand Down Expand Up @@ -56,7 +57,7 @@ fun App(
},
onStartSlideshow = {
slideshowViewModel.setSlideshow(it)
directoryViewModel.clearSlideshow()
//directoryViewModel.clearSlideshow()
navController.navigate(Screen.SLIDESHOW.name)
},
onShowPlaylists = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.kevinschildhorn.fotopresenter

import co.touchlab.kermit.Logger
import co.touchlab.kermit.LoggerConfig
import com.kevinschildhorn.fotopresenter.data.DirectoryEtc
import com.kevinschildhorn.fotopresenter.data.datasources.CredentialsDataSource
import com.kevinschildhorn.fotopresenter.data.datasources.DirectoryDataSource
import com.kevinschildhorn.fotopresenter.data.datasources.ImageCacheDataSource
Expand All @@ -24,6 +25,7 @@ import com.kevinschildhorn.fotopresenter.domain.image.RetrieveImageUseCase
import com.kevinschildhorn.fotopresenter.domain.image.RetrieveSlideshowFromPlaylistUseCase
import com.kevinschildhorn.fotopresenter.domain.image.SaveMetadataForPathUseCase
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryViewModelTwo
import com.kevinschildhorn.fotopresenter.ui.screens.login.LoginViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.PlaylistViewModel
import com.kevinschildhorn.fotopresenter.ui.screens.slideshow.SlideshowViewModel
Expand All @@ -45,6 +47,8 @@ val commonModule =
single { CredentialsRepository(get()) }
single { DirectoryDataSource(get(), baseLogger.withTag("DirectoryDataSource")) }
single { DirectoryRepository(get(), get()) }
single { DirectoryEtc(get(), get(), baseLogger.withTag("DirectoryEtc")) }

single { ImageRemoteDataSource(get()) }
single { ImageRepository(get()) }
single { ImageCacheDataSource(get(), get(), baseLogger.withTag("ImageCacheDataSource")) }
Expand Down Expand Up @@ -83,7 +87,8 @@ val commonModule =
factory { SaveMetadataForPathUseCase(get()) }
// UI
single { LoginViewModel(baseLogger.withTag("LoginViewModel"), get()) }
single { DirectoryViewModel(get(), baseLogger.withTag("DirectoryViewModel")) }
single { DirectoryViewModel(get(), baseLogger.withTag("DirectoryViewModel"), get(), get()) }
single { DirectoryViewModelTwo(get(), baseLogger.withTag("DirectoryViewModel"), get(), get())}
single { SlideshowViewModel(baseLogger.withTag("SlideshowViewModel")) }
single { PlaylistViewModel(get(), baseLogger.withTag("PlaylistViewModel")) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kevinschildhorn.fotopresenter.data

import androidx.compose.ui.graphics.ImageBitmap
import com.kevinschildhorn.fotopresenter.data.network.NetworkDirectoryDetails
import com.kevinschildhorn.fotopresenter.ui.SortingType
import com.kevinschildhorn.fotopresenter.ui.shared.SharedImage
Expand Down Expand Up @@ -29,6 +30,7 @@ data class ImageDirectory(
override val details: NetworkDirectoryDetails,
val metaData: MetadataFileDetails?,
val image: SharedImage? = null,
val imageBitmap: ImageBitmap? = null,
) : Directory {
override fun toString(): String = "(I:${details.fullPath}:${details.id})"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.kevinschildhorn.fotopresenter.data

import co.touchlab.kermit.Logger
import com.kevinschildhorn.fotopresenter.data.repositories.DirectoryRepository
import com.kevinschildhorn.fotopresenter.data.repositories.ImageRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

class DirectoryEtc(
private val imageRepository: ImageRepository,
private val directoryRepository: DirectoryRepository,
private val logger: Logger,
) {

private val _directoryContentsFlow = MutableStateFlow<DirectoryContents>(DirectoryContents())
private val _imageDirectoryList = MutableStateFlow<List<ImageDirectory>>(emptyList())

val directoryFlow: Flow<DirectoryContents> =
combine(_directoryContentsFlow, _imageDirectoryList) { directory, images ->
directory.copy(images = images)
}

fun refreshDirectories(path: String, coroutineScope: CoroutineScope) {
coroutineScope.launch(Dispatchers.Default) {
// Retrieve File Data
var directoryContents = directoryRepository.getDirectoryContents(path)
_directoryContentsFlow.emit(directoryContents)
_imageDirectoryList.emit(directoryContents.images)

// For each image, get the Byte Array
val images = directoryContents
.images
//.take(10)
.toMutableList()

images.chunked(10).forEach { imageChunk ->
logger.i("Starting Jobs!")
runBlocking {
imageChunk.forEachIndexed { index, image ->
coroutineScope.launch {
processItemAsync(index, image)
}
}
}
logger.i("Joined!")
}

logger.i { "Finished!" }
}
}

suspend fun processItemAsync(
index: Int,
item: ImageDirectory,
) {
var mutableItemDirectory = item
val itemWithBytes = withContext(Dispatchers.IO) {
val sharedImage = imageRepository.getImage(item.details)
mutableItemDirectory.copy(image = sharedImage)
}
updateImageList(index, itemWithBytes)
val itemWithBitmap = withContext(Dispatchers.IO) {
val bitmap = itemWithBytes.image?.getImageBitmap(256)
itemWithBytes.copy(imageBitmap = bitmap)
}
updateImageList(index, itemWithBitmap)
item.image?.close()
}

private fun updateImageList(index: Int, item: ImageDirectory) {
val items = _imageDirectoryList.value.toMutableList()
items[index] = item
_imageDirectoryList.update { items }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ class DirectoryRepository(
private val directoryDataSource: DirectoryDataSource,
private val metadataDataSource: ImageMetadataDataSource,
) {
// Retrieves [NetworkDirectoryDetails] for folders and directories. Contains no bitmap data
suspend fun getDirectoryContents(path: String): DirectoryContents {
val folderDirectories: List<NetworkDirectoryDetails> =
directoryDataSource.getFolderDirectories(path)
val imageDirectories: List<NetworkDirectoryDetails> =
directoryDataSource.getImageDirectories(path)

val metaData = metadataDataSource.importMetaData()

//val metaData = metadataDataSource.importMetaData()

return DirectoryContents(
folders = folderDirectories.map { FolderDirectory(it) },
images = imageDirectories.map { networkDetails ->
ImageDirectory(
networkDetails,
metaData = metaData.files.find { networkDetails.fullPath == it.filePath }
metaData = null, //metaData.files.find { networkDetails.fullPath == it.filePath }
image = null,
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ import com.kevinschildhorn.fotopresenter.ui.shared.SharedImage
class ImageRepository(
private val remoteDataSource: ImageRemoteDataSource,
) {
// Gets Image ByteArray from a Remote Data Source
suspend fun getImage(directory: NetworkDirectoryDetails): SharedImage? = remoteDataSource.getImage(directory)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RetrieveImageUseCase(
private val logger: Logger,
) {

suspend operator fun invoke(
operator fun invoke(
directory: ImageDirectory,
imageSize: Int,
): ImageBitmap? {
Expand Down
Loading

0 comments on commit 933fe8c

Please sign in to comment.