Add option to clear trending song (#413)

This commit is contained in:
vfsfitvnm 2022-10-13 12:34:18 +02:00
parent 3194fe3387
commit f318e71d20
4 changed files with 52 additions and 9 deletions

View file

@ -317,6 +317,15 @@ interface Database {
@RewriteQueriesToDropUnusedColumns
fun trending(now: Long = System.currentTimeMillis()): Flow<DetailedSong?>
@Query("SELECT COUNT (*) FROM Event")
fun eventsCount(): Flow<Int>
@Query("DELETE FROM Event")
fun clearEvents()
@Query("DELETE FROM Event WHERE songId = :songId")
fun clearEventsFor(songId: String)
@Insert
fun insert(event: Event)

View file

@ -134,6 +134,7 @@ fun NonQueuedMediaItemMenu(
modifier: Modifier = Modifier,
onRemoveFromPlaylist: (() -> Unit)? = null,
onHideFromDatabase: (() -> Unit)? = null,
onRemoveFromQuickPicks: (() -> Unit)? = null,
) {
val binder = LocalPlayerServiceBinder.current
@ -154,6 +155,7 @@ fun NonQueuedMediaItemMenu(
onEnqueue = { binder?.player?.enqueue(mediaItem) },
onRemoveFromPlaylist = onRemoveFromPlaylist,
onHideFromDatabase = onHideFromDatabase,
onRemoveFromQuickPicks = onRemoveFromQuickPicks,
modifier = modifier
)
}
@ -192,6 +194,7 @@ fun BaseMediaItemMenu(
onRemoveFromQueue: (() -> Unit)? = null,
onRemoveFromPlaylist: (() -> Unit)? = null,
onHideFromDatabase: (() -> Unit)? = null,
onRemoveFromQuickPicks: (() -> Unit)? = null,
) {
val context = LocalContext.current
@ -232,6 +235,7 @@ fun BaseMediaItemMenu(
context.startActivity(Intent.createChooser(sendIntent, null))
},
onRemoveFromQuickPicks = onRemoveFromQuickPicks,
modifier = modifier
)
}
@ -253,6 +257,7 @@ fun MediaItemMenu(
onAddToPlaylist: ((Playlist, Int) -> Unit)? = null,
onGoToAlbum: ((String) -> Unit)? = null,
onGoToArtist: ((String) -> Unit)? = null,
onRemoveFromQuickPicks: (() -> Unit)? = null,
onShare: () -> Unit
) {
val (colorPalette) = LocalAppearance.current
@ -670,6 +675,17 @@ fun MediaItemMenu(
onClick = onHideFromDatabase
)
}
onRemoveFromQuickPicks?.let {
MenuEntry(
icon = R.drawable.trash,
text = "Hide from \"Quick picks\"",
onClick = {
onDismiss()
onRemoveFromQuickPicks()
}
)
}
}
}
}

View file

@ -40,6 +40,7 @@ import it.vfsfitvnm.vimusic.Database
import it.vfsfitvnm.vimusic.LocalPlayerAwareWindowInsets
import it.vfsfitvnm.vimusic.LocalPlayerServiceBinder
import it.vfsfitvnm.vimusic.R
import it.vfsfitvnm.vimusic.query
import it.vfsfitvnm.vimusic.savers.DetailedSongSaver
import it.vfsfitvnm.vimusic.savers.InnertubeRelatedPageSaver
import it.vfsfitvnm.vimusic.savers.nullableSaver
@ -75,7 +76,6 @@ import it.vfsfitvnm.youtubemusic.models.bodies.NextBody
import it.vfsfitvnm.youtubemusic.requests.relatedPage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flowOn
@ExperimentalFoundationApi
@ -98,7 +98,6 @@ fun QuickPicks(
) {
Database.trending()
.flowOn(Dispatchers.IO)
.filterNotNull()
.distinctUntilChanged()
.collect { value = it }
}
@ -188,6 +187,11 @@ fun QuickPicks(
NonQueuedMediaItemMenu(
onDismiss = menuState::hide,
mediaItem = song.asMediaItem,
onRemoveFromQuickPicks = {
query {
Database.clearEventsFor(song.id)
}
}
)
}
},
@ -220,7 +224,7 @@ fun QuickPicks(
menuState.display {
NonQueuedMediaItemMenu(
onDismiss = menuState::hide,
mediaItem = song.asMediaItem,
mediaItem = song.asMediaItem
)
}
},

View file

@ -43,6 +43,13 @@ fun DatabaseSettings() {
val context = LocalContext.current
val (colorPalette) = LocalAppearance.current
val eventsCount by produceSaveableState(initialValue = 0, stateSaver = autoSaver()) {
Database.eventsCount()
.flowOn(Dispatchers.IO)
.distinctUntilChanged()
.collect { value = it }
}
val queriesCount by produceSaveableState(initialValue = 0, stateSaver = autoSaver()) {
Database.queriesCount()
.flowOn(Dispatchers.IO)
@ -100,7 +107,7 @@ fun DatabaseSettings() {
) {
Header(title = "Database")
SettingsEntryGroupText(title = "SEARCH HISTORY")
SettingsEntryGroupText(title = "CLEANUP")
SettingsEntry(
title = "Clear search history",
@ -110,11 +117,18 @@ fun DatabaseSettings() {
"History is empty"
},
isEnabled = queriesCount > 0,
onClick = {
query {
Database.clearQueries()
}
}
onClick = { query(Database::clearQueries) }
)
SettingsEntry(
title = "Reset quick picks",
text = if (eventsCount > 0) {
"Delete $eventsCount playback events"
} else {
"Quick picks are cleared"
},
isEnabled = eventsCount > 0,
onClick = { query(Database::clearEvents) }
)
SettingsGroupSpacer()