add history settings screen

This commit is contained in:
Ida Delphine authored and Aayush Gupta committed 2026-10-27 18:26:48 +08:00
1 parent 12e7ddc1bc
commit b7d0d143e2
7 files changed
+546

No files matched your search

@@ -0,0 +1,61 @@
/*
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
* 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"
}
}
@@ -217,4 +217,37 @@
<string name="no_appropriate_file_manager_message">No appropriate file manager was found for this action.\nPlease install a file manager or try to disable '%1$s' in the download settings</string>
<string name="no_appropriate_file_manager_message_android_10">No appropriate file manager was found for this action.\nPlease install a Storage Access Framework compatible file manager</string>
<!-- History and cache Settings -->
<string name="enable_watch_history_title">Watch history</string>
<string name="enable_watch_history_summary">Keep track of watched videos</string>
<string name="enable_playback_resume_title">Resume playback</string>
<string name="enable_playback_resume_summary">Restore last playback position</string>
<string name="enable_playback_state_lists_title">Positions in lists</string>
<string name="enable_playback_state_lists_summary">Show playback position indicators in lists</string>
<string name="enable_search_history_title">Search history</string>
<string name="enable_search_history_summary">Store search queries locally</string>
<!-- History and cache Settings: clear data -->
<string name="settings_category_clear_data_title">Clear data</string>
<string name="delete">Delete</string>
<string name="metadata_cache_wipe_title">Wipe cached metadata</string>
<string name="metadata_cache_wipe_summary">Remove all cached webpage data</string>
<string name="metadata_cache_wipe_complete_notice">Metadata cache wiped</string>
<string name="clear_views_history_title">Clear watch history</string>
<string name="clear_views_history_summary">Deletes the history of played streams and the playback positions</string>
<string name="delete_view_history_alert">Delete entire watch history?</string>
<string name="watch_history_deleted">Watch history deleted</string>
<string name="clear_playback_states_title">Delete playback positions</string>
<string name="clear_playback_states_summary">Deletes all playback positions</string>
<string name="delete_playback_states_alert">Delete all playback positions?</string>
<string name="watch_history_states_deleted">Playback positions deleted</string>
<string name="clear_search_history_title">Clear search history</string>
<string name="clear_search_history_summary">Deletes history of search keywords</string>
<string name="delete_search_history_alert">Delete entire search history?</string>
<string name="search_history_deleted">Search history deleted</string>
<string name="clear_cookie_title">Clear reCAPTCHA cookies</string>
<string name="clear_cookie_summary">Clear cookies that NewPipe stores when you solve a reCAPTCHA</string>
<string name="recaptcha_cookies_cleared">reCAPTCHA cookies have been cleared</string>
</resources>
@@ -14,6 +14,9 @@ import kotlinx.serialization.Serializable
@Serializable
sealed interface Destination : NavKey {
@Serializable
data object HistorySettings : Destination
@Serializable
data object DownloadSettings : Destination
@@ -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<Destination.DownloadSettings> {
DownloadSettingsScreen()
}
navigation<Destination.HistorySettings> {
HistorySettingsScreen()
}
}
/**
@@ -0,0 +1,319 @@
/*
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
* 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<HistoryClearTarget?>(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()
}
@@ -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
}
@@ -0,0 +1,121 @@
/*
* SPDX-FileCopyrightText: 2026 NewPipe e.V. <https://newpipe-ev.de>
* 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<Boolean>
field = booleanFlow(KEY_ENABLE_WATCH_HISTORY, DEFAULT_ENABLE_WATCH_HISTORY)
val playbackResumeEnabled: StateFlow<Boolean>
field = booleanFlow(KEY_ENABLE_PLAYBACK_RESUME, DEFAULT_ENABLE_PLAYBACK_RESUME)
val playbackStateListsEnabled: StateFlow<Boolean>
field = booleanFlow(
KEY_ENABLE_PLAYBACK_STATE_LISTS,
DEFAULT_ENABLE_PLAYBACK_STATE_LISTS
)
val searchHistoryEnabled: StateFlow<Boolean>
field = booleanFlow(KEY_ENABLE_SEARCH_HISTORY, DEFAULT_ENABLE_SEARCH_HISTORY)
val recaptchaCookiesPresent: StateFlow<Boolean>
field = MutableStateFlow(storedRecaptchaCookies().isNotEmpty())
val message: StateFlow<HistoryMessage?>
field = MutableStateFlow<HistoryMessage?>(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<Boolean>.persist(key: String, newValue: Boolean) {
settings.putBoolean(key, newValue)
value = newValue
}
}