Skip to content

Commit

Permalink
Add new playing begin with behavior on songs in album or playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Apr 29, 2024
1 parent b28ecdd commit 4b9155d
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.blackcandy.android.data

import org.blackcandy.android.api.BlackCandyService
import org.blackcandy.android.models.Song
import org.blackcandy.android.utils.PlayableResource
import org.blackcandy.android.utils.TaskResult

class CurrentPlaylistRepository(
Expand All @@ -27,17 +26,12 @@ class CurrentPlaylistRepository(
return service.moveSongInCurrentPlaylist(songId, destinationSongId).asResult()
}

suspend fun replaceWith(
resourceType: PlayableResource,
resourceId: Int,
): TaskResult<List<Song>> {
val response =
when (resourceType) {
PlayableResource.ALBUM -> service.replaceCurrentPlaylistWithAlbumSongs(resourceId)
PlayableResource.PLAYLIST -> service.replaceCurrentPlaylistWithPlaylistSongs(resourceId)
}
suspend fun replaceWithAlbumSongs(albumId: Int): TaskResult<List<Song>> {
return service.replaceCurrentPlaylistWithAlbumSongs(albumId).asResult()
}

return response.asResult()
suspend fun replaceWithPlaylistSongs(playlistId: Int): TaskResult<List<Song>> {
return service.replaceCurrentPlaylistWithPlaylistSongs(playlistId).asResult()
}

suspend fun addSongToNext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.blackcandy.android.fragments.web.WebFragment
import org.blackcandy.android.fragments.web.WebHomeFragment
import org.blackcandy.android.fragments.web.WebLibraryFragment
import org.blackcandy.android.utils.BLACK_CANDY_USER_AGENT
import org.blackcandy.android.utils.PlayableResource
import org.blackcandy.android.utils.SnackbarUtil.Companion.showSnackbar
import org.blackcandy.android.utils.Theme
import org.blackcandy.android.viewmodels.NavHostViewModel
Expand Down Expand Up @@ -83,17 +82,34 @@ open class MainNavHostFragment : TurboSessionNavHostFragment() {
}

@JavascriptInterface
fun playAll(
resourceType: String,
resourceId: Int,
fun playAlbum(albumId: Int) {
viewModel.playAlbum(albumId)
}

@JavascriptInterface
fun playPlaylist(playlistId: Int) {
viewModel.playPlaylist(playlistId)
}

@JavascriptInterface
fun playAlbumBeginWith(
albumId: Int,
songId: Int,
) {
viewModel.playAlbumBeginWith(albumId, songId)
}

@JavascriptInterface
fun playPlaylistBeginWith(
playlistId: Int,
songId: Int,
) {
val resourceTypeValue = PlayableResource.values().find { it.name == resourceType.uppercase() } ?: return
viewModel.playAll(resourceTypeValue, resourceId)
viewModel.playPlaylistBeginWith(playlistId, songId)
}

@JavascriptInterface
fun playSong(songId: Int) {
viewModel.playSong(songId)
fun playNow(songId: Int) {
viewModel.playNow(songId)
}

@JavascriptInterface
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.blackcandy.android.data.CurrentPlaylistRepository
import org.blackcandy.android.data.ServerAddressRepository
import org.blackcandy.android.media.MusicServiceController
import org.blackcandy.android.models.AlertMessage
import org.blackcandy.android.utils.PlayableResource
import org.blackcandy.android.models.Song
import org.blackcandy.android.utils.TaskResult
import org.blackcandy.android.utils.Theme

Expand Down Expand Up @@ -55,15 +55,40 @@ class NavHostViewModel(
}
}

fun playAll(
resourceType: PlayableResource,
resourceId: Int,
fun playAlbum(albumId: Int) {
viewModelScope.launch {
when (val result = currentPlaylistRepository.replaceWithAlbumSongs(albumId)) {
is TaskResult.Success -> {
playSongs(result.data)
}
is TaskResult.Failure -> {
_uiState.update { it.copy(alertMessage = AlertMessage.String(result.message)) }
}
}
}
}

fun playPlaylist(playlistId: Int) {
viewModelScope.launch {
when (val result = currentPlaylistRepository.replaceWithPlaylistSongs(playlistId)) {
is TaskResult.Success -> {
playSongs(result.data)
}
is TaskResult.Failure -> {
_uiState.update { it.copy(alertMessage = AlertMessage.String(result.message)) }
}
}
}
}

fun playAlbumBeginWith(
albumId: Int,
songId: Int,
) {
viewModelScope.launch {
when (val result = currentPlaylistRepository.replaceWith(resourceType, resourceId)) {
when (val result = currentPlaylistRepository.replaceWithAlbumSongs(albumId)) {
is TaskResult.Success -> {
musicServiceController.updatePlaylist(result.data)
musicServiceController.playOn(0)
playSongsBeginWith(result.data, songId)
}
is TaskResult.Failure -> {
_uiState.update { it.copy(alertMessage = AlertMessage.String(result.message)) }
Expand All @@ -72,7 +97,23 @@ class NavHostViewModel(
}
}

fun playSong(songId: Int) {
fun playPlaylistBeginWith(
playlistId: Int,
songId: Int,
) {
viewModelScope.launch {
when (val result = currentPlaylistRepository.replaceWithPlaylistSongs(playlistId)) {
is TaskResult.Success -> {
playSongsBeginWith(result.data, songId)
}
is TaskResult.Failure -> {
_uiState.update { it.copy(alertMessage = AlertMessage.String(result.message)) }
}
}
}
}

fun playNow(songId: Int) {
viewModelScope.launch {
val index = musicServiceController.getSongIndex(songId)

Expand Down Expand Up @@ -135,4 +176,24 @@ class NavHostViewModel(
fun showFlashMessage(message: String) {
_uiState.update { it.copy(alertMessage = AlertMessage.String(message)) }
}

private fun playSongs(songs: List<Song>) {
musicServiceController.updatePlaylist(songs)
musicServiceController.playOn(0)
}

private fun playSongsBeginWith(
songs: List<Song>,
songId: Int,
) {
musicServiceController.updatePlaylist(songs)

val index = musicServiceController.getSongIndex(songId)

if (index != -1) {
musicServiceController.playOn(index)
} else {
musicServiceController.playOn(0)
}
}
}

0 comments on commit 4b9155d

Please sign in to comment.