|
| 1 | +package org.schabi.newpipe.settings.presentation.history_cache |
| 2 | + |
| 3 | +import androidx.lifecycle.ViewModel |
| 4 | +import androidx.lifecycle.viewModelScope |
| 5 | +import dagger.hilt.android.lifecycle.HiltViewModel |
| 6 | +import kotlinx.coroutines.Dispatchers |
| 7 | +import kotlinx.coroutines.flow.MutableSharedFlow |
| 8 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 9 | +import kotlinx.coroutines.flow.StateFlow |
| 10 | +import kotlinx.coroutines.flow.asSharedFlow |
| 11 | +import kotlinx.coroutines.flow.asStateFlow |
| 12 | +import kotlinx.coroutines.flow.update |
| 13 | +import kotlinx.coroutines.launch |
| 14 | +import org.schabi.newpipe.DownloaderImpl |
| 15 | +import org.schabi.newpipe.R |
| 16 | +import org.schabi.newpipe.error.ErrorInfo |
| 17 | +import org.schabi.newpipe.error.ReCaptchaActivity |
| 18 | +import org.schabi.newpipe.error.UserAction |
| 19 | +import org.schabi.newpipe.error.usecases.OpenErrorActivity |
| 20 | +import org.schabi.newpipe.settings.domain.usecases.DeleteCompleteSearchHistory |
| 21 | +import org.schabi.newpipe.settings.domain.usecases.DeleteCompleteStreamStateHistory |
| 22 | +import org.schabi.newpipe.settings.domain.usecases.DeleteWatchHistory |
| 23 | +import org.schabi.newpipe.settings.domain.usecases.get_preference.GetPreference |
| 24 | +import org.schabi.newpipe.settings.domain.usecases.update_boolean_preference.UpdatePreference |
| 25 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheEvent |
| 26 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheEvent.OnClickClearSearchHistory |
| 27 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheEvent.OnClickClearWatchHistory |
| 28 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheEvent.OnClickDeletePlaybackPositions |
| 29 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheEvent.OnClickReCaptchaCookies |
| 30 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheEvent.OnClickWipeCachedMetadata |
| 31 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheEvent.OnUpdateBooleanPreference |
| 32 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheUiEvent |
| 33 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheUiEvent.ShowClearWatchHistorySnackbar |
| 34 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheUiEvent.ShowDeletePlaybackSnackbar |
| 35 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheUiEvent.ShowDeleteSearchHistorySnackbar |
| 36 | +import org.schabi.newpipe.settings.presentation.history_cache.events.HistoryCacheUiEvent.ShowWipeCachedMetadataSnackbar |
| 37 | +import org.schabi.newpipe.util.InfoCache |
| 38 | +import javax.inject.Inject |
| 39 | + |
| 40 | +@HiltViewModel |
| 41 | +class HistoryCacheSettingsViewModel @Inject constructor( |
| 42 | + private val updateBooleanPreference: UpdatePreference<Boolean>, |
| 43 | + private val updateStringPreference: UpdatePreference<String>, |
| 44 | + private val getBooleanPreference: GetPreference<Boolean>, |
| 45 | + private val deleteWatchHistory: DeleteWatchHistory, |
| 46 | + private val deleteCompleteStreamStateHistory: DeleteCompleteStreamStateHistory, |
| 47 | + private val deleteCompleteSearchHistory: DeleteCompleteSearchHistory, |
| 48 | + private val openErrorActivity: OpenErrorActivity, |
| 49 | +) : ViewModel() { |
| 50 | + private val _state = MutableStateFlow(SwitchPreferencesUiState()) |
| 51 | + val state: StateFlow<SwitchPreferencesUiState> = _state.asStateFlow() |
| 52 | + |
| 53 | + private val _eventFlow = MutableSharedFlow<HistoryCacheUiEvent>() |
| 54 | + val eventFlow = _eventFlow.asSharedFlow() |
| 55 | + |
| 56 | + init { |
| 57 | + viewModelScope.launch { |
| 58 | + getBooleanPreference(R.string.enable_watch_history_key, true).collect { preference -> |
| 59 | + _state.update { oldState -> |
| 60 | + oldState.copy( |
| 61 | + watchHistoryEnabled = preference |
| 62 | + ) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + viewModelScope.launch { |
| 68 | + getBooleanPreference(R.string.enable_playback_resume_key, true).collect { preference -> |
| 69 | + _state.update { oldState -> |
| 70 | + oldState.copy( |
| 71 | + resumePlaybackEnabled = preference |
| 72 | + ) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + viewModelScope.launch { |
| 78 | + getBooleanPreference( |
| 79 | + R.string.enable_playback_state_lists_key, |
| 80 | + true |
| 81 | + ).collect { preference -> |
| 82 | + _state.update { oldState -> |
| 83 | + oldState.copy( |
| 84 | + positionsInListsEnabled = preference |
| 85 | + ) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + viewModelScope.launch { |
| 90 | + getBooleanPreference(R.string.enable_search_history_key, true).collect { preference -> |
| 91 | + _state.update { oldState -> |
| 92 | + oldState.copy( |
| 93 | + searchHistoryEnabled = preference |
| 94 | + ) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + fun onEvent(event: HistoryCacheEvent) { |
| 101 | + when (event) { |
| 102 | + is OnUpdateBooleanPreference -> { |
| 103 | + viewModelScope.launch { |
| 104 | + updateBooleanPreference(event.key, event.isEnabled) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + is OnClickWipeCachedMetadata -> { |
| 109 | + InfoCache.getInstance().clearCache() |
| 110 | + viewModelScope.launch { |
| 111 | + _eventFlow.emit(ShowWipeCachedMetadataSnackbar) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + is OnClickClearWatchHistory -> { |
| 116 | + viewModelScope.launch { |
| 117 | + deleteWatchHistory( |
| 118 | + onDeletePlaybackStates = { |
| 119 | + viewModelScope.launch { |
| 120 | + _eventFlow.emit(ShowDeletePlaybackSnackbar) |
| 121 | + } |
| 122 | + }, |
| 123 | + onDeleteWholeStreamHistory = { |
| 124 | + viewModelScope.launch { |
| 125 | + _eventFlow.emit(ShowClearWatchHistorySnackbar) |
| 126 | + } |
| 127 | + }, |
| 128 | + onRemoveOrphanedRecords = { |
| 129 | + // TODO: ask why original did nothing |
| 130 | + } |
| 131 | + ) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + is OnClickDeletePlaybackPositions -> { |
| 136 | + viewModelScope.launch { |
| 137 | + deleteCompleteStreamStateHistory( |
| 138 | + Dispatchers.IO, |
| 139 | + onError = { error -> |
| 140 | + openErrorActivity( |
| 141 | + ErrorInfo( |
| 142 | + error, |
| 143 | + UserAction.DELETE_FROM_HISTORY, |
| 144 | + "Delete playback states" |
| 145 | + ) |
| 146 | + ) |
| 147 | + }, |
| 148 | + onSuccess = { |
| 149 | + viewModelScope.launch { |
| 150 | + _eventFlow.emit(ShowDeletePlaybackSnackbar) |
| 151 | + } |
| 152 | + } |
| 153 | + ) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + is OnClickClearSearchHistory -> { |
| 158 | + viewModelScope.launch { |
| 159 | + deleteCompleteSearchHistory( |
| 160 | + dispatcher = Dispatchers.IO, |
| 161 | + onError = { error -> |
| 162 | + openErrorActivity( |
| 163 | + ErrorInfo( |
| 164 | + error, |
| 165 | + UserAction.DELETE_FROM_HISTORY, |
| 166 | + "Delete search history" |
| 167 | + ) |
| 168 | + ) |
| 169 | + }, |
| 170 | + onSuccess = { |
| 171 | + viewModelScope.launch { |
| 172 | + _eventFlow.emit(ShowDeleteSearchHistorySnackbar) |
| 173 | + } |
| 174 | + } |
| 175 | + ) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + is OnClickReCaptchaCookies -> { |
| 180 | + viewModelScope.launch { |
| 181 | + updateStringPreference(event.key, "") |
| 182 | + DownloaderImpl.getInstance() |
| 183 | + .setCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY, "") |
| 184 | + _eventFlow.emit(HistoryCacheUiEvent.ShowWipeCachedMetadataSnackbar) |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments