DeleteLibraryMangaDialog.kt 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package eu.kanade.presentation.library
  2. import androidx.compose.foundation.clickable
  3. import androidx.compose.foundation.layout.Column
  4. import androidx.compose.foundation.layout.Row
  5. import androidx.compose.foundation.layout.fillMaxWidth
  6. import androidx.compose.material3.AlertDialog
  7. import androidx.compose.material3.Checkbox
  8. import androidx.compose.material3.Text
  9. import androidx.compose.material3.TextButton
  10. import androidx.compose.runtime.Composable
  11. import androidx.compose.runtime.getValue
  12. import androidx.compose.runtime.mutableStateOf
  13. import androidx.compose.runtime.remember
  14. import androidx.compose.runtime.setValue
  15. import androidx.compose.ui.Alignment
  16. import androidx.compose.ui.Modifier
  17. import androidx.compose.ui.res.stringResource
  18. import eu.kanade.tachiyomi.R
  19. import tachiyomi.core.preference.CheckboxState
  20. @Composable
  21. fun DeleteLibraryMangaDialog(
  22. containsLocalManga: Boolean,
  23. onDismissRequest: () -> Unit,
  24. onConfirm: (Boolean, Boolean) -> Unit,
  25. ) {
  26. var list by remember {
  27. mutableStateOf(
  28. buildList<CheckboxState.State<Int>> {
  29. add(CheckboxState.State.None(R.string.manga_from_library))
  30. if (!containsLocalManga) {
  31. add(CheckboxState.State.None(R.string.downloaded_chapters))
  32. }
  33. },
  34. )
  35. }
  36. AlertDialog(
  37. onDismissRequest = onDismissRequest,
  38. dismissButton = {
  39. TextButton(onClick = onDismissRequest) {
  40. Text(text = stringResource(R.string.action_cancel))
  41. }
  42. },
  43. confirmButton = {
  44. TextButton(
  45. enabled = list.any { it.isChecked },
  46. onClick = {
  47. onDismissRequest()
  48. onConfirm(
  49. list[0].isChecked,
  50. list.getOrElse(1) { CheckboxState.State.None(0) }.isChecked,
  51. )
  52. },
  53. ) {
  54. Text(text = stringResource(android.R.string.ok))
  55. }
  56. },
  57. title = {
  58. Text(text = stringResource(R.string.action_remove))
  59. },
  60. text = {
  61. Column {
  62. list.forEach { state ->
  63. val onCheck = {
  64. val index = list.indexOf(state)
  65. val mutableList = list.toMutableList()
  66. mutableList[index] = state.next() as CheckboxState.State<Int>
  67. list = mutableList.toList()
  68. }
  69. Row(
  70. modifier = Modifier
  71. .fillMaxWidth()
  72. .clickable { onCheck() },
  73. verticalAlignment = Alignment.CenterVertically,
  74. ) {
  75. Checkbox(
  76. checked = state.isChecked,
  77. onCheckedChange = { onCheck() },
  78. )
  79. Text(text = stringResource(state.value))
  80. }
  81. }
  82. }
  83. },
  84. )
  85. }