Fix source tab long click

This commit is contained in:
Jobobby04 2022-12-04 16:34:24 -05:00
parent de4567a508
commit 290e962a57
3 changed files with 74 additions and 63 deletions

View File

@ -7,6 +7,8 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PushPin import androidx.compose.material.icons.filled.PushPin
import androidx.compose.material.icons.outlined.PushPin import androidx.compose.material.icons.outlined.PushPin
@ -189,8 +191,8 @@ fun SourceOptionsDialog(
onClickPin: () -> Unit, onClickPin: () -> Unit,
onClickDisable: () -> Unit, onClickDisable: () -> Unit,
// SY --> // SY -->
onClickSetCategories: () -> Unit, onClickSetCategories: (() -> Unit)?,
onClickToggleDataSaver: () -> Unit, onClickToggleDataSaver: (() -> Unit)?,
// SY <-- // SY <--
onDismiss: () -> Unit, onDismiss: () -> Unit,
) { ) {
@ -218,6 +220,7 @@ fun SourceOptionsDialog(
) )
} }
// SY --> // SY -->
if (onClickSetCategories != null) {
Text( Text(
text = stringResource(id = R.string.categories), text = stringResource(id = R.string.categories),
modifier = Modifier modifier = Modifier
@ -225,6 +228,8 @@ fun SourceOptionsDialog(
.fillMaxWidth() .fillMaxWidth()
.padding(vertical = 16.dp), .padding(vertical = 16.dp),
) )
}
if (onClickToggleDataSaver != null) {
Text( Text(
text = if (source.isExcludedFromDataSaver) { text = if (source.isExcludedFromDataSaver) {
stringResource(id = R.string.data_saver_stop_exclude) stringResource(id = R.string.data_saver_stop_exclude)
@ -236,6 +241,7 @@ fun SourceOptionsDialog(
.fillMaxWidth() .fillMaxWidth()
.padding(vertical = 16.dp), .padding(vertical = 16.dp),
) )
}
// SY <-- // SY <--
} }
}, },
@ -252,12 +258,11 @@ sealed class SourceUiModel {
// SY --> // SY -->
@Composable @Composable
fun SourceCategoriesDialog( fun SourceCategoriesDialog(
source: Source?, source: Source,
categories: List<String>, categories: List<String>,
onClickCategories: (List<String>) -> Unit, onClickCategories: (List<String>) -> Unit,
onDismiss: () -> Unit, onDismissRequest: () -> Unit,
) { ) {
source ?: return
val newCategories = remember(source) { val newCategories = remember(source) {
mutableStateListOf<String>().also { it += source.categories } mutableStateListOf<String>().also { it += source.categories }
} }
@ -266,7 +271,7 @@ fun SourceCategoriesDialog(
Text(text = source.visualName) Text(text = source.visualName)
}, },
text = { text = {
Column { Column(Modifier.verticalScroll(rememberScrollState())) {
categories.forEach { categories.forEach {
Row( Row(
modifier = Modifier modifier = Modifier
@ -293,7 +298,7 @@ fun SourceCategoriesDialog(
} }
} }
}, },
onDismissRequest = onDismiss, onDismissRequest = onDismissRequest,
confirmButton = { confirmButton = {
TextButton(onClick = { onClickCategories(newCategories.toList()) }) { TextButton(onClick = { onClickCategories(newCategories.toList()) }) {
Text(text = stringResource(android.R.string.ok)) Text(text = stringResource(android.R.string.ok))

View File

@ -24,6 +24,7 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import logcat.LogPriority import logcat.LogPriority
@ -67,6 +68,12 @@ class SourcesScreenModel(
} }
.flowOn(Dispatchers.IO) .flowOn(Dispatchers.IO)
.launchIn(coroutineScope) .launchIn(coroutineScope)
sourcePreferences.dataSaver().changes()
.onEach { enabled ->
mutableState.update { it.copy(dataSaverEnabled = enabled) }
}
.launchIn(coroutineScope)
// SY <-- // SY <--
} }
@ -182,6 +189,7 @@ data class SourcesState(
val categories: List<String> = emptyList(), val categories: List<String> = emptyList(),
val showPin: Boolean = true, val showPin: Boolean = true,
val showLatest: Boolean = false, val showLatest: Boolean = false,
val dataSaverEnabled: Boolean = false,
// SY <-- // SY <--
) { ) {
val isEmpty = items.isEmpty() val isEmpty = items.isEmpty()

View File

@ -77,9 +77,8 @@ fun Screen.sourcesTab(
onLongClickItem = screenModel::showSourceDialog, onLongClickItem = screenModel::showSourceDialog,
) )
state.dialog?.let { dialog -> when (val dialog = state.dialog) {
when (dialog) { is SourcesScreenModel.Dialog.SourceLongClick -> {
is SourcesScreenModel.Dialog.SourceCategories -> {
val source = dialog.source val source = dialog.source
SourceOptionsDialog( SourceOptionsDialog(
source = source, source = source,
@ -94,16 +93,15 @@ fun Screen.sourcesTab(
// SY --> // SY -->
onClickSetCategories = { onClickSetCategories = {
screenModel.showSourceCategoriesDialog(source) screenModel.showSourceCategoriesDialog(source)
screenModel.closeDialog() }.takeIf { state.categories.isNotEmpty() },
},
onClickToggleDataSaver = { onClickToggleDataSaver = {
screenModel.toggleExcludeFromDataSaver(source) screenModel.toggleExcludeFromDataSaver(source)
screenModel.closeDialog() screenModel.closeDialog()
}, }.takeIf { state.dataSaverEnabled },
onDismiss = screenModel::closeDialog, onDismiss = screenModel::closeDialog,
) )
} }
is SourcesScreenModel.Dialog.SourceLongClick -> { is SourcesScreenModel.Dialog.SourceCategories -> {
val source = dialog.source val source = dialog.source
SourceCategoriesDialog( SourceCategoriesDialog(
source = source, source = source,
@ -112,10 +110,10 @@ fun Screen.sourcesTab(
screenModel.setSourceCategories(source, categories) screenModel.setSourceCategories(source, categories)
screenModel.closeDialog() screenModel.closeDialog()
}, },
onDismiss = screenModel::closeDialog, onDismissRequest = screenModel::closeDialog,
) )
} }
} null -> Unit
} }
val internalErrString = stringResource(R.string.internal_error) val internalErrString = stringResource(R.string.internal_error)