|
@@ -15,6 +15,7 @@ import androidx.compose.ui.focus.FocusRequester
|
|
|
import androidx.compose.ui.focus.focusRequester
|
|
|
import androidx.compose.ui.res.stringResource
|
|
|
import eu.kanade.domain.category.model.Category
|
|
|
+import eu.kanade.domain.category.model.anyWithName
|
|
|
import eu.kanade.tachiyomi.R
|
|
|
import kotlinx.coroutines.delay
|
|
|
import kotlin.time.Duration.Companion.seconds
|
|
@@ -23,17 +24,23 @@ import kotlin.time.Duration.Companion.seconds
|
|
|
fun CategoryCreateDialog(
|
|
|
onDismissRequest: () -> Unit,
|
|
|
onCreate: (String) -> Unit,
|
|
|
+ categories: List<Category>,
|
|
|
) {
|
|
|
var name by remember { mutableStateOf("") }
|
|
|
+
|
|
|
val focusRequester = remember { FocusRequester() }
|
|
|
+ val nameAlreadyExists = remember(name) { categories.anyWithName(name) }
|
|
|
|
|
|
AlertDialog(
|
|
|
onDismissRequest = onDismissRequest,
|
|
|
confirmButton = {
|
|
|
- TextButton(onClick = {
|
|
|
- onCreate(name)
|
|
|
- onDismissRequest()
|
|
|
- },) {
|
|
|
+ TextButton(
|
|
|
+ enabled = name.isNotEmpty() && !nameAlreadyExists,
|
|
|
+ onClick = {
|
|
|
+ onCreate(name)
|
|
|
+ onDismissRequest()
|
|
|
+ },
|
|
|
+ ) {
|
|
|
Text(text = stringResource(R.string.action_add))
|
|
|
}
|
|
|
},
|
|
@@ -47,13 +54,15 @@ fun CategoryCreateDialog(
|
|
|
},
|
|
|
text = {
|
|
|
OutlinedTextField(
|
|
|
- modifier = Modifier
|
|
|
- .focusRequester(focusRequester),
|
|
|
+ modifier = Modifier.focusRequester(focusRequester),
|
|
|
value = name,
|
|
|
onValueChange = { name = it },
|
|
|
- label = {
|
|
|
- Text(text = stringResource(R.string.name))
|
|
|
+ label = { Text(text = stringResource(R.string.name)) },
|
|
|
+ supportingText = {
|
|
|
+ val msgRes = if (name.isNotEmpty() && nameAlreadyExists) R.string.error_category_exists else R.string.information_required_plain
|
|
|
+ Text(text = stringResource(msgRes))
|
|
|
},
|
|
|
+ isError = name.isNotEmpty() && nameAlreadyExists,
|
|
|
singleLine = true,
|
|
|
)
|
|
|
},
|
|
@@ -70,18 +79,25 @@ fun CategoryCreateDialog(
|
|
|
fun CategoryRenameDialog(
|
|
|
onDismissRequest: () -> Unit,
|
|
|
onRename: (String) -> Unit,
|
|
|
+ categories: List<Category>,
|
|
|
category: Category,
|
|
|
) {
|
|
|
var name by remember { mutableStateOf(category.name) }
|
|
|
+ var valueHasChanged by remember { mutableStateOf(false) }
|
|
|
+
|
|
|
val focusRequester = remember { FocusRequester() }
|
|
|
+ val nameAlreadyExists = remember(name) { categories.anyWithName(name) }
|
|
|
|
|
|
AlertDialog(
|
|
|
onDismissRequest = onDismissRequest,
|
|
|
confirmButton = {
|
|
|
- TextButton(onClick = {
|
|
|
- onRename(name)
|
|
|
- onDismissRequest()
|
|
|
- },) {
|
|
|
+ TextButton(
|
|
|
+ enabled = valueHasChanged && !nameAlreadyExists,
|
|
|
+ onClick = {
|
|
|
+ onRename(name)
|
|
|
+ onDismissRequest()
|
|
|
+ },
|
|
|
+ ) {
|
|
|
Text(text = stringResource(android.R.string.ok))
|
|
|
}
|
|
|
},
|
|
@@ -95,13 +111,18 @@ fun CategoryRenameDialog(
|
|
|
},
|
|
|
text = {
|
|
|
OutlinedTextField(
|
|
|
- modifier = Modifier
|
|
|
- .focusRequester(focusRequester),
|
|
|
+ modifier = Modifier.focusRequester(focusRequester),
|
|
|
value = name,
|
|
|
- onValueChange = { name = it },
|
|
|
- label = {
|
|
|
- Text(text = stringResource(R.string.name))
|
|
|
+ onValueChange = {
|
|
|
+ valueHasChanged = name != it
|
|
|
+ name = it
|
|
|
+ },
|
|
|
+ label = { Text(text = stringResource(R.string.name)) },
|
|
|
+ supportingText = {
|
|
|
+ val msgRes = if (valueHasChanged && nameAlreadyExists) R.string.error_category_exists else R.string.information_required_plain
|
|
|
+ Text(text = stringResource(msgRes))
|
|
|
},
|
|
|
+ isError = valueHasChanged && nameAlreadyExists,
|
|
|
singleLine = true,
|
|
|
)
|
|
|
},
|