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,24 +220,28 @@ fun SourceOptionsDialog(
) )
} }
// SY --> // SY -->
Text( if (onClickSetCategories != null) {
text = stringResource(id = R.string.categories), Text(
modifier = Modifier text = stringResource(id = R.string.categories),
.clickable(onClick = onClickSetCategories) modifier = Modifier
.fillMaxWidth() .clickable(onClick = onClickSetCategories)
.padding(vertical = 16.dp), .fillMaxWidth()
) .padding(vertical = 16.dp),
Text( )
text = if (source.isExcludedFromDataSaver) { }
stringResource(id = R.string.data_saver_stop_exclude) if (onClickToggleDataSaver != null) {
} else { Text(
stringResource(id = R.string.data_saver_exclude) text = if (source.isExcludedFromDataSaver) {
}, stringResource(id = R.string.data_saver_stop_exclude)
modifier = Modifier } else {
.clickable(onClick = onClickToggleDataSaver) stringResource(id = R.string.data_saver_exclude)
.fillMaxWidth() },
.padding(vertical = 16.dp), modifier = Modifier
) .clickable(onClick = onClickToggleDataSaver)
.fillMaxWidth()
.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,45 +77,43 @@ 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, onClickPin = {
onClickPin = { screenModel.togglePin(source)
screenModel.togglePin(source) screenModel.closeDialog()
screenModel.closeDialog() },
}, onClickDisable = {
onClickDisable = { screenModel.toggleSource(source)
screenModel.toggleSource(source) screenModel.closeDialog()
screenModel.closeDialog() },
}, // SY -->
// SY --> onClickSetCategories = {
onClickSetCategories = { screenModel.showSourceCategoriesDialog(source)
screenModel.showSourceCategoriesDialog(source) }.takeIf { state.categories.isNotEmpty() },
screenModel.closeDialog() onClickToggleDataSaver = {
}, screenModel.toggleExcludeFromDataSaver(source)
onClickToggleDataSaver = { screenModel.closeDialog()
screenModel.toggleExcludeFromDataSaver(source) }.takeIf { state.dataSaverEnabled },
screenModel.closeDialog() onDismiss = screenModel::closeDialog,
}, )
onDismiss = screenModel::closeDialog,
)
}
is SourcesScreenModel.Dialog.SourceLongClick -> {
val source = dialog.source
SourceCategoriesDialog(
source = source,
categories = state.categories,
onClickCategories = { categories ->
screenModel.setSourceCategories(source, categories)
screenModel.closeDialog()
},
onDismiss = screenModel::closeDialog,
)
}
} }
is SourcesScreenModel.Dialog.SourceCategories -> {
val source = dialog.source
SourceCategoriesDialog(
source = source,
categories = state.categories,
onClickCategories = { categories ->
screenModel.setSourceCategories(source, categories)
screenModel.closeDialog()
},
onDismissRequest = screenModel::closeDialog,
)
}
null -> Unit
} }
val internalErrString = stringResource(R.string.internal_error) val internalErrString = stringResource(R.string.internal_error)