Skip to content

Commit

Permalink
Implemented move song in playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Mar 19, 2024
1 parent ddd3604 commit 82aa80d
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 87 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ dependencies {
implementation "androidx.media3:media3-exoplayer:$media3_version"
implementation "androidx.media3:media3-session:$media3_version"
implementation "androidx.media3:media3-datasource-cronet:$media3_version"
implementation "sh.calvin.reorderable:reorderable:1.3.3"

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
Expand Down
35 changes: 25 additions & 10 deletions app/src/main/java/org/blackcandy/android/api/BlackCandyService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import io.ktor.client.call.body
import io.ktor.client.request.delete
import io.ktor.client.request.forms.submitForm
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.client.request.post
import io.ktor.client.request.put
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.http.HttpHeaders
Expand Down Expand Up @@ -38,6 +41,11 @@ interface BlackCandyService {
suspend fun removeAllSongsFromCurrentPlaylist(): ApiResponse<Unit>

suspend fun removeSongFromCurrentPlaylist(songId: Int): ApiResponse<Unit>

suspend fun moveSongInCurrentPlaylist(
songId: Int,
destinationSongId: Int,
): ApiResponse<Unit>
}

class BlackCandyServiceImpl(
Expand Down Expand Up @@ -100,13 +108,9 @@ class BlackCandyServiceImpl(

override suspend fun addSongToFavorite(songId: Int): ApiResponse<Song> {
return handleResponse {
client.submitForm(
url = "favorite_playlist/songs",
formParameters =
parameters {
append("song_id", songId.toString())
},
).body()
client.post("favorite_playlist/songs") {
parameter("song_id", songId.toString())
}.body()
}
}

Expand All @@ -128,11 +132,22 @@ class BlackCandyServiceImpl(
}
}

override suspend fun moveSongInCurrentPlaylist(
songId: Int,
destinationSongId: Int,
): ApiResponse<Unit> {
return handleResponse {
client.put("current_playlist/songs/$songId/move") {
parameter("destination_song_id", destinationSongId.toString())
}.body()
}
}

private suspend fun <T> handleResponse(request: suspend () -> T): ApiResponse<T> {
try {
return ApiResponse.Success(request())
return try {
ApiResponse.Success(request())
} catch (e: ApiException) {
return ApiResponse.Failure(e)
ApiResponse.Failure(e)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
package org.blackcandy.android.compose.player

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SwipeToDismissBox
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Text
import androidx.compose.material3.rememberSwipeToDismissBoxState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import org.blackcandy.android.R
import org.blackcandy.android.models.Song
import org.blackcandy.android.utils.DurationFormatter
import sh.calvin.reorderable.ReorderableItem
import sh.calvin.reorderable.rememberReorderableLazyColumnState

@OptIn(ExperimentalMaterial3Api::class)
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun PlayerPlaylist(
modifier: Modifier = Modifier,
playlist: List<Song>,
currentSong: Song?,
onItemClicked: (Int) -> Unit,
onItemSweepToDismiss: (Song) -> Unit,
onItemSweepToDismiss: (Int) -> Unit,
onItemMoved: (Int, Int) -> Unit,
) {
if (playlist.isEmpty()) {
Box(
Expand All @@ -45,62 +39,42 @@ fun PlayerPlaylist(
Text(text = stringResource(R.string.empty_playlist_indication))
}
} else {
LazyColumn(modifier = modifier) {
itemsIndexed(playlist, key = { _, song -> song.id }) { index, song ->
val dismissState =
rememberSwipeToDismissBoxState(
confirmValueChange = {
onItemSweepToDismiss(song)
true
},
)

SwipeToDismissBox(
state = dismissState,
enableDismissFromStartToEnd = false,
backgroundContent = {
Box(
modifier =
Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.error)
.padding(horizontal = dimensionResource(R.dimen.padding_small)),
contentAlignment = Alignment.CenterEnd,
) {
Icon(
painter = painterResource(R.drawable.baseline_delete_24),
contentDescription = stringResource(R.string.delete),
tint = MaterialTheme.colorScheme.onError,
)
}
var fromIndex by remember { mutableIntStateOf(-1) }
val (playlistState, setPlaylistState) = remember(playlist) { mutableStateOf(playlist) }
val lazyListState = rememberLazyListState()
val reorderableLazyColumnState =
rememberReorderableLazyColumnState(lazyListState) { from, to ->
setPlaylistState(
playlistState.toMutableList().apply {
add(to.index, removeAt(from.index))
},
) {
ListItem(
modifier =
Modifier
.clickable(
onClick = { onItemClicked(index) },
),
headlineContent = {
Text(
text = song.name,
color = if (song == currentSong) MaterialTheme.colorScheme.primary else Color.Unspecified,
fontWeight = if (song == currentSong) FontWeight.Bold else null,
maxLines = 2,
overflow = TextOverflow.Ellipsis,
)
)
}

LazyColumn(
modifier = modifier,
state = lazyListState,
) {
items(playlistState, key = { it.id }) { song ->
ReorderableItem(reorderableLazyColumnState, key = song.id) {
PlayerPlaylistItem(
song = song,
isCurrent = song == currentSong,
scope = this,
onSweepToDismiss = onItemSweepToDismiss,
onClicked = onItemClicked,
onMoveStarted = { songId ->
fromIndex = playlistState.indexOfFirst { it.id == songId }
},
supportingContent = {
Text(
text = song.artistName,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
onMoveEnded = { songId ->
val toIndex = playlistState.indexOfFirst { it.id == songId }

if (fromIndex != -1 && fromIndex != toIndex) {
onItemMoved(fromIndex, toIndex)
fromIndex = -1
}
},
trailingContent = { Text(text = DurationFormatter.string(song.duration)) },
)

HorizontalDivider()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.blackcandy.android.compose.player

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SwipeToDismissBox
import androidx.compose.material3.SwipeToDismissBoxValue
import androidx.compose.material3.Text
import androidx.compose.material3.rememberSwipeToDismissBoxState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import org.blackcandy.android.R
import org.blackcandy.android.models.Song
import sh.calvin.reorderable.ReorderableItemScope

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PlayerPlaylistItem(
song: Song,
isCurrent: Boolean,
scope: ReorderableItemScope,
onSweepToDismiss: (Int) -> Unit,
onClicked: (Int) -> Unit,
onMoveStarted: (Int) -> Unit,
onMoveEnded: (Int) -> Unit,
) {
val dismissState =
rememberSwipeToDismissBoxState(
confirmValueChange = {
if (it == SwipeToDismissBoxValue.EndToStart) {
onSweepToDismiss(song.id)
}

it == SwipeToDismissBoxValue.EndToStart
},
positionalThreshold = { it * 0.5f },
)

SwipeToDismissBox(
state = dismissState,
enableDismissFromStartToEnd = false,
backgroundContent = {
Box(
modifier =
Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.error)
.padding(horizontal = dimensionResource(R.dimen.padding_small)),
contentAlignment = Alignment.CenterEnd,
) {
Icon(
painter = painterResource(R.drawable.baseline_delete_24),
contentDescription = stringResource(R.string.delete),
tint = MaterialTheme.colorScheme.onError,
)
}
},
) {
ListItem(
modifier =
Modifier
.clickable(
onClick = { onClicked(song.id) },
),
headlineContent = {
Text(
text = song.name,
color = if (isCurrent) MaterialTheme.colorScheme.primary else Color.Unspecified,
fontWeight = if (isCurrent) FontWeight.Bold else null,
maxLines = 2,
overflow = TextOverflow.Ellipsis,
)
},
supportingContent = {
Text(
text = song.artistName,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
},
trailingContent = {
Icon(
modifier =
with(scope) {
Modifier.draggableHandle(
onDragStarted = {
onMoveStarted(song.id)
},
onDragStopped = {
onMoveEnded(song.id)
},
)
},
painter = painterResource(R.drawable.baseline_drag_handle_24),
contentDescription = stringResource(R.string.drag_handle),
)
},
)
}

HorizontalDivider()
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ fun PlayerScreen(
modifier = Modifier.weight(1f),
playlist = uiState.musicState.playlist,
currentSong = uiState.musicState.currentSong,
onItemClicked = { viewModel.playOn(it) },
onItemSweepToDismiss = { viewModel.removeSongFromPlaylist(it) },
onItemClicked = { songId -> viewModel.playOn(songId) },
onItemSweepToDismiss = { songId -> viewModel.removeSongFromPlaylist(songId) },
onItemMoved = { from, to -> viewModel.moveSongInPlaylist(from, to) },
)
} else {
Spacer(modifier = Modifier.weight(1f))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ class CurrentPlaylistRepository(
suspend fun removeSong(songId: Int): TaskResult<Unit> {
return service.removeSongFromCurrentPlaylist(songId).asResult()
}

suspend fun moveSong(
songId: Int,
destinationSongId: Int,
): TaskResult<Unit> {
return service.moveSongInCurrentPlaylist(songId, destinationSongId).asResult()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ class MusicServiceController(
}
}

fun moveSongInPlaylist(
from: Int,
to: Int,
) {
val songs = musicState.value.playlist.toMutableList().apply { add(to, removeAt(from)) }
updatePlaylist(songs)
}

fun setPlaybackMode(playbackMode: PlaybackMode) {
when (playbackMode) {
PlaybackMode.NO_REPEAT -> {
Expand Down
Loading

0 comments on commit 82aa80d

Please sign in to comment.