diff --git a/app/src/main/java/org/schabi/newpipe/settings/ComposeHistoryActions.kt b/app/src/main/java/org/schabi/newpipe/settings/ComposeHistoryActions.kt new file mode 100644 index 000000000..c0aa79453 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/settings/ComposeHistoryActions.kt @@ -0,0 +1,61 @@ +/* +* SPDX-FileCopyrightText: 2026 NewPipe e.V. +* SPDX-License-Identifier: GPL-3.0-or-later +*/ + +package org.schabi.newpipe.settings + +import android.content.Context +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.rx3.await +import net.newpipe.app.platform.HistoryActions +import org.schabi.newpipe.DownloaderImpl +import org.schabi.newpipe.error.ErrorInfo +import org.schabi.newpipe.error.ErrorUtil +import org.schabi.newpipe.error.ReCaptchaActivity +import org.schabi.newpipe.error.UserAction +import org.schabi.newpipe.local.history.HistoryRecordManager +import org.schabi.newpipe.util.InfoCache + +@Suppress("unused") +class ComposeHistoryActions(private val context: Context) : HistoryActions { + + private val recordManager = HistoryRecordManager(context) + + override fun wipeMetadataCache() = InfoCache.getInstance().clearCache() + + override suspend fun clearWatchHistory() = runDeletion(REQUEST_WATCH_HISTORY) { + recordManager.deleteCompleteStreamStateHistory().await() + recordManager.deleteWholeStreamHistory().await() + recordManager.removeOrphanedRecords().await() + } + + override suspend fun clearPlaybackStates() = runDeletion(REQUEST_PLAYBACK_STATES) { + recordManager.deleteCompleteStreamStateHistory().await() + } + + override suspend fun clearSearchHistory() = runDeletion(REQUEST_SEARCH_HISTORY) { + recordManager.deleteCompleteSearchHistory().await() + } + + override fun clearRecaptchaCookies() = DownloaderImpl.getInstance().setCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY, "") + + private suspend fun runDeletion(request: String, deletion: suspend () -> Unit): Boolean = try { + deletion() + true + } catch (cancellation: CancellationException) { + throw cancellation + } catch (error: Exception) { + ErrorUtil.openActivity( + context, + ErrorInfo(error, UserAction.DELETE_FROM_HISTORY, request) + ) + false + } + + private companion object { + const val REQUEST_WATCH_HISTORY = "Delete from history" + const val REQUEST_PLAYBACK_STATES = "Delete playback states" + const val REQUEST_SEARCH_HISTORY = "Delete search history" + } +} diff --git a/shared/src/commonMain/composeResources/values/strings.xml b/shared/src/commonMain/composeResources/values/strings.xml index 014417a0e..a861a101c 100644 --- a/shared/src/commonMain/composeResources/values/strings.xml +++ b/shared/src/commonMain/composeResources/values/strings.xml @@ -217,4 +217,37 @@ No appropriate file manager was found for this action.\nPlease install a file manager or try to disable '%1$s' in the download settings No appropriate file manager was found for this action.\nPlease install a Storage Access Framework compatible file manager + + Watch history + Keep track of watched videos + Resume playback + Restore last playback position + Positions in lists + Show playback position indicators in lists + Search history + Store search queries locally + + + Clear data + Delete + Wipe cached metadata + Remove all cached webpage data + Metadata cache wiped + Clear watch history + Deletes the history of played streams and the playback positions + Delete entire watch history? + Watch history deleted + Delete playback positions + Deletes all playback positions + Delete all playback positions? + Playback positions deleted + Clear search history + Deletes history of search keywords + Delete entire search history? + Search history deleted + Clear reCAPTCHA cookies + Clear cookies that NewPipe stores when you solve a reCAPTCHA + reCAPTCHA cookies have been cleared + + diff --git a/shared/src/commonMain/kotlin/net/newpipe/app/navigation/Destination.kt b/shared/src/commonMain/kotlin/net/newpipe/app/navigation/Destination.kt index 819aab820..4d38b2ae0 100644 --- a/shared/src/commonMain/kotlin/net/newpipe/app/navigation/Destination.kt +++ b/shared/src/commonMain/kotlin/net/newpipe/app/navigation/Destination.kt @@ -14,6 +14,9 @@ import kotlinx.serialization.Serializable @Serializable sealed interface Destination : NavKey { + @Serializable + data object HistorySettings : Destination + @Serializable data object DownloadSettings : Destination diff --git a/shared/src/commonMain/kotlin/net/newpipe/app/navigation/NavModule.kt b/shared/src/commonMain/kotlin/net/newpipe/app/navigation/NavModule.kt index a21c8e14d..15ea33eca 100644 --- a/shared/src/commonMain/kotlin/net/newpipe/app/navigation/NavModule.kt +++ b/shared/src/commonMain/kotlin/net/newpipe/app/navigation/NavModule.kt @@ -11,6 +11,7 @@ import net.newpipe.app.screen.about.AboutScreen import net.newpipe.app.screen.settings.AppearanceSettingsScreen import net.newpipe.app.screen.settings.DebugSettingsScreen import net.newpipe.app.screen.settings.DownloadSettingsScreen +import net.newpipe.app.screen.settings.HistorySettingsScreen import net.newpipe.app.screen.settings.PlayerSettingsScreen import net.newpipe.app.screen.settings.SettingsHomeScreen import net.newpipe.app.screen.settings.VideoAudioSettingsScreen @@ -57,6 +58,10 @@ fun navModule() = module { navigation { DownloadSettingsScreen() } + + navigation { + HistorySettingsScreen() + } } /** diff --git a/shared/src/commonMain/kotlin/net/newpipe/app/screen/settings/HistorySettingsScreen.kt b/shared/src/commonMain/kotlin/net/newpipe/app/screen/settings/HistorySettingsScreen.kt new file mode 100644 index 000000000..b39701acc --- /dev/null +++ b/shared/src/commonMain/kotlin/net/newpipe/app/screen/settings/HistorySettingsScreen.kt @@ -0,0 +1,319 @@ +/* + * SPDX-FileCopyrightText: 2026 NewPipe e.V. + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package net.newpipe.app.screen.settings + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.WindowInsets +import androidx.compose.foundation.layout.asPaddingValues +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.navigationBars +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material3.AlertDialog +import androidx.compose.material3.Scaffold +import androidx.compose.material3.SnackbarHost +import androidx.compose.material3.SnackbarHostState +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.PreviewLightDark +import androidx.compose.ui.tooling.preview.PreviewWrapper +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import kotlinx.coroutines.launch +import net.newpipe.app.composable.PreferenceCategoryTitle +import net.newpipe.app.composable.PreferenceRow +import net.newpipe.app.composable.SwitchPreference +import net.newpipe.app.composable.TopAppBar +import net.newpipe.app.navigation.Navigator +import net.newpipe.app.preferences.HistoryPreferences.DEFAULT_ENABLE_PLAYBACK_RESUME +import net.newpipe.app.preferences.HistoryPreferences.DEFAULT_ENABLE_PLAYBACK_STATE_LISTS +import net.newpipe.app.preferences.HistoryPreferences.DEFAULT_ENABLE_SEARCH_HISTORY +import net.newpipe.app.preferences.HistoryPreferences.DEFAULT_ENABLE_WATCH_HISTORY +import net.newpipe.app.preview.ThemePreviewProvider +import net.newpipe.app.viewmodel.settings.HistoryMessage +import net.newpipe.app.viewmodel.settings.HistorySettingsViewModel +import newpipe.shared.generated.resources.Res +import newpipe.shared.generated.resources.cancel +import newpipe.shared.generated.resources.clear_cookie_summary +import newpipe.shared.generated.resources.clear_cookie_title +import newpipe.shared.generated.resources.clear_playback_states_summary +import newpipe.shared.generated.resources.clear_playback_states_title +import newpipe.shared.generated.resources.clear_search_history_summary +import newpipe.shared.generated.resources.clear_search_history_title +import newpipe.shared.generated.resources.clear_views_history_summary +import newpipe.shared.generated.resources.clear_views_history_title +import newpipe.shared.generated.resources.delete +import newpipe.shared.generated.resources.delete_playback_states_alert +import newpipe.shared.generated.resources.delete_search_history_alert +import newpipe.shared.generated.resources.delete_view_history_alert +import newpipe.shared.generated.resources.enable_playback_resume_summary +import newpipe.shared.generated.resources.enable_playback_resume_title +import newpipe.shared.generated.resources.enable_playback_state_lists_summary +import newpipe.shared.generated.resources.enable_playback_state_lists_title +import newpipe.shared.generated.resources.enable_search_history_summary +import newpipe.shared.generated.resources.enable_search_history_title +import newpipe.shared.generated.resources.enable_watch_history_summary +import newpipe.shared.generated.resources.enable_watch_history_title +import newpipe.shared.generated.resources.metadata_cache_wipe_complete_notice +import newpipe.shared.generated.resources.metadata_cache_wipe_summary +import newpipe.shared.generated.resources.metadata_cache_wipe_title +import newpipe.shared.generated.resources.recaptcha_cookies_cleared +import newpipe.shared.generated.resources.search_history_deleted +import newpipe.shared.generated.resources.settings_category_clear_data_title +import newpipe.shared.generated.resources.settings_category_history_title +import newpipe.shared.generated.resources.watch_history_deleted +import newpipe.shared.generated.resources.watch_history_states_deleted +import org.jetbrains.compose.resources.stringResource +import org.koin.compose.koinInject +import org.koin.compose.viewmodel.koinViewModel + +enum class HistoryClearTarget { + WATCH_HISTORY, + PLAYBACK_STATES, + SEARCH_HISTORY +} + +@Composable +fun HistorySettingsScreen( + navigator: Navigator = koinInject(), + viewModel: HistorySettingsViewModel = koinViewModel() +) { + val watchHistoryEnabled by viewModel.watchHistoryEnabled.collectAsStateWithLifecycle() + val playbackResumeEnabled by viewModel.playbackResumeEnabled.collectAsStateWithLifecycle() + val playbackStateListsEnabled by viewModel.playbackStateListsEnabled + .collectAsStateWithLifecycle() + val searchHistoryEnabled by viewModel.searchHistoryEnabled.collectAsStateWithLifecycle() + val recaptchaCookiesPresent by viewModel.recaptchaCookiesPresent + .collectAsStateWithLifecycle() + val message by viewModel.message.collectAsStateWithLifecycle() + + var pendingClear by remember { mutableStateOf(null) } + + HistorySettingsScreenContent( + watchHistoryEnabled = watchHistoryEnabled, + playbackResumeEnabled = playbackResumeEnabled, + playbackStateListsEnabled = playbackStateListsEnabled, + searchHistoryEnabled = searchHistoryEnabled, + recaptchaCookiesPresent = recaptchaCookiesPresent, + message = message, + pendingClear = pendingClear, + onWatchHistoryEnabledChange = viewModel::setWatchHistoryEnabled, + onPlaybackResumeEnabledChange = viewModel::setPlaybackResumeEnabled, + onPlaybackStateListsEnabledChange = viewModel::setPlaybackStateListsEnabled, + onSearchHistoryEnabledChange = viewModel::setSearchHistoryEnabled, + onWipeMetadataCache = viewModel::wipeMetadataCache, + onRequestClear = { target -> pendingClear = target }, + onConfirmClear = { target -> + when (target) { + HistoryClearTarget.WATCH_HISTORY -> viewModel.clearWatchHistory() + HistoryClearTarget.PLAYBACK_STATES -> viewModel.clearPlaybackStates() + HistoryClearTarget.SEARCH_HISTORY -> viewModel.clearSearchHistory() + } + }, + onDismissClear = { pendingClear = null }, + onClearRecaptchaCookies = viewModel::clearRecaptchaCookies, + onMessageShown = viewModel::onMessageShown, + onNavigateUp = { navigator.navigateUp() } + ) +} + +@Composable +fun HistorySettingsScreenContent( + watchHistoryEnabled: Boolean = DEFAULT_ENABLE_WATCH_HISTORY, + playbackResumeEnabled: Boolean = DEFAULT_ENABLE_PLAYBACK_RESUME, + playbackStateListsEnabled: Boolean = DEFAULT_ENABLE_PLAYBACK_STATE_LISTS, + searchHistoryEnabled: Boolean = DEFAULT_ENABLE_SEARCH_HISTORY, + recaptchaCookiesPresent: Boolean = false, + message: HistoryMessage? = null, + pendingClear: HistoryClearTarget? = null, + onWatchHistoryEnabledChange: (Boolean) -> Unit = {}, + onPlaybackResumeEnabledChange: (Boolean) -> Unit = {}, + onPlaybackStateListsEnabledChange: (Boolean) -> Unit = {}, + onSearchHistoryEnabledChange: (Boolean) -> Unit = {}, + onWipeMetadataCache: () -> Unit = {}, + onRequestClear: (HistoryClearTarget) -> Unit = {}, + onConfirmClear: (HistoryClearTarget) -> Unit = {}, + onDismissClear: () -> Unit = {}, + onClearRecaptchaCookies: () -> Unit = {}, + onMessageShown: () -> Unit = {}, + onNavigateUp: () -> Unit = {} +) { + val scope = rememberCoroutineScope() + val snackbarHostState = remember { SnackbarHostState() } + + val messageText = message?.let { pending -> + when (pending) { + HistoryMessage.METADATA_CACHE_WIPED -> { + stringResource(Res.string.metadata_cache_wipe_complete_notice) + } + + HistoryMessage.WATCH_HISTORY_DELETED -> { + stringResource(Res.string.watch_history_deleted) + } + + HistoryMessage.PLAYBACK_STATES_DELETED -> { + stringResource(Res.string.watch_history_states_deleted) + } + + HistoryMessage.SEARCH_HISTORY_DELETED -> { + stringResource(Res.string.search_history_deleted) + } + + HistoryMessage.RECAPTCHA_COOKIES_CLEARED -> { + stringResource(Res.string.recaptcha_cookies_cleared) + } + } + } + + LaunchedEffect(message) { + if (messageText == null) { + return@LaunchedEffect + } + // Showing the snackbar from the surrounding scope instead of this effect, so that marking + // the message as seen below cannot cancel it by restarting the effect + scope.launch { snackbarHostState.showSnackbar(messageText) } + onMessageShown() + } + + Scaffold( + topBar = { + TopAppBar( + title = stringResource(Res.string.settings_category_history_title), + onNavigateUp = onNavigateUp + ) + }, + snackbarHost = { SnackbarHost(snackbarHostState) } + ) { paddingValues -> + Column( + modifier = Modifier + .fillMaxSize() + .padding(top = paddingValues.calculateTopPadding()) + .verticalScroll(rememberScrollState()) + .padding(WindowInsets.navigationBars.asPaddingValues()) + ) { + SwitchPreference( + title = stringResource(Res.string.enable_watch_history_title), + summary = stringResource(Res.string.enable_watch_history_summary), + checked = watchHistoryEnabled, + onCheckedChange = onWatchHistoryEnabledChange + ) + + SwitchPreference( + title = stringResource(Res.string.enable_playback_resume_title), + summary = stringResource(Res.string.enable_playback_resume_summary), + checked = playbackResumeEnabled, + onCheckedChange = onPlaybackResumeEnabledChange, + enabled = watchHistoryEnabled + ) + SwitchPreference( + title = stringResource(Res.string.enable_playback_state_lists_title), + summary = stringResource(Res.string.enable_playback_state_lists_summary), + checked = playbackStateListsEnabled, + onCheckedChange = onPlaybackStateListsEnabledChange, + enabled = watchHistoryEnabled + ) + SwitchPreference( + title = stringResource(Res.string.enable_search_history_title), + summary = stringResource(Res.string.enable_search_history_summary), + checked = searchHistoryEnabled, + onCheckedChange = onSearchHistoryEnabledChange + ) + + PreferenceCategoryTitle( + title = stringResource(Res.string.settings_category_clear_data_title) + ) + + PreferenceRow( + title = stringResource(Res.string.metadata_cache_wipe_title), + summary = stringResource(Res.string.metadata_cache_wipe_summary), + onClick = onWipeMetadataCache + ) + PreferenceRow( + title = stringResource(Res.string.clear_views_history_title), + summary = stringResource(Res.string.clear_views_history_summary), + onClick = { onRequestClear(HistoryClearTarget.WATCH_HISTORY) } + ) + PreferenceRow( + title = stringResource(Res.string.clear_playback_states_title), + summary = stringResource(Res.string.clear_playback_states_summary), + onClick = { onRequestClear(HistoryClearTarget.PLAYBACK_STATES) } + ) + PreferenceRow( + title = stringResource(Res.string.clear_search_history_title), + summary = stringResource(Res.string.clear_search_history_summary), + onClick = { onRequestClear(HistoryClearTarget.SEARCH_HISTORY) } + ) + PreferenceRow( + title = stringResource(Res.string.clear_cookie_title), + summary = stringResource(Res.string.clear_cookie_summary), + enabled = recaptchaCookiesPresent, + onClick = onClearRecaptchaCookies + ) + } + } + + if (pendingClear != null) { + ClearDataConfirmationDialog( + target = pendingClear, + onConfirm = { + onConfirmClear(pendingClear) + onDismissClear() + }, + onDismiss = onDismissClear + ) + } +} + +@Composable +private fun ClearDataConfirmationDialog( + target: HistoryClearTarget, + onConfirm: () -> Unit, + onDismiss: () -> Unit +) { + val title = when (target) { + HistoryClearTarget.WATCH_HISTORY -> { + stringResource(Res.string.delete_view_history_alert) + } + + HistoryClearTarget.PLAYBACK_STATES -> { + stringResource(Res.string.delete_playback_states_alert) + } + + HistoryClearTarget.SEARCH_HISTORY -> { + stringResource(Res.string.delete_search_history_alert) + } + } + + AlertDialog( + onDismissRequest = onDismiss, + title = { Text(text = title) }, + confirmButton = { + TextButton(onClick = onConfirm) { + Text(text = stringResource(Res.string.delete)) + } + }, + dismissButton = { + TextButton(onClick = onDismiss) { + Text(text = stringResource(Res.string.cancel)) + } + } + ) +} + +@PreviewWrapper(ThemePreviewProvider::class) +@PreviewLightDark +@Composable +private fun HistorySettingsScreenPreview() { + HistorySettingsScreenContent() +} diff --git a/shared/src/commonMain/kotlin/net/newpipe/app/screen/settings/SettingsHomeScreen.kt b/shared/src/commonMain/kotlin/net/newpipe/app/screen/settings/SettingsHomeScreen.kt index 0f5ba5373..61a132472 100644 --- a/shared/src/commonMain/kotlin/net/newpipe/app/screen/settings/SettingsHomeScreen.kt +++ b/shared/src/commonMain/kotlin/net/newpipe/app/screen/settings/SettingsHomeScreen.kt @@ -68,6 +68,10 @@ fun SettingsHomeScreen( Destination.DownloadSettings ) + SettingsCategoryType.HISTORY -> navigator.navigateTo( + Destination.HistorySettings + ) + // TODO: wire remaining sub-screens as they are migrated else -> Unit } diff --git a/shared/src/commonMain/kotlin/net/newpipe/app/viewmodel/settings/HistorySettingsViewModel.kt b/shared/src/commonMain/kotlin/net/newpipe/app/viewmodel/settings/HistorySettingsViewModel.kt new file mode 100644 index 000000000..4c8112ae4 --- /dev/null +++ b/shared/src/commonMain/kotlin/net/newpipe/app/viewmodel/settings/HistorySettingsViewModel.kt @@ -0,0 +1,121 @@ +/* + * SPDX-FileCopyrightText: 2026 NewPipe e.V. + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package net.newpipe.app.viewmodel.settings + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.russhwolf.settings.Settings +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch +import net.newpipe.app.platform.HistoryActions +import net.newpipe.app.preferences.HistoryPreferences.DEFAULT_ENABLE_PLAYBACK_RESUME +import net.newpipe.app.preferences.HistoryPreferences.DEFAULT_ENABLE_PLAYBACK_STATE_LISTS +import net.newpipe.app.preferences.HistoryPreferences.DEFAULT_ENABLE_SEARCH_HISTORY +import net.newpipe.app.preferences.HistoryPreferences.DEFAULT_ENABLE_WATCH_HISTORY +import net.newpipe.app.preferences.HistoryPreferences.DEFAULT_RECAPTCHA_COOKIES +import net.newpipe.app.preferences.HistoryPreferences.KEY_ENABLE_PLAYBACK_RESUME +import net.newpipe.app.preferences.HistoryPreferences.KEY_ENABLE_PLAYBACK_STATE_LISTS +import net.newpipe.app.preferences.HistoryPreferences.KEY_ENABLE_SEARCH_HISTORY +import net.newpipe.app.preferences.HistoryPreferences.KEY_ENABLE_WATCH_HISTORY +import net.newpipe.app.preferences.HistoryPreferences.KEY_RECAPTCHA_COOKIES +import org.koin.core.annotation.KoinViewModel + +enum class HistoryMessage { + METADATA_CACHE_WIPED, + WATCH_HISTORY_DELETED, + PLAYBACK_STATES_DELETED, + SEARCH_HISTORY_DELETED, + RECAPTCHA_COOKIES_CLEARED +} + +@KoinViewModel +class HistorySettingsViewModel( + private val settings: Settings, + private val historyActions: HistoryActions +) : ViewModel() { + + val watchHistoryEnabled: StateFlow + field = booleanFlow(KEY_ENABLE_WATCH_HISTORY, DEFAULT_ENABLE_WATCH_HISTORY) + + val playbackResumeEnabled: StateFlow + field = booleanFlow(KEY_ENABLE_PLAYBACK_RESUME, DEFAULT_ENABLE_PLAYBACK_RESUME) + + val playbackStateListsEnabled: StateFlow + field = booleanFlow( + KEY_ENABLE_PLAYBACK_STATE_LISTS, + DEFAULT_ENABLE_PLAYBACK_STATE_LISTS + ) + + val searchHistoryEnabled: StateFlow + field = booleanFlow(KEY_ENABLE_SEARCH_HISTORY, DEFAULT_ENABLE_SEARCH_HISTORY) + + val recaptchaCookiesPresent: StateFlow + field = MutableStateFlow(storedRecaptchaCookies().isNotEmpty()) + + val message: StateFlow + field = MutableStateFlow(null) + + fun setWatchHistoryEnabled(value: Boolean) = watchHistoryEnabled + .persist(KEY_ENABLE_WATCH_HISTORY, value) + + fun setPlaybackResumeEnabled(value: Boolean) = playbackResumeEnabled + .persist(KEY_ENABLE_PLAYBACK_RESUME, value) + + fun setPlaybackStateListsEnabled(value: Boolean) = playbackStateListsEnabled + .persist(KEY_ENABLE_PLAYBACK_STATE_LISTS, value) + + fun setSearchHistoryEnabled(value: Boolean) = searchHistoryEnabled + .persist(KEY_ENABLE_SEARCH_HISTORY, value) + + fun wipeMetadataCache() { + historyActions.wipeMetadataCache() + message.value = HistoryMessage.METADATA_CACHE_WIPED + } + + fun clearWatchHistory() = clearThenReport( + success = HistoryMessage.WATCH_HISTORY_DELETED, + deletion = historyActions::clearWatchHistory + ) + + fun clearPlaybackStates() = clearThenReport( + success = HistoryMessage.PLAYBACK_STATES_DELETED, + deletion = historyActions::clearPlaybackStates + ) + + fun clearSearchHistory() = clearThenReport( + success = HistoryMessage.SEARCH_HISTORY_DELETED, + deletion = historyActions::clearSearchHistory + ) + + fun clearRecaptchaCookies() { + historyActions.clearRecaptchaCookies() + settings.putString(KEY_RECAPTCHA_COOKIES, DEFAULT_RECAPTCHA_COOKIES) + recaptchaCookiesPresent.value = false + message.value = HistoryMessage.RECAPTCHA_COOKIES_CLEARED + } + + fun onMessageShown() { + message.value = null + } + + private fun clearThenReport(success: HistoryMessage, deletion: suspend () -> Boolean) { + viewModelScope.launch { + if (deletion()) { + message.value = success + } + } + } + + private fun storedRecaptchaCookies() = settings.getString(KEY_RECAPTCHA_COOKIES, DEFAULT_RECAPTCHA_COOKIES) + + private fun booleanFlow(key: String, defaultValue: Boolean) = MutableStateFlow(settings.getBoolean(key, defaultValue)) + + private fun MutableStateFlow.persist(key: String, newValue: Boolean) { + settings.putBoolean(key, newValue) + value = newValue + } +}