From 6dd88b796eb96c0c91537d8c2a2495f2d84797b4 Mon Sep 17 00:00:00 2001 From: Bnyro <82752168+Bnyro@users.noreply.github.com> Date: Sun, 16 Oct 2022 15:15:32 +0000 Subject: [PATCH] Add an option to pause the search history (#324, #455) --- .../vimusic/ui/screens/home/HomeScreen.kt | 10 +++-- .../vimusic/ui/screens/search/OnlineSearch.kt | 12 ++++-- .../ui/screens/settings/DatabaseSettings.kt | 25 ++--------- .../ui/screens/settings/OtherSettings.kt | 42 +++++++++++++++++-- .../it/vfsfitvnm/vimusic/utils/Preferences.kt | 1 + 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/home/HomeScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/home/HomeScreen.kt index 1e677ab..dc08831 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/home/HomeScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/home/HomeScreen.kt @@ -4,6 +4,7 @@ import androidx.compose.animation.ExperimentalAnimationApi import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.runtime.Composable import androidx.compose.runtime.saveable.rememberSaveableStateHolder +import androidx.compose.ui.platform.LocalContext import it.vfsfitvnm.route.RouteHandler import it.vfsfitvnm.route.defaultStacking import it.vfsfitvnm.route.defaultStill @@ -30,13 +31,14 @@ import it.vfsfitvnm.vimusic.ui.screens.searchRoute import it.vfsfitvnm.vimusic.ui.screens.searchresult.SearchResultScreen import it.vfsfitvnm.vimusic.ui.screens.settings.SettingsScreen import it.vfsfitvnm.vimusic.ui.screens.settingsRoute -import it.vfsfitvnm.vimusic.utils.homeScreenTabIndexKey -import it.vfsfitvnm.vimusic.utils.rememberPreference +import it.vfsfitvnm.vimusic.utils.* @ExperimentalFoundationApi @ExperimentalAnimationApi @Composable fun HomeScreen(onPlaylistUrl: (String) -> Unit) { + val context = LocalContext.current + val saveableStateHolder = rememberSaveableStateHolder() RouteHandler( @@ -88,7 +90,7 @@ fun HomeScreen(onPlaylistUrl: (String) -> Unit) { pop() searchResultRoute(query) - query { + if (!context.preferences.getBoolean(pauseSearchHistoryKey, false)) query { Database.insert(SearchQuery(query = query)) } }, @@ -113,7 +115,7 @@ fun HomeScreen(onPlaylistUrl: (String) -> Unit) { Item(2, "Playlists", R.drawable.playlist) Item(3, "Artists", R.drawable.person) Item(4, "Albums", R.drawable.disc) - }, + } ) { currentTabIndex -> saveableStateHolder.SaveableStateProvider(key = currentTabIndex) { when (currentTabIndex) { diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/search/OnlineSearch.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/search/OnlineSearch.kt index 10d2361..b9c285c 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/search/OnlineSearch.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/search/OnlineSearch.kt @@ -38,6 +38,7 @@ import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.SolidColor +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.text.TextRange import androidx.compose.ui.text.input.ImeAction @@ -57,6 +58,7 @@ import it.vfsfitvnm.vimusic.ui.components.themed.FloatingActionsContainerWithScr import it.vfsfitvnm.vimusic.ui.components.themed.Header import it.vfsfitvnm.vimusic.ui.components.themed.SecondaryTextButton import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance +import it.vfsfitvnm.vimusic.utils.* import it.vfsfitvnm.vimusic.utils.align import it.vfsfitvnm.vimusic.utils.center import it.vfsfitvnm.vimusic.utils.medium @@ -79,6 +81,8 @@ fun OnlineSearch( onViewPlaylist: (String) -> Unit, decorationBox: @Composable (@Composable () -> Unit) -> Unit ) { + val context = LocalContext.current + val (colorPalette, typography) = LocalAppearance.current val history by produceSaveableState( @@ -108,7 +112,7 @@ fun OnlineSearch( val isPlaylistUrl = listOf( "https://www.youtube.com/playlist?", "https://music.youtube.com/playlist?", - "https://m.youtube.com/playlist?", + "https://m.youtube.com/playlist?" ).any(textFieldValue.text::startsWith) if (isPlaylistUrl) textFieldValue.text.toUri().getQueryParameter("list") else null @@ -164,7 +168,7 @@ fun OnlineSearch( val isAlbum = playlistId.startsWith("OLAK5uy_") SecondaryTextButton( - text = "View ${if (isAlbum) "album" else "playlist"}", + text = "View ${if (isAlbum) "album" else "playlist"}", onClick = { onViewPlaylist(textFieldValue.text) } ) } @@ -176,7 +180,7 @@ fun OnlineSearch( if (textFieldValue.text.isNotEmpty()) { SecondaryTextButton( - text = "Clear", + text = "Clear", onClick = { onTextFieldValueChanged(TextFieldValue()) } ) } @@ -184,7 +188,7 @@ fun OnlineSearch( ) } - items( + if (!context.preferences.getBoolean(pauseSearchHistoryKey, false)) items( items = history, key = SearchQuery::id ) { searchQuery -> diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/DatabaseSettings.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/DatabaseSettings.kt index d03c91e..650a1b3 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/DatabaseSettings.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/DatabaseSettings.kt @@ -28,14 +28,14 @@ import it.vfsfitvnm.vimusic.ui.components.themed.Header import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance import it.vfsfitvnm.vimusic.utils.intent import it.vfsfitvnm.vimusic.utils.produceSaveableState +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.flowOn import java.io.FileInputStream import java.io.FileOutputStream import java.text.SimpleDateFormat import java.util.Date import kotlin.system.exitProcess -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.flowOn @ExperimentalAnimationApi @Composable @@ -50,13 +50,6 @@ fun DatabaseSettings() { .collect { value = it } } - val queriesCount by produceSaveableState(initialValue = 0, stateSaver = autoSaver()) { - Database.queriesCount() - .flowOn(Dispatchers.IO) - .distinctUntilChanged() - .collect { value = it } - } - val backupLauncher = rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("application/vnd.sqlite3")) { uri -> if (uri == null) return@rememberLauncherForActivityResult @@ -93,7 +86,6 @@ fun DatabaseSettings() { } } - Column( modifier = Modifier .background(colorPalette.background0) @@ -109,17 +101,6 @@ fun DatabaseSettings() { SettingsEntryGroupText(title = "CLEANUP") - SettingsEntry( - title = "Clear search history", - text = if (queriesCount > 0) { - "Delete $queriesCount search queries" - } else { - "History is empty" - }, - isEnabled = queriesCount > 0, - onClick = { query(Database::clearQueries) } - ) - SettingsEntry( title = "Reset quick picks", text = if (eventsCount > 0) { diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettings.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettings.kt index 82b37b0..431bf9d 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettings.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/settings/OtherSettings.kt @@ -29,12 +29,16 @@ import androidx.compose.foundation.layout.WindowInsetsSides import androidx.compose.foundation.layout.asPaddingValues import androidx.compose.foundation.layout.only import androidx.compose.runtime.SnapshotMutationPolicy +import androidx.compose.runtime.saveable.autoSaver +import it.vfsfitvnm.vimusic.Database +import it.vfsfitvnm.vimusic.query import it.vfsfitvnm.vimusic.service.PlayerMediaBrowserService import it.vfsfitvnm.vimusic.ui.components.themed.Header import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance -import it.vfsfitvnm.vimusic.utils.isIgnoringBatteryOptimizations -import it.vfsfitvnm.vimusic.utils.isInvincibilityEnabledKey -import it.vfsfitvnm.vimusic.utils.rememberPreference +import it.vfsfitvnm.vimusic.utils.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.flowOn @ExperimentalAnimationApi @Composable @@ -73,6 +77,18 @@ fun OtherSettings() { isIgnoringBatteryOptimizations = context.isIgnoringBatteryOptimizations } + var pauseSearchHistory by rememberPreference( + pauseSearchHistoryKey, + false + ) + + val queriesCount by produceSaveableState(initialValue = 0, stateSaver = autoSaver()) { + Database.queriesCount() + .flowOn(Dispatchers.IO) + .distinctUntilChanged() + .collect { value = it } + } + Column( modifier = Modifier .background(colorPalette.background0) @@ -147,5 +163,25 @@ fun OtherSettings() { isChecked = isInvincibilityEnabled, onCheckedChange = { isInvincibilityEnabled = it } ) + + SettingsEntryGroupText(title = "SEARCH HISTORY") + + SwitchSettingEntry( + title = "Pause search history", + text = "Neither save new searched queries nor show already existing items in the history", + isChecked = pauseSearchHistory, + onCheckedChange = { pauseSearchHistory = it } + ) + + SettingsEntry( + title = "Clear search history", + text = if (queriesCount > 0) { + "Delete $queriesCount search queries" + } else { + "History is empty" + }, + isEnabled = queriesCount > 0, + onClick = { query(Database::clearQueries) } + ) } } diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/Preferences.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/Preferences.kt index 2ad759f..163cca7 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/Preferences.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/utils/Preferences.kt @@ -34,6 +34,7 @@ const val isShowingThumbnailInLockscreenKey = "isShowingThumbnailInLockscreen" const val homeScreenTabIndexKey = "homeScreenTabIndex" const val searchResultScreenTabIndexKey = "searchResultScreenTabIndex" const val artistScreenTabIndexKey = "artistScreenTabIndex" +const val pauseSearchHistoryKey = "pauseSearchHistory" inline fun > SharedPreferences.getEnum( key: String,