Skip to content

Commit

Permalink
feat(album menu): add all songs to library
Browse files Browse the repository at this point in the history
  • Loading branch information
z-huang committed Aug 30, 2024
1 parent e0d98e4 commit 3a83597
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/zionhuang/music/db/DatabaseDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ interface DatabaseDao {
@Query("UPDATE song SET totalPlayTime = totalPlayTime + :playTime WHERE id = :songId")
fun incrementTotalPlayTime(songId: String, playTime: Long)

// If the song is already in the library, the time won't be updated
@Query("UPDATE song SET inLibrary = :inLibrary WHERE id = :songId AND (inLibrary IS NULL OR :inLibrary IS NULL)")
fun inLibrary(songId: String, inLibrary: LocalDateTime?)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.media3.exoplayer.offline.Download
import com.zionhuang.music.R

val GridMenuItemHeight = 96.dp
val GridMenuItemHeight = 108.dp

@Composable
fun GridMenu(
Expand Down Expand Up @@ -101,7 +102,11 @@ fun LazyGridScope.GridMenuItem(
style = MaterialTheme.typography.labelLarge,
textAlign = TextAlign.Center,
maxLines = 2,
modifier = Modifier.fillMaxWidth()
modifier = Modifier
.fillMaxWidth()
.height(with(LocalDensity.current) {
MaterialTheme.typography.labelLarge.lineHeight.toDp() * 2
})
)
}
}
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/zionhuang/music/ui/menu/AlbumMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import com.zionhuang.music.ui.component.GridMenuItem
import com.zionhuang.music.ui.component.ListDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.time.LocalDateTime

@Composable
fun AlbumMenu(
Expand All @@ -86,6 +87,9 @@ fun AlbumMenu(
var songs by remember {
mutableStateOf(emptyList<Song>())
}
val allInLibrary = remember(songs) {
songs.all { it.song.inLibrary != null }
}

LaunchedEffect(Unit) {
database.albumSongs(album.id).collect {
Expand Down Expand Up @@ -222,19 +226,46 @@ fun AlbumMenu(
onDismiss()
playerConnection.playNext(songs.map { it.toMediaItem() })
}

GridMenuItem(
icon = R.drawable.queue_music,
title = R.string.add_to_queue
) {
onDismiss()
playerConnection.addToQueue(songs.map { it.toMediaItem() })
}

GridMenuItem(
icon = R.drawable.playlist_add,
title = R.string.add_to_playlist
) {
showChoosePlaylistDialog = true
}

if (allInLibrary) {
GridMenuItem(
icon = R.drawable.library_add_check,
title = R.string.remove_all_from_library
) {
database.transaction {
songs.forEach {
inLibrary(it.id, null)
}
}
}
} else {
GridMenuItem(
icon = R.drawable.library_add,
title = R.string.add_all_to_library
) {
database.transaction {
songs.forEach {
inLibrary(it.id, LocalDateTime.now())
}
}
}
}

DownloadGridMenu(
state = downloadState,
onDownload = {
Expand Down Expand Up @@ -262,6 +293,7 @@ fun AlbumMenu(
}
}
)

GridMenuItem(
icon = R.drawable.artist,
title = R.string.view_artist
Expand All @@ -273,6 +305,7 @@ fun AlbumMenu(
showSelectArtistDialog = true
}
}

GridMenuItem(
icon = {
Icon(
Expand All @@ -292,6 +325,7 @@ fun AlbumMenu(
}
}
}

GridMenuItem(
icon = R.drawable.share,
title = R.string.share
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@
<string name="play_next">Play next</string>
<string name="add_to_queue">Add to queue</string>
<string name="add_to_library">Add to library</string>
<string name="add_all_to_library">Add all to library</string>
<string name="remove_from_library">Remove from library</string>
<string name="remove_all_from_library">Remove all from library</string>
<string name="download">Download</string>
<string name="downloading">Downloading</string>
<string name="remove_download">Remove download</string>
Expand Down

0 comments on commit 3a83597

Please sign in to comment.