Skip to content

Commit

Permalink
feat: improve queue item menu
Browse files Browse the repository at this point in the history
  • Loading branch information
z-huang committed Aug 28, 2024
1 parent 731dcf4 commit 3f7d564
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class DownloadUtil @Inject constructor(
}
val downloads = MutableStateFlow<Map<String, Download>>(emptyMap())

fun getDownload(songId: String): Flow<Download?> = downloads.map { it[songId] }
fun getDownload(songId: String?): Flow<Download?> = downloads.map { it[songId] }

init {
val result = mutableMapOf<String, Download>()
Expand Down
16 changes: 15 additions & 1 deletion app/src/main/java/com/zionhuang/music/ui/component/Items.kt
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,20 @@ fun PlaylistGridItem(
@Composable
fun MediaMetadataListItem(
mediaMetadata: MediaMetadata,
modifier: Modifier,
modifier: Modifier = Modifier,
badges: @Composable RowScope.() -> Unit = {
val database = LocalDatabase.current
val song by database.song(mediaMetadata.id).collectAsState(initial = null)

if (song?.song?.liked == true) {
Icon.Favorite()
}
if (song?.song?.inLibrary != null) {
Icon.Library()
}
val download by LocalDownloadUtil.current.getDownload(song?.id).collectAsState(initial = null)
Icon.Download(download?.state)
},
isActive: Boolean = false,
isPlaying: Boolean = false,
trailingContent: @Composable RowScope.() -> Unit = {},
Expand All @@ -597,6 +610,7 @@ fun MediaMetadataListItem(
mediaMetadata.artists.joinToString { it.name },
makeTimeString(mediaMetadata.duration * 1000L)
),
badges = badges,
thumbnailContent = {
ItemThumbnail(
thumbnailUrl = mediaMetadata.thumbnailUrl,
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/com/zionhuang/music/ui/menu/PlayerMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
Expand Down Expand Up @@ -56,6 +58,7 @@ import com.zionhuang.music.LocalDownloadUtil
import com.zionhuang.music.LocalPlayerConnection
import com.zionhuang.music.R
import com.zionhuang.music.constants.ListItemHeight
import com.zionhuang.music.db.entities.SongEntity
import com.zionhuang.music.models.MediaMetadata
import com.zionhuang.music.playback.ExoDownloadService
import com.zionhuang.music.ui.component.BigSeekBar
Expand All @@ -64,6 +67,7 @@ import com.zionhuang.music.ui.component.DownloadGridMenu
import com.zionhuang.music.ui.component.GridMenu
import com.zionhuang.music.ui.component.GridMenuItem
import com.zionhuang.music.ui.component.ListDialog
import com.zionhuang.music.ui.component.MediaMetadataListItem
import java.time.LocalDateTime
import kotlin.math.log2
import kotlin.math.pow
Expand Down Expand Up @@ -174,6 +178,35 @@ fun PlayerMenu(
modifier = Modifier.weight(1f)
)
}
} else {
MediaMetadataListItem(
mediaMetadata = mediaMetadata,
badges = {},
trailingContent = {
val song by database.song(mediaMetadata.id).collectAsState(initial = null)

IconButton(
onClick = {
database.query {
val currentSong = song
if (currentSong == null) {
insert(mediaMetadata, SongEntity::toggleLike)
} else {
update(currentSong.song.toggleLike())
}
}
}
) {
Icon(
painter = painterResource(if (song?.song?.liked == true) R.drawable.favorite else R.drawable.favorite_border),
tint = if (song?.song?.liked == true) MaterialTheme.colorScheme.error else LocalContentColor.current,
contentDescription = null
)
}
}
)

HorizontalDivider()
}

GridMenu(
Expand Down

0 comments on commit 3f7d564

Please sign in to comment.