diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/Database.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/Database.kt index b0f7020201..35f3f6dc98 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/Database.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/Database.kt @@ -194,19 +194,19 @@ interface Database { @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY artistsText") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY artistsText") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByArtistAsc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY artistsText DESC") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY artistsText DESC") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByArtistDesc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY totalPlayTimeMs") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY totalPlayTimeMs") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByPlayTimeAsc(): Flow> @@ -222,7 +222,7 @@ interface Database { @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY totalPlayTimeMs DESC") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY totalPlayTimeMs DESC") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByPlayTimeDesc(): Flow> @@ -238,63 +238,63 @@ interface Database { @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY title COLLATE NOCASE ASC") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY title COLLATE NOCASE ASC") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByTitleAsc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY title COLLATE NOCASE DESC") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY title COLLATE NOCASE DESC") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByTitleDesc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY ROWID") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY ROWID") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByRowIdAsc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY ROWID DESC") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY ROWID DESC") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByRowIdDesc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY likedAt") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY likedAt") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByLikedAtAsc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY likedAt DESC") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY likedAt DESC") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByLikedAtDesc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction @Query("SELECT DISTINCT S.* FROM Song S LEFT JOIN Event E ON E.songId=S.id " + - "WHERE likedAt IS NOT NULL " + + "WHERE likedAt IS NOT NULL AND likedAt > 0 " + "ORDER BY E.timestamp DESC") fun songsFavoritesByDatePlayedDesc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction @Query("SELECT DISTINCT S.* FROM Song S LEFT JOIN Event E ON E.songId=S.id " + - "WHERE likedAt IS NOT NULL " + + "WHERE likedAt IS NOT NULL AND likedAt > 0 " + "ORDER BY E.timestamp") fun songsFavoritesByDatePlayedAsc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY durationText") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY durationText") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByDurationAsc(): Flow> @SuppressWarnings(RoomWarnings.QUERY_MISMATCH) @Transaction - @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY durationText DESC") + @Query("SELECT * FROM Song WHERE likedAt IS NOT NULL AND likedAt > 0 ORDER BY durationText DESC") @RewriteQueriesToDropUnusedColumns fun songsFavoritesByDurationDesc(): Flow> @@ -1329,7 +1329,7 @@ interface Database { @Query("UPDATE Playlist SET name = REPLACE(name,'${PINNED_PREFIX}','') WHERE id = :playlistId") fun unPinPlaylist(playlistId: Long): Int - @Query("SELECT count(id) FROM Song WHERE id = :songId and likedAt IS NOT NULL") + @Query("SELECT count(id) FROM Song WHERE id = :songId and likedAt IS NOT NULL and likedAt > 0") fun songliked(songId: String): Int @Query("SELECT * FROM Song WHERE id = :id") @@ -1341,6 +1341,9 @@ interface Database { @Query("SELECT likedAt FROM Song WHERE id = :songId") fun likedAt(songId: String): Flow + @Query("SELECT likedAt FROM Song WHERE id = :songId") + fun getLikedAt(songId: String): Long? + @Query("UPDATE Album SET bookmarkedAt = :bookmarkedAt WHERE id = :id") fun bookmarkAlbum(id: String, bookmarkedAt: Long?): Int diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/models/Song.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/models/Song.kt index 37b5279fa9..da9ab230d1 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/models/Song.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/models/Song.kt @@ -5,6 +5,7 @@ import androidx.room.Entity import androidx.room.PrimaryKey import it.fast4x.rimusic.cleanPrefix import it.fast4x.rimusic.utils.durationTextToMillis +import it.fast4x.rimusic.utils.setDisLikeState import it.fast4x.rimusic.utils.setLikeState import kotlinx.serialization.Serializable @@ -40,6 +41,12 @@ data class Song( ) } + fun toggleDislike(): Song { + return copy( + likedAt = setDisLikeState(likedAt) + ) + } + fun cleanTitle() = cleanPrefix( this.title ) fun relativePlayTime(): Double { diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/IconButton.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/IconButton.kt index 31f6f92d74..39d55e70ff 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/IconButton.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/IconButton.kt @@ -1,9 +1,11 @@ package it.fast4x.rimusic.ui.components.themed import androidx.annotation.DrawableRes +import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.Image import androidx.compose.foundation.Indication import androidx.compose.foundation.clickable +import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size @@ -39,9 +41,11 @@ fun HeaderIconButton( ) } +@OptIn(ExperimentalFoundationApi::class) @Composable fun IconButton( onClick: () -> Unit, + onLongClick: (() -> Unit)? = null, @DrawableRes icon: Int, color: Color, modifier: Modifier = Modifier, @@ -53,11 +57,12 @@ fun IconButton( contentDescription = null, colorFilter = ColorFilter.tint(color), modifier = Modifier - .clickable( + .combinedClickable( indication = indication ?: ripple(bounded = false), interactionSource = remember { MutableInteractionSource() }, enabled = enabled, - onClick = onClick + onClick = onClick, + onLongClick = onLongClick ) .then(modifier) ) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/MediaItemGridMenu.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/MediaItemGridMenu.kt index aac81303bf..6e780fffe9 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/MediaItemGridMenu.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/MediaItemGridMenu.kt @@ -92,8 +92,13 @@ import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import it.fast4x.rimusic.colorPalette +import it.fast4x.rimusic.context +import it.fast4x.rimusic.models.Song +import it.fast4x.rimusic.service.MyDownloadHelper import it.fast4x.rimusic.typography import it.fast4x.rimusic.ui.screens.settings.isYouTubeSyncEnabled +import it.fast4x.rimusic.utils.getLikeState +import it.fast4x.rimusic.utils.setDisLikeState @OptIn(UnstableApi::class) @Composable @@ -304,14 +309,13 @@ fun MediaItemGridMenu ( val isLocal by remember { derivedStateOf { mediaItem.isLocal } } - var updateData by remember { - mutableStateOf(false) - } var likedAt by remember { mutableStateOf(null) } - LaunchedEffect(Unit, mediaItem.mediaId, updateData) { - Database.likedAt(mediaItem.mediaId).collect { likedAt = it } + LaunchedEffect(Unit, mediaItem.mediaId) { + Database.likedAt(mediaItem.mediaId).collect { + likedAt = it + } } var downloadState by remember { @@ -434,11 +438,22 @@ fun MediaItemGridMenu ( Column(verticalArrangement = Arrangement.spacedBy(4.dp)) { IconButton( - icon = if (likedAt == null) R.drawable.heart_outline else R.drawable.heart, + icon = getLikeState(mediaItem.mediaId), color = colorPalette().favoritesIcon, onClick = { mediaItemToggleLike(mediaItem) - updateData = !updateData + Database.asyncQuery { + if(songliked(mediaItem.mediaId) != 0){ + MyDownloadHelper.autoDownloadWhenLiked(context(), mediaItem) + } + } + }, + onLongClick = { + Database.asyncTransaction { + if (like(mediaItem.mediaId, setDisLikeState(likedAt)) == 0) { + insert(mediaItem, Song::toggleDislike) + } + } }, modifier = Modifier .padding(all = 4.dp) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/MediaItemMenu.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/MediaItemMenu.kt index c69e818273..4b9ef7715a 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/MediaItemMenu.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/components/themed/MediaItemMenu.kt @@ -118,6 +118,8 @@ import it.fast4x.rimusic.context import it.fast4x.rimusic.service.MyDownloadHelper import it.fast4x.rimusic.typography import it.fast4x.rimusic.ui.screens.settings.isYouTubeSyncEnabled +import it.fast4x.rimusic.utils.mediaItemToggleLike +import it.fast4x.rimusic.utils.setDisLikeState import timber.log.Timber import java.time.LocalTime.now import java.time.format.DateTimeFormatter @@ -1130,12 +1132,19 @@ fun MediaItemMenu( color = colorPalette().favoritesIcon, //color = if (likedAt == null) colorPalette().textDisabled else colorPalette().text, onClick = { + mediaItemToggleLike(mediaItem) + Database.asyncQuery { + if(songliked(mediaItem.mediaId) != 0){ + MyDownloadHelper.autoDownloadWhenLiked(context(), mediaItem) + } + } + }, + onLongClick = { Database.asyncTransaction { - if ( like( mediaItem.mediaId, setLikeState(likedAt) ) == 0 ) { - insert(mediaItem, Song::toggleLike) + if (like(mediaItem.mediaId, setDisLikeState(likedAt)) == 0) { + insert(mediaItem, Song::toggleDislike) } } - MyDownloadHelper.autoDownloadWhenLiked(context(),mediaItem) }, modifier = Modifier .padding(all = 4.dp) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/album/AlbumDetails.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/album/AlbumDetails.kt index c2f56b00a3..9c23c5d6cf 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/album/AlbumDetails.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/album/AlbumDetails.kt @@ -143,6 +143,7 @@ import it.fast4x.rimusic.models.SongAlbumMap import it.fast4x.rimusic.service.MyDownloadHelper import it.fast4x.rimusic.typography import it.fast4x.rimusic.ui.screens.settings.isYouTubeSyncEnabled +import it.fast4x.rimusic.utils.mediaItemSetLiked import it.fast4x.rimusic.utils.mediaItemToggleLike import kotlinx.coroutines.flow.first import timber.log.Timber @@ -989,12 +990,9 @@ fun AlbumDetails( }, onAddToFavourites = { songs.forEach { song -> - - val likedAt: Long? = song.likedAt - if(likedAt == null) { - mediaItemToggleLike(song.asMediaItem) - } - } + mediaItemSetLiked(song.asMediaItem) + } + SmartMessage(context.resources.getString(R.string.done), context = context) }, disableScrollingText = disableScrollingText ) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/localplaylist/LocalPlaylistSongsModern.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/localplaylist/LocalPlaylistSongsModern.kt index 8ebfd70495..e92fbb16ef 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/localplaylist/LocalPlaylistSongsModern.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/localplaylist/LocalPlaylistSongsModern.kt @@ -189,6 +189,7 @@ import it.fast4x.rimusic.utils.isNowPlaying import it.fast4x.rimusic.utils.saveImageToInternalStorage import kotlinx.coroutines.CoroutineScope import it.fast4x.rimusic.models.SongEntity +import it.fast4x.rimusic.utils.mediaItemSetLiked import it.fast4x.rimusic.utils.mediaItemToggleLike import kotlinx.coroutines.flow.map @@ -1287,10 +1288,9 @@ fun LocalPlaylistSongsModern( }, onAddToPreferites = { playlistSongs.forEachIndexed { _, song -> - if(song.song.likedAt == null) { - mediaItemToggleLike(song.asMediaItem) - } + mediaItemSetLiked(song.asMediaItem) } + SmartMessage(context.resources.getString(R.string.done), context = context) }, onRenumberPositions = { if (playlistNotMonthlyType) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/player/components/controls/Essential.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/player/components/controls/Essential.kt index 5a099186da..6a6e88e67d 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/player/components/controls/Essential.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/player/components/controls/Essential.kt @@ -1,5 +1,6 @@ package it.fast4x.rimusic.ui.screens.player.components.controls +import android.annotation.SuppressLint import androidx.compose.animation.core.LinearEasing import androidx.compose.animation.core.animateDp import androidx.compose.animation.core.tween @@ -85,6 +86,7 @@ import it.fast4x.rimusic.utils.getIconQueueLoopState import it.fast4x.rimusic.utils.getLikeState import it.fast4x.rimusic.utils.getUnlikedIcon import it.fast4x.rimusic.utils.jumpPreviousKey +import it.fast4x.rimusic.utils.mediaItemToggleLike import it.fast4x.rimusic.utils.playNext import it.fast4x.rimusic.utils.playPrevious import it.fast4x.rimusic.utils.playerBackgroundColorsKey @@ -92,6 +94,7 @@ import it.fast4x.rimusic.utils.playerControlsTypeKey import it.fast4x.rimusic.utils.queueLoopTypeKey import it.fast4x.rimusic.utils.rememberPreference import it.fast4x.rimusic.utils.semiBold +import it.fast4x.rimusic.utils.setDisLikeState import it.fast4x.rimusic.utils.setLikeState import it.fast4x.rimusic.utils.setQueueLoopState import it.fast4x.rimusic.utils.showthumbnailKey @@ -99,6 +102,7 @@ import it.fast4x.rimusic.utils.textCopyToClipboard import it.fast4x.rimusic.utils.textoutlineKey +@SuppressLint("UnusedBoxWithConstraintsScope") @UnstableApi @ExperimentalFoundationApi @Composable @@ -254,33 +258,45 @@ fun InfoAlbumAndArtistEssential( icon = getLikeState(mediaId), onClick = { val currentMediaItem = binder.player.currentMediaItem - Database.asyncTransaction { - if (like(mediaId, setLikeState(likedAt)) == 0) { - currentMediaItem - ?.takeIf { it.mediaId == mediaId } - ?.let { - insert(currentMediaItem, Song::toggleLike) + currentMediaItem?.takeIf { it.mediaId == mediaId }.let { mediaItem -> + if (mediaItem != null) { + mediaItemToggleLike(mediaItem) + Database.asyncQuery { + if(songliked(mediaId) != 0){ + MyDownloadHelper.autoDownloadWhenLiked(context(), mediaItem) } - if (currentMediaItem != null) { - MyDownloadHelper.autoDownloadWhenLiked(context(),currentMediaItem) } } } if (effectRotationEnabled) isRotated = !isRotated }, - modifier = Modifier - .padding(start = 5.dp) - .size(24.dp) - ) - if (playerBackgroundColors == PlayerBackgroundColors.BlurredCoverColor) { - Icon( - painter = painterResource(id = getUnlikedIcon()), - tint = colorPalette().text, - contentDescription = null, + onLongClick = { + val currentMediaItem = binder.player.currentMediaItem + Database.asyncTransaction { + if (like(mediaId, setDisLikeState(likedAt)) == 0) { + currentMediaItem + ?.takeIf { it.mediaId == mediaId } + ?.let { + insert(currentMediaItem, Song::toggleDislike) + } + } + } + if (effectRotationEnabled) isRotated = !isRotated + }, modifier = Modifier .padding(start = 5.dp) .size(24.dp) ) + if (playerBackgroundColors == PlayerBackgroundColors.BlurredCoverColor) { + Icon( + painter = painterResource(id = getUnlikedIcon()), + tint = colorPalette().text, + contentDescription = null, + modifier = Modifier + .padding(start = 5.dp) + .size(24.dp) + ) + } } } @@ -416,13 +432,27 @@ fun ControlsEssential( color = colorPalette().favoritesIcon, icon = getLikeState(mediaId), onClick = { + val currentMediaItem = binder.player.currentMediaItem + currentMediaItem?.takeIf { it.mediaId == mediaId }.let { mediaItem -> + if (mediaItem != null) { + mediaItemToggleLike(mediaItem) + Database.asyncQuery { + if(songliked(mediaId) != 0){ + MyDownloadHelper.autoDownloadWhenLiked(context(), mediaItem) + } + } + } + } + if (effectRotationEnabled) isRotated = !isRotated + }, + onLongClick = { val currentMediaItem = binder.player.currentMediaItem Database.asyncTransaction { - if ( like( mediaId, setLikeState(likedAt) ) == 0 ) { + if (like(mediaId, setDisLikeState(likedAt)) == 0) { currentMediaItem ?.takeIf { it.mediaId == mediaId } ?.let { - insert(currentMediaItem, Song::toggleLike) + insert(currentMediaItem, Song::toggleDislike) } } } diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/player/components/controls/Modern.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/player/components/controls/Modern.kt index f806605906..8057bceb43 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/player/components/controls/Modern.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/player/components/controls/Modern.kt @@ -76,6 +76,7 @@ import it.fast4x.rimusic.utils.effectRotationKey import it.fast4x.rimusic.utils.getLikeState import it.fast4x.rimusic.utils.getUnlikedIcon import it.fast4x.rimusic.utils.jumpPreviousKey +import it.fast4x.rimusic.utils.mediaItemToggleLike import it.fast4x.rimusic.utils.playNext import it.fast4x.rimusic.utils.playPrevious import it.fast4x.rimusic.utils.playerBackgroundColorsKey @@ -83,6 +84,7 @@ import it.fast4x.rimusic.utils.playerControlsTypeKey import it.fast4x.rimusic.utils.playerInfoShowIconsKey import it.fast4x.rimusic.utils.rememberPreference import it.fast4x.rimusic.utils.semiBold +import it.fast4x.rimusic.utils.setDisLikeState import it.fast4x.rimusic.utils.setLikeState import it.fast4x.rimusic.utils.showthumbnailKey import it.fast4x.rimusic.utils.textCopyToClipboard @@ -231,17 +233,28 @@ fun InfoAlbumAndArtistModern( color = colorPalette().favoritesIcon, icon = getLikeState(mediaId), onClick = { + val currentMediaItem = binder.player.currentMediaItem + currentMediaItem?.takeIf { it.mediaId == mediaId }.let { mediaItem -> + if (mediaItem != null) { + mediaItemToggleLike(mediaItem) + Database.asyncQuery { + if(songliked(mediaId) != 0){ + MyDownloadHelper.autoDownloadWhenLiked(context(), mediaItem) + } + } + } + } + if (effectRotationEnabled) isRotated = !isRotated + }, + onLongClick = { val currentMediaItem = binder.player.currentMediaItem Database.asyncTransaction { - if ( like( mediaId, setLikeState(likedAt) ) == 0 ) { + if (like(mediaId, setDisLikeState(likedAt)) == 0) { currentMediaItem ?.takeIf { it.mediaId == mediaId } ?.let { - insert(currentMediaItem, Song::toggleLike) + insert(currentMediaItem, Song::toggleDislike) } - if (currentMediaItem != null) { - MyDownloadHelper.autoDownloadWhenLiked(context(),currentMediaItem) - } } } if (effectRotationEnabled) isRotated = !isRotated diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/playlist/PlaylistSongList.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/playlist/PlaylistSongList.kt index f17f9d7d92..f4d45b6c0f 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/playlist/PlaylistSongList.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/playlist/PlaylistSongList.kt @@ -138,6 +138,7 @@ import it.fast4x.rimusic.colorPalette import it.fast4x.rimusic.models.Song import it.fast4x.rimusic.typography import it.fast4x.rimusic.ui.screens.settings.isYouTubeSyncEnabled +import it.fast4x.rimusic.utils.mediaItemSetLiked import it.fast4x.rimusic.utils.setLikeState import timber.log.Timber @@ -662,11 +663,7 @@ fun PlaylistSongList( .combinedClickable( onClick = { playlistPage!!.songsPage?.items?.forEachIndexed { _, song -> - Database.asyncTransaction { - if ( like( song.asMediaItem.mediaId, setLikeState(song.asSong.likedAt) ) == 0 ) { - insert(song.asMediaItem, Song::toggleLike) - } - } + mediaItemSetLiked(song.asMediaItem) } SmartMessage(context.resources.getString(R.string.done), context = context) }, diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/LikeState.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/LikeState.kt index 60ed97b9af..daf5a4876c 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/LikeState.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/LikeState.kt @@ -36,6 +36,15 @@ fun setLikeState(likedAt: Long?): Long? { } //println("mediaItem setLikeState: $current") return current +} +fun setDisLikeState(likedAt: Long?): Long? { + val current = + when (likedAt) { + -1L -> null + null -> -1L + else -> -1L + } + return current } diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/Utils.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/Utils.kt index 7a25155be6..30a9cbbac9 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/Utils.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/Utils.kt @@ -83,18 +83,28 @@ fun songToggleLike( song: Song ) { fun mediaItemToggleLike( mediaItem: MediaItem ) { Database.asyncTransaction { if (songExist(mediaItem.mediaId) == 0) - insert(mediaItem, Song::toggleLike) - //else { - if (songliked(mediaItem.mediaId) == 0) - like( - mediaItem.mediaId, - System.currentTimeMillis() - ) - else like( + insert(mediaItem) + if (getLikedAt(mediaItem.mediaId) in listOf(null, -1L)) + like( mediaItem.mediaId, - null + System.currentTimeMillis() + ) + else like( + mediaItem.mediaId, + null + ) + } +} + +fun mediaItemSetLiked( mediaItem: MediaItem ) { + Database.asyncTransaction { + if (songExist(mediaItem.mediaId) == 0) + insert(mediaItem) + if (getLikedAt(mediaItem.mediaId) in listOf(null, -1L)) + like( + mediaItem.mediaId, + System.currentTimeMillis() ) - //} } }