Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: button to add all songs of an Album/Playlist to library
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Oct 24, 2023
1 parent 4cb41b8 commit 5388441
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ class LocalMusicRepository(
),
artistsText = cursor.getString(artistColumn),
thumbnailUri = getAlbumArt(id),
numberOfSongs = cursor.getInt(songsColumn)
numberOfSongs = cursor.getInt(songsColumn),
isLocal = true
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.coroutines.flow.Flow

interface SongDatabaseRepository {
suspend fun addSong(song: Song)
suspend fun addSongs(songs: List<Song>)
suspend fun removeSong(song: Song)
suspend fun getSongById(id: String): Song?
fun getAllSongsStream(): Flow<List<SongEntity>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Song>) =
songsDao.addSongs(songs.map { it.asSongEntity })

override suspend fun getSongById(id: String): Song? = songsDao.getSongById(id)?.asSong
override fun getAllSongsStream(): Flow<List<SongEntity>> = songsDao.getAllSongsStream()
override fun getFavSongsStream(): Flow<List<SongEntity>> = songsDao.getFavSongsStream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ class PlayerViewModel(
}
}

fun saveSongs(songs: List<Song>) {
viewModelScope.launch {
songDatabaseRepository.addSongs(songs)
}
}

fun playNext(song: Song) {
controller!!.addNext(song.asMediaItem)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
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
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
Expand All @@ -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
Expand All @@ -42,20 +49,44 @@ 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,
playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory)
) {
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)
)
}
}
}
}) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@
<string name="appearance_settings_description">Customize the appearance to fit your needs.</string>
<string name="fallback_thumnail_accent">Notification thumbnail fallback</string>
<string name="fallback_thumnail_accent_description">Fall back to a using the system/theme accent color as a notification thumbnail if there\'s none.</string>
<string name="add_to_library">Add to library</string>
<string name="added_all_the_songs_to_the_library">Added all the songs to the library</string>
<string name="add_all_songs_to_the_library">Add all songs to the library</string>
</resources>

0 comments on commit 5388441

Please sign in to comment.