Skip to content

Commit

Permalink
Adding creating playlists, only names for now
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinSchildhorn committed Dec 16, 2023
1 parent dc552ca commit e0d1923
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.kevinschildhorn.fotopresenter.ui.screens.common.composables.ActionShe
import com.kevinschildhorn.fotopresenter.ui.screens.common.composables.ButtonState
import com.kevinschildhorn.fotopresenter.ui.screens.common.composables.ConfirmationDialog
import com.kevinschildhorn.fotopresenter.ui.screens.common.composables.PrimaryTextButton
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.composables.TextConfirmationDialog
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.composables.TextEntryDialog

@Preview
@Composable
Expand Down Expand Up @@ -54,7 +54,7 @@ fun ConfirmationDialogPreview() {
@Preview
@Composable
fun TextConfirmationDialogPreview() {
TextConfirmationDialog(
TextEntryDialog(
{

},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ class PlaylistDataSource(
}
}

fun deletePlaylistByName(name: String): Boolean {
fun deletePlaylist(id: Long): Boolean {
return try {
val playlist = database.playlistQueries.selectPlaylistByName(name).executeAsOne()
val playlist = database.playlistQueries.selectPlaylistById(id).executeAsOne()
database.playlistQueries.deletePlaylist(playlist.name)
database.imageDirectoryQueries.deletePlaylist(playlist.id)
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class PlaylistRepository(
fun getPlaylistImage(playlistId: Long, directoryPath: String): PlaylistImage? =
playlistDataSource.getPlaylistImage(playlistId, directoryPath)

fun deletePlaylistByName(name: String): Boolean =
playlistDataSource.deletePlaylistByName(name)
fun deletePlaylist(id: Long): Boolean =
playlistDataSource.deletePlaylist(id)
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,78 @@
package com.kevinschildhorn.fotopresenter.ui.screens.common.composables

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.kevinschildhorn.atomik.color.base.composeColor
import com.kevinschildhorn.fotopresenter.ui.atoms.FotoColors
import com.kevinschildhorn.fotopresenter.ui.atoms.Padding
import com.kevinschildhorn.fotopresenter.ui.screens.common.CommonAtoms

@Composable
fun FotoDialog(
dialogTitle: String,
onDismissRequest: () -> Unit,
onConfirmation: () -> Unit,
content: @Composable (() -> Unit)?,
content: @Composable ColumnScope.() -> Unit,
) {
AlertDialog(
modifier = Modifier.clip(RoundedCornerShape(10.dp)),
backgroundColor = FotoColors.surface.composeColor,
title = {
AtomikText(
text = dialogTitle,
atom = CommonAtoms.dialogTitle
)
},
text = content,
onDismissRequest = {
onDismissRequest()
},
confirmButton = {
PrimaryTextButton("Confirm"){
onConfirmation()
}
},
dismissButton = {
TextButton(
onClick = {
onDismissRequest()
}
Dialog(onDismissRequest = { onDismissRequest() }) {
// Draw a rectangle shape with rounded corners inside the dialog
Card(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
shape = RoundedCornerShape(16.dp),
) {
Column(
modifier = Modifier
.fillMaxWidth().padding(Padding.STANDARD.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.Start,
) {
AtomikText("Cancel", atom = CommonAtoms.dialogButton)
AtomikText(
text = dialogTitle,
atom = CommonAtoms.dialogTitle,
)
Spacer(Modifier.size(Padding.MEDIUM.dp))
content()
Row(
modifier = Modifier
.fillMaxWidth()
.padding(top = Padding.SMALL.dp),
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically
) {
TextButton(
onClick = {
onDismissRequest()
},
modifier = Modifier.padding(Padding.SMALL.dp)
) {
AtomikText("Cancel", atom = CommonAtoms.dialogButton)
}
PrimaryTextButton("Confirm") {
onConfirmation()
}
}
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.kevinschildhorn.fotopresenter.ui.screens.common.composables.ConfirmationDialog
import com.kevinschildhorn.fotopresenter.ui.screens.directory.DirectoryOverlay
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.composables.PlaylistOverlay
import com.kevinschildhorn.fotopresenter.ui.screens.playlist.composables.TextEntryDialog

enum class PlaylistDialog{
NONE,
CREATE,
DELETE,
EDIT
}
@Composable
fun PlaylistScreen(
viewModel: PlaylistViewModel,
onLoginSuccess: () -> Unit,
) {
val uiState by viewModel.uiState.collectAsState()
var dialogOpen by remember { mutableStateOf(PlaylistDialog.NONE) }

LaunchedEffect(Unit) {
viewModel.refreshPlaylists()
Expand All @@ -23,10 +36,37 @@ fun PlaylistScreen(
onClick = {

}, onDelete = {

dialogOpen = PlaylistDialog.DELETE
viewModel.setSelectedPlaylist(it)
}, onEdit = {

dialogOpen = PlaylistDialog.EDIT
viewModel.setSelectedPlaylist(it)
}, onCreate = {
dialogOpen = PlaylistDialog.CREATE
}
)

if (dialogOpen == PlaylistDialog.CREATE) {
TextEntryDialog({
dialogOpen = PlaylistDialog.NONE
}, {
viewModel.createPlaylist(it)
dialogOpen = PlaylistDialog.NONE
})
}
if (dialogOpen == PlaylistDialog.DELETE) {
ConfirmationDialog(
"Delete Playlist",
"Are you sure you want to delete this playlist?",
onDismissRequest = {
dialogOpen = PlaylistDialog.NONE
viewModel.clearSelectedPlaylist()
},
onConfirmation = {
viewModel.deletePlaylist()
dialogOpen = PlaylistDialog.NONE
viewModel.clearSelectedPlaylist()
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import com.kevinschildhorn.fotopresenter.ui.screens.common.ScreenState

data class PlaylistScreenState(
val playlists: List<Playlist> = emptyList(),
val selectedId: Long = 0L,
val selectedId: Long? = null,
override val state: UiState = UiState.IDLE,
) : ScreenState
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,24 @@ class PlaylistViewModel(
_uiState.update { it.copy(playlists = allPlaylists) }
}

fun setSelectedPlaylist(id:Long){
_uiState.update { it.copy(selectedId = id) }
}

fun clearSelectedPlaylist(){
_uiState.update { it.copy(selectedId = null) }
}

fun createPlaylist(name: String){
playlistRepository.createPlaylist(name)
refreshPlaylists()
}

fun deletePlaylist(name: String){
playlistRepository.deletePlaylistByName(name)
fun deletePlaylist(){
_uiState.value.selectedId?.let {
playlistRepository.deletePlaylist(it)
}
refreshPlaylists()
clearSelectedPlaylist()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.kevinschildhorn.fotopresenter.ui.screens.common.composables.FotoDialo
import com.kevinschildhorn.fotopresenter.ui.screens.login.composables.LoginTextField

@Composable
fun TextConfirmationDialog(
fun TextEntryDialog(
onDismissRequest: () -> Unit,
onConfirmation: (String) -> Unit,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ SELECT *
FROM Playlist
WHERE name = ?;

selectPlaylistById:
SELECT *
FROM Playlist
WHERE id = ?;

insertPlaylist:
INSERT INTO Playlist(name)
VALUES (?);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ class PlaylistDataSourceTest {
val newPlaylist = dataSource.getPlaylistByName(playlistName)
assertNotNull(newPlaylist)

val image = dataSource.getPlaylistImage(playlist?.id ?: 0L, imageDirectory.details.fullPath)
val image = dataSource.getPlaylistImage(playlist.id, imageDirectory.details.fullPath)
assertNotNull(image)

val result = dataSource.deletePlaylistByName(playlistName)
val result = dataSource.deletePlaylist(playlist.id)
assertTrue(result)

val deletedPlaylist = dataSource.getPlaylistByName(playlistName)
assertNull(deletedPlaylist)
val deletedImage = dataSource.getPlaylistImage(playlist?.id ?: 0L, imageDirectory.details.fullPath)
val deletedImage = dataSource.getPlaylistImage(playlist.id, imageDirectory.details.fullPath)
assertNull(deletedImage)
}

Expand Down

0 comments on commit e0d1923

Please sign in to comment.