From b0e53445607081500065f2b24000494b24295af0 Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Tue, 20 Sep 2022 10:15:20 +0200 Subject: [PATCH] Add "Clear search history" entry (#310) --- .../kotlin/it/vfsfitvnm/vimusic/Database.kt | 6 +++++ .../screens/settings/OtherSettingsScreen.kt | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt index 15add7d..6d12504 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt @@ -114,6 +114,12 @@ interface Database { @Query("SELECT * FROM SearchQuery WHERE query LIKE :query ORDER BY id DESC") fun queries(query: String): Flow> + @Query("SELECT COUNT (*) FROM SearchQuery") + fun queriesCount(): Flow + + @Query("DELETE FROM SearchQuery") + fun clearQueries() + @Query("SELECT * FROM Song WHERE id = :id") fun song(id: String): Flow diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettingsScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettingsScreen.kt index 10a6618..0729292 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettingsScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettingsScreen.kt @@ -21,6 +21,7 @@ import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.text.BasicText import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -31,8 +32,10 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp import it.vfsfitvnm.route.RouteHandler +import it.vfsfitvnm.vimusic.Database import it.vfsfitvnm.vimusic.LocalPlayerAwarePaddingValues import it.vfsfitvnm.vimusic.R +import it.vfsfitvnm.vimusic.query import it.vfsfitvnm.vimusic.ui.components.TopAppBar import it.vfsfitvnm.vimusic.ui.screens.SettingsDescription import it.vfsfitvnm.vimusic.ui.screens.SettingsEntry @@ -45,6 +48,7 @@ import it.vfsfitvnm.vimusic.utils.isIgnoringBatteryOptimizations import it.vfsfitvnm.vimusic.utils.isInvincibilityEnabledKey import it.vfsfitvnm.vimusic.utils.rememberPreference import it.vfsfitvnm.vimusic.utils.semiBold +import kotlinx.coroutines.Dispatchers @ExperimentalAnimationApi @Composable @@ -59,6 +63,10 @@ fun OtherSettingsScreen() { val context = LocalContext.current val (colorPalette, typography) = LocalAppearance.current + val queriesCount by remember { + Database.queriesCount() + }.collectAsState(initial = 0, context = Dispatchers.IO) + var isInvincibilityEnabled by rememberPreference(isInvincibilityEnabledKey, false) var isIgnoringBatteryOptimizations by remember { @@ -100,6 +108,23 @@ fun OtherSettingsScreen() { .padding(all = 16.dp) ) + SettingsEntryGroupText(title = "SEARCH HISTORY") + + SettingsEntry( + title = "Clear search history", + text = if (queriesCount > 0) { + "Delete $queriesCount search queries" + } else { + "History is empty" + }, + isEnabled = queriesCount > 0, + onClick = { + query { + Database.clearQueries() + } + } + ) + SettingsEntryGroupText(title = "SERVICE LIFETIME") SettingsGroupDescription(text = "If battery optimizations are applied, the playback notification can suddenly disappear when paused.")