From 53884410bb47eb3332494f6ccc92964a3fc7e010 Mon Sep 17 00:00:00 2001 From: Suhas Dissanayake Date: Tue, 24 Oct 2023 23:15:35 +0530 Subject: [PATCH] feat: button to add all songs of an Album/Playlist to library --- .../suhasdissa/vibeyou/backend/data/Album.kt | 3 +- .../repository/LocalMusicRepository.kt | 3 +- .../repository/SongDatabaseRepository.kt | 1 + .../repository/SongDatabaseRepositoryImpl.kt | 3 ++ .../backend/viewmodel/PlayerViewModel.kt | 6 +++ .../vibeyou/ui/screens/search/AlbumScreen.kt | 45 ++++++++++++++++--- app/src/main/res/values/strings.xml | 3 ++ 7 files changed, 55 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/app/suhasdissa/vibeyou/backend/data/Album.kt b/app/src/main/java/app/suhasdissa/vibeyou/backend/data/Album.kt index 22921837..fe99dd97 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/backend/data/Album.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/backend/data/Album.kt @@ -7,5 +7,6 @@ data class Album( val title: String, val thumbnailUri: Uri? = null, val artistsText: String, - val numberOfSongs: Int? = null + val numberOfSongs: Int? = null, + val isLocal: Boolean = false ) diff --git a/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/LocalMusicRepository.kt b/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/LocalMusicRepository.kt index a8ceac0c..c790e7c2 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/LocalMusicRepository.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/LocalMusicRepository.kt @@ -162,7 +162,8 @@ class LocalMusicRepository( ), artistsText = cursor.getString(artistColumn), thumbnailUri = getAlbumArt(id), - numberOfSongs = cursor.getInt(songsColumn) + numberOfSongs = cursor.getInt(songsColumn), + isLocal = true ) ) } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/SongDatabaseRepository.kt b/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/SongDatabaseRepository.kt index bc9205eb..866d3f89 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/SongDatabaseRepository.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/SongDatabaseRepository.kt @@ -6,6 +6,7 @@ import kotlinx.coroutines.flow.Flow interface SongDatabaseRepository { suspend fun addSong(song: Song) + suspend fun addSongs(songs: List) suspend fun removeSong(song: Song) suspend fun getSongById(id: String): Song? fun getAllSongsStream(): Flow> diff --git a/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/SongDatabaseRepositoryImpl.kt b/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/SongDatabaseRepositoryImpl.kt index 5fbfaffe..23b5a405 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/SongDatabaseRepositoryImpl.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/backend/repository/SongDatabaseRepositoryImpl.kt @@ -10,6 +10,9 @@ import kotlinx.coroutines.flow.Flow class SongDatabaseRepositoryImpl(private val songsDao: SongsDao) : SongDatabaseRepository { override suspend fun addSong(song: Song) = songsDao.addSong(song.asSongEntity) + override suspend fun addSongs(songs: List) = + songsDao.addSongs(songs.map { it.asSongEntity }) + override suspend fun getSongById(id: String): Song? = songsDao.getSongById(id)?.asSong override fun getAllSongsStream(): Flow> = songsDao.getAllSongsStream() override fun getFavSongsStream(): Flow> = songsDao.getFavSongsStream() diff --git a/app/src/main/java/app/suhasdissa/vibeyou/backend/viewmodel/PlayerViewModel.kt b/app/src/main/java/app/suhasdissa/vibeyou/backend/viewmodel/PlayerViewModel.kt index a2ef8531..6d81b673 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/backend/viewmodel/PlayerViewModel.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/backend/viewmodel/PlayerViewModel.kt @@ -96,6 +96,12 @@ class PlayerViewModel( } } + fun saveSongs(songs: List) { + viewModelScope.launch { + songDatabaseRepository.addSongs(songs) + } + } + fun playNext(song: Song) { controller!!.addNext(song.asMediaItem) } diff --git a/app/src/main/java/app/suhasdissa/vibeyou/ui/screens/search/AlbumScreen.kt b/app/src/main/java/app/suhasdissa/vibeyou/ui/screens/search/AlbumScreen.kt index ef95901f..ce1baa8d 100644 --- a/app/src/main/java/app/suhasdissa/vibeyou/ui/screens/search/AlbumScreen.kt +++ b/app/src/main/java/app/suhasdissa/vibeyou/ui/screens/search/AlbumScreen.kt @@ -1,12 +1,15 @@ package app.suhasdissa.vibeyou.ui.screens.search +import android.widget.Toast import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.LazyColumn @@ -14,9 +17,12 @@ import androidx.compose.foundation.lazy.items import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Shuffle +import androidx.compose.material.icons.rounded.LibraryAdd +import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.FloatingActionButton import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.PlainTooltipBox import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue @@ -27,6 +33,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp @@ -42,6 +49,7 @@ import app.suhasdissa.vibeyou.ui.components.SongCard import app.suhasdissa.vibeyou.ui.components.SongSettingsSheetSearchPage import coil.compose.AsyncImage +@OptIn(ExperimentalMaterial3Api::class) @Composable fun AlbumScreen( state: AlbumInfoState, @@ -49,13 +57,36 @@ fun AlbumScreen( ) { MiniPlayerScaffold(fab = { if (state is AlbumInfoState.Success) { - FloatingActionButton(onClick = { - playerViewModel.playSongs(state.songs, shuffle = true) - }) { - Icon( - imageVector = Icons.Default.Shuffle, - contentDescription = stringResource(R.string.shuffle) - ) + Column { + val context = LocalContext.current + if (!state.album.isLocal) { + PlainTooltipBox(tooltip = { + Text(stringResource(R.string.add_all_songs_to_the_library)) + }) { + FloatingActionButton(onClick = { + playerViewModel.saveSongs(state.songs) + Toast.makeText( + context, + context.getString(R.string.added_all_the_songs_to_the_library), + Toast.LENGTH_SHORT + ).show() + }) { + Icon( + imageVector = Icons.Rounded.LibraryAdd, + contentDescription = stringResource(R.string.add_to_library) + ) + } + } + Spacer(modifier = Modifier.height(8.dp)) + } + FloatingActionButton(onClick = { + playerViewModel.playSongs(state.songs, shuffle = true) + }) { + Icon( + imageVector = Icons.Default.Shuffle, + contentDescription = stringResource(R.string.shuffle) + ) + } } } }) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dc4dc7db..9b47bb40 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -72,4 +72,7 @@ Customize the appearance to fit your needs. Notification thumbnail fallback Fall back to a using the system/theme accent color as a notification thumbnail if there\'s none. + Add to library + Added all the songs to the library + Add all songs to the library \ No newline at end of file