Update page range filter limit

This commit is contained in:
FooIbar committed 2024-07-31 18:54:51 +08:00
1 parent abcef9a86e
commit a2547f1e8d
4 files changed
+31 -20

No files matched your search

@@ -5,6 +5,7 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
@@ -152,23 +153,27 @@ fun SearchFilter(
val (from, to) = dialogState.awaitResult(
initial = advancedOption.fromPage to advancedOption.toPage,
title = R.string.key_pages,
invalidator = { (min, _max) ->
// 0 means max pages not filled, though +inf
val max = if (_max == 0) Int.MAX_VALUE else _max
if (min != 0) {
ensure(max - min >= 20) { pageErr2 }
} else {
ensure(max >= 10) { pageErr1 }
invalidator = { (min, max) ->
if (max != 0) {
if (min != 0) {
ensure(min <= (max / 2).coerceAtMost(max - 20)) { pageErr2 }
} else {
ensure(max >= 10) { pageErr1 }
}
}
},
) { error ->
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Row(verticalAlignment = Alignment.CenterVertically) {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceEvenly,
) {
OutlinedTextField(
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
value = expectedValue.first.takeIf { it > 0 }?.toString().orEmpty(),
onValueChange = {
expectedValue = expectedValue.copy(first = it.toIntOrDefault(0).coerceAtLeast(0))
expectedValue = expectedValue.copy(first = it.toIntOrDefault(0).coerceIn(0, 1000))
},
modifier = Modifier.width(112.dp).padding(16.dp),
singleLine = true,
@@ -179,7 +184,7 @@ fun SearchFilter(
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
value = expectedValue.second.takeIf { it > 0 }?.toString().orEmpty(),
onValueChange = {
expectedValue = expectedValue.copy(second = it.toIntOrDefault(0).coerceAtLeast(0))
expectedValue = expectedValue.copy(second = it.toIntOrDefault(0).coerceIn(0, 2000))
},
modifier = Modifier.width(112.dp).padding(16.dp),
singleLine = true,
@@ -189,7 +194,9 @@ fun SearchFilter(
}
if (error != null) {
ListItem(
headlineContent = { Text(text = error) },
headlineContent = {
Text(text = error, style = MaterialTheme.typography.bodySmall)
},
leadingContent = {
Icon(imageVector = Icons.Default.Info, contentDescription = null)
},
@@ -52,12 +52,14 @@ import androidx.compose.material3.TimePicker
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.material3.rememberTimePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.compositionLocalOf
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.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@@ -79,6 +81,7 @@ import com.jamal.composeprefs3.ui.ifNotNullThen
import com.jamal.composeprefs3.ui.ifTrueThen
import kotlin.coroutines.resume
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
@@ -116,7 +119,6 @@ class DialogState {
invalidator: (suspend Raise<String>.(R) -> Unit)? = null,
block: @Composable DialogScope<R>.(String?) -> Unit,
): R = dialog { cont ->
val coroutineScope = rememberCoroutineScope()
val state = remember(cont) { mutableStateOf(initial) }
var errorMsg by remember(cont) { mutableStateOf<String?>(null) }
val impl = remember(cont) {
@@ -124,17 +126,19 @@ class DialogState {
override var expectedValue by state
}
}
if (invalidator != null) {
LaunchedEffect(state) {
snapshotFlow { state.value }.collectLatest {
errorMsg = either { invalidator(it) }.leftOrNull()
}
}
}
AlertDialog(
onDismissRequest = { cont.cancel() },
confirmButton = {
TextButton(onClick = {
if (invalidator == null) {
if (invalidator == null || errorMsg == null) {
cont.resume(state.value)
} else {
coroutineScope.launch {
errorMsg = either { invalidator(state.value) }.leftOrNull()
errorMsg ?: cont.resume(state.value)
}
}
}) {
Text(text = stringResource(id = android.R.string.ok))
+1 -1
View File
@@ -72,7 +72,7 @@
<string name="search_sr">最低评分</string>
<string name="search_sp_to">到</string>
<string name="search_sp_err1">页数最大值至少为 10</string>
<string name="search_sp_err2">页数范围差至少为 20</string>
<string name="search_sp_err2">页数范围过窄</string>
<string name="search_sf">禁用排除项:</string>
<string name="search_sfl">语言</string>
<string name="search_sfu">上传者</string>
+1 -1
View File
@@ -110,7 +110,7 @@
<string name="search_sr">Minimum Rating</string>
<string name="search_sp_to">to</string>
<string name="search_sp_err1">The page range maximum cannot be below 10</string>
<string name="search_sp_err2">The page range must be at least 20</string>
<string name="search_sp_err2">The page range is too narrow</string>
<!-- A placeholder for other languages -->
<string name="search_sp_suffix" />
<string name="search_sf">Disable default filters for:</string>