|
| 1 | +package org.schabi.newpipe.settings.presentation.history_cache |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Arrangement |
| 4 | +import androidx.compose.foundation.layout.Column |
| 5 | +import androidx.compose.foundation.layout.fillMaxSize |
| 6 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 7 | +import androidx.compose.foundation.layout.padding |
| 8 | +import androidx.compose.foundation.rememberScrollState |
| 9 | +import androidx.compose.foundation.verticalScroll |
| 10 | +import androidx.compose.material3.Scaffold |
| 11 | +import androidx.compose.material3.SnackbarHost |
| 12 | +import androidx.compose.material3.SnackbarHostState |
| 13 | +import androidx.compose.material3.Surface |
| 14 | +import androidx.compose.runtime.Composable |
| 15 | +import androidx.compose.runtime.collectAsState |
| 16 | +import androidx.compose.runtime.getValue |
| 17 | +import androidx.compose.runtime.mutableStateOf |
| 18 | +import androidx.compose.runtime.remember |
| 19 | +import androidx.compose.runtime.rememberCoroutineScope |
| 20 | +import androidx.compose.ui.Alignment |
| 21 | +import androidx.compose.ui.Modifier |
| 22 | +import androidx.compose.ui.tooling.preview.Preview |
| 23 | +import androidx.hilt.navigation.compose.hiltViewModel |
| 24 | +import kotlinx.coroutines.launch |
| 25 | +import org.schabi.newpipe.settings.presentation.history_cache.components.CachePreferencesComponent |
| 26 | +import org.schabi.newpipe.settings.presentation.history_cache.components.HistoryPreferencesComponent |
| 27 | +import org.schabi.newpipe.ui.theme.AppTheme |
| 28 | + |
| 29 | +@Composable |
| 30 | +fun HistoryCacheScreen( |
| 31 | + modifier: Modifier = Modifier, |
| 32 | + viewModel: HistoryCacheSettingsViewModel = hiltViewModel(), |
| 33 | +) { |
| 34 | + val state by viewModel.state.collectAsState() |
| 35 | + HistoryCacheComponent( |
| 36 | + state = state, |
| 37 | + onEvent = viewModel::onEvent, |
| 38 | + modifier = modifier |
| 39 | + ) |
| 40 | +} |
| 41 | + |
| 42 | +@Composable |
| 43 | +fun HistoryCacheComponent( |
| 44 | + state: HistoryCacheUiState, |
| 45 | + onEvent: (HistoryCacheEvent) -> Unit, |
| 46 | + modifier: Modifier = Modifier, |
| 47 | +) { |
| 48 | + val snackBarHostState = remember { SnackbarHostState() } |
| 49 | + Scaffold( |
| 50 | + modifier = modifier, |
| 51 | + snackbarHost = { |
| 52 | + SnackbarHost(snackBarHostState) |
| 53 | + } |
| 54 | + ) { padding -> |
| 55 | + val scrollState = rememberScrollState() |
| 56 | + Column( |
| 57 | + modifier = Modifier |
| 58 | + .padding(padding) |
| 59 | + .verticalScroll(scrollState), |
| 60 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 61 | + verticalArrangement = Arrangement.Center, |
| 62 | + ) { |
| 63 | + HistoryPreferencesComponent( |
| 64 | + state = state.switchPreferencesUiState, |
| 65 | + onEvent = onEvent, |
| 66 | + modifier = Modifier.fillMaxWidth() |
| 67 | + ) |
| 68 | + val coroutineScope = rememberCoroutineScope() |
| 69 | + CachePreferencesComponent( |
| 70 | + onEvent = onEvent, |
| 71 | + onShowSnackbar = { |
| 72 | + coroutineScope.launch { |
| 73 | + snackBarHostState.showSnackbar(it) |
| 74 | + } |
| 75 | + }, |
| 76 | + modifier = Modifier.fillMaxWidth() |
| 77 | + ) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +@Preview(showBackground = true) |
| 83 | +@Composable |
| 84 | +private fun HistoryCacheComponentPreview() { |
| 85 | + val state by remember { |
| 86 | + mutableStateOf( |
| 87 | + HistoryCacheUiState() |
| 88 | + ) |
| 89 | + } |
| 90 | + AppTheme( |
| 91 | + useDarkTheme = false |
| 92 | + ) { |
| 93 | + Surface { |
| 94 | + HistoryCacheComponent( |
| 95 | + state = state, |
| 96 | + onEvent = { |
| 97 | + }, |
| 98 | + modifier = Modifier.fillMaxSize() |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments