Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add photo to slideshow #43

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import com.kevinschildhorn.fotopresenter.Playlist
import com.kevinschildhorn.fotopresenter.data.PlaylistDetails
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.composables.PlaylistColumn
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.composables.PlaylistOverlay
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.composables.PlaylistScreenCreateRow
Expand All @@ -26,8 +27,8 @@ import com.kevinschildhorn.fotopresenter.ui.screens.playlist.composables.Playlis
@Preview
fun PlaylistRowsPreview() {
Column(modifier = Modifier.fillMaxSize()) {
PlaylistScreenPlaylistRow("Playlist 1", onClick = {}, onDelete = {}, onEdit = {})
PlaylistScreenPlaylistRow("Playlist 2", onClick = {}, onDelete = {}, onEdit = {})
PlaylistScreenPlaylistRow("Playlist 1", {},{}, {}, {})
PlaylistScreenPlaylistRow("Playlist 2", {}, {}, {}, {})
PlaylistScreenCreateRow(onClick = {})
}
}
Expand All @@ -36,7 +37,8 @@ fun PlaylistRowsPreview() {
@Preview
fun PlaylistColumnPreview() {
PlaylistColumn(
listOf(Playlist(1,"Playlist 1"), Playlist(2, "Playlist 2")),
listOf(PlaylistDetails(1,"Playlist 1"), PlaylistDetails(2, "Playlist 2")),
{},
{},
{},
{},
Expand All @@ -57,11 +59,12 @@ fun PlaylistOverlayPreview() {
Text("Toggle")
}
PlaylistOverlay(
listOf(Playlist(1,"Playlist 1"), Playlist(2, "Playlist 2")),
listOf(PlaylistDetails(1,"Playlist 1"), PlaylistDetails(2, "Playlist 2")),
{},
{},
{},
{},
{},
{}
)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ val commonModule =
single { ImageRemoteDataSource(get()) }
single { ImageRepository(get()) }
single { ImageCacheDataSource(get()) }
single { PlaylistDataSource(get()) }
single { PlaylistDataSource(get(), baseLogger.withTag("PlaylistDataSource")) }
single { PlaylistRepository(get()) }

// Domain
Expand All @@ -62,7 +62,7 @@ val commonModule =

// UI
single { LoginViewModel(baseLogger.withTag("LoginViewModel"), get()) }
single { DirectoryViewModel(baseLogger.withTag("DirectoryViewModel")) }
single { DirectoryViewModel(get(),baseLogger.withTag("DirectoryViewModel")) }
single { SlideshowViewModel(baseLogger.withTag("SlideshowViewModel")) }
single { PlaylistViewModel(get(),baseLogger.withTag("PlaylistViewModel")) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.kevinschildhorn.fotopresenter.data

import com.kevinschildhorn.fotopresenter.PlaylistImage
import com.kevinschildhorn.fotopresenter.data.network.DefaultNetworkDirectoryDetails

data class PlaylistDetails(
val id: Long,
val name: String,
val images: List<PlaylistImage> = emptyList(),
){
val asImageSlideshowDetails:ImageSlideshowDetails
get() = ImageSlideshowDetails(
directories = images.map {
ImageDirectory(
DefaultNetworkDirectoryDetails(
id = it.directory_id.toInt(),
fullPath = it.directory_path
)
)
}
)
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package com.kevinschildhorn.fotopresenter.data.datasources

import app.cash.sqldelight.db.SqlDriver
import co.touchlab.kermit.Logger
import com.kevinschildhorn.fotopresenter.Playlist
import com.kevinschildhorn.fotopresenter.PlaylistDatabase
import com.kevinschildhorn.fotopresenter.PlaylistImage
import com.kevinschildhorn.fotopresenter.data.ImageDirectory
import com.kevinschildhorn.fotopresenter.data.PlaylistDetails
import org.koin.core.component.KoinComponent

class PlaylistDataSource(
driver: SqlDriver,
private val logger: Logger? = null,
) : KoinComponent {

private val database = PlaylistDatabase(driver)

fun createPlaylist(name: String, directories: List<ImageDirectory> = emptyList()): Playlist? {
return try {
//logger?.i { "Creating Playlist $name with images: ${directories.count()}" }
logger?.i { "Creating Playlist $name with images: ${directories.count()}" }
database.playlistQueries.insertPlaylist(name)
val playlist = database.playlistQueries.selectPlaylistByName(name).executeAsOne()
//logger?.i { "Playlist Created, now adding images" }
logger?.i { "Playlist Created, now adding images" }

directories.forEach {
insertPlaylistImage(playlistId = playlist.id, directory = it)
Expand All @@ -29,30 +32,34 @@ class PlaylistDataSource(
}
}

fun getAllPlaylists(): List<Playlist> {
fun getAllPlaylists(): List<PlaylistDetails> {
return try {
database.playlistQueries.selectAllPlaylists().executeAsList()
database.playlistQueries.selectAllPlaylists().executeAsList().map {
val images =
database.imageDirectoryQueries.selectPlaylistImages(it.id).executeAsList()
PlaylistDetails(it.id,it.name, images)
}
} catch (e: Exception) {
emptyList()
}

}

fun getPlaylistByName(name: String): Playlist? {
fun getPlaylistByName(name: String): PlaylistDetails? {
return try {
//logger?.i { "Selecting playlist by name $name" }
logger?.i { "Selecting playlist by name $name" }
val playList: Playlist =
database.playlistQueries.selectPlaylistByName(name).executeAsOne()
//logger?.i { "Retrieved Playlist!" }
playList
val images =
database.imageDirectoryQueries.selectPlaylistImages(playList.id).executeAsList()
logger?.i { "Retrieved Playlist!" }
PlaylistDetails(playList.id, playList.name, images)
} catch (e: Exception) {
null
}
}

fun insertPlaylistImage(playlistId: Long, directory: ImageDirectory): PlaylistImage? {
//logger?.i { "Inserting Playlist Image ${directory.name}" }

logger?.i { "Inserting Playlist Image ${directory.name}" }
database.imageDirectoryQueries.insertPlaylistImage(
playlist_id = playlistId,
directory_path = directory.details.fullPath,
Expand All @@ -63,11 +70,11 @@ class PlaylistDataSource(

fun getPlaylistImage(playlistId: Long, directoryPath: String): PlaylistImage? {
return try {
//logger?.i { "Selecting Playlist Image $playlistId" }
logger?.i { "Selecting Playlist Image $playlistId" }
val image: PlaylistImage =
database.imageDirectoryQueries.selectPlaylistImage(playlistId, directoryPath)
.executeAsOne()
//logger?.i { "Selecting Playlist Image" }
logger?.i { "Selecting Playlist Image" }
image
} catch (e: Exception) {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface NetworkDirectoryDetails {
fileExtension == "bmp"
}

class MockNetworkDirectoryDetails(
class DefaultNetworkDirectoryDetails(
override val fullPath: String,
override val id: Int,
) : NetworkDirectoryDetails
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ interface NetworkHandler {

suspend fun disconnect()

suspend fun getDirectoryDetails(path: String): NetworkDirectoryDetails?

suspend fun getDirectoryContents(path: String): List<NetworkDirectoryDetails>

suspend fun openDirectory(path: String): String?
Expand All @@ -25,4 +27,5 @@ class NetworkHandlerException : Exception {
enum class NetworkHandlerError(val message: String) {
NOT_CONNECTED("The Network Handler is not Connected"),
DIRECTORY_NOT_FOUND("The Directory you selected was not found"),
FILE_NOT_FOUND("The File you selected was not found"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.kevinschildhorn.fotopresenter.data.repositories
import com.kevinschildhorn.fotopresenter.Playlist
import com.kevinschildhorn.fotopresenter.PlaylistImage
import com.kevinschildhorn.fotopresenter.data.ImageDirectory
import com.kevinschildhorn.fotopresenter.data.PlaylistDetails
import com.kevinschildhorn.fotopresenter.data.datasources.PlaylistDataSource

class PlaylistRepository(
Expand All @@ -12,10 +13,10 @@ class PlaylistRepository(
fun createPlaylist(name: String, directories: List<ImageDirectory> = emptyList()): Playlist? =
playlistDataSource.createPlaylist(name, directories)

fun getAllPlaylists(): List<Playlist> =
fun getAllPlaylists(): List<PlaylistDetails> =
playlistDataSource.getAllPlaylists()

fun getPlaylistByName(name: String): Playlist? =
fun getPlaylistByName(name: String): PlaylistDetails? =
playlistDataSource.getPlaylistByName(name)

fun insertPlaylistImage(playlistId: Long, directory: ImageDirectory): PlaylistImage? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.kevinschildhorn.fotopresenter.ui.screens.common

enum class ActionSheetAction(val title: String) {
START_SLIDESHOW("Start A Slideshow"),
ADD_STATIC_LOCATION("Add to a Playlist"),
NONE("Nothing"), // TEMP
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ data class ImageScreenState(
val imageDirectories: List<ImageDirectory> = emptyList(),
val selectedImageIndex: Int? = null,
val selectedImage: ImageBitmap? = null,
)
) {
val selectedImageDirectory: ImageDirectory?
get() =
selectedImageIndex?.let {
imageDirectories.getOrNull(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class DefaultImageViewModel(private val logger: Logger? = null) : ImageViewModel
}

override fun setSelectedImage(index: Int?) {
logger?.i { "Setting selected image to $index" }
_uiState.update { it.copy(selectedImageIndex = index) }
updateSelectedImage()
}
Expand All @@ -80,16 +81,12 @@ class DefaultImageViewModel(private val logger: Logger? = null) : ImageViewModel

private fun updateSelectedImage() {
logger?.i { "Updating Selected Index" }

with(imageUiState.value) {
selectedImageIndex?.let { index ->
logger?.d { "Selected Image Index found. getting Image Directory" }
this.imageDirectories.getOrNull(index)?.let {
logger?.d { "Image Directory found, showing photo" }
showPhoto(it)
} ?: run {
logger?.w { "Image Directory NOT found for index: $index in count ${imageDirectories.count()}" }
}
selectedImageDirectory?.let {
logger?.d { "Image Directory found, showing photo" }
showPhoto(it)
} ?: run {
logger?.w { "Image Directory NOT found" }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import com.kevinschildhorn.fotopresenter.ui.screens.common.CommonAtoms
fun FotoDialog(
dialogTitle: String,
onDismissRequest: () -> Unit,
onConfirmation: () -> Unit,
onConfirmation: (() -> Unit)? = null,
content: @Composable ColumnScope.() -> Unit,
) {
Dialog(onDismissRequest = { onDismissRequest() }) {
Expand Down Expand Up @@ -68,8 +68,10 @@ fun FotoDialog(
) {
AtomikText("Cancel", atom = CommonAtoms.dialogButton)
}
PrimaryTextButton("Confirm") {
onConfirmation()
onConfirmation?.let {
PrimaryTextButton("Confirm") {
it()
}
}
}
}
Expand Down
Loading
Loading