AsssistantScreen.kt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Alper Ozturk
  5. * Copyright (C) 2024 Alper Ozturk
  6. * Copyright (C) 2024 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.nextcloud.client.assistant
  22. import androidx.compose.foundation.ExperimentalFoundationApi
  23. import androidx.compose.foundation.horizontalScroll
  24. import androidx.compose.foundation.layout.Row
  25. import androidx.compose.foundation.layout.Spacer
  26. import androidx.compose.foundation.layout.fillMaxSize
  27. import androidx.compose.foundation.layout.fillMaxWidth
  28. import androidx.compose.foundation.layout.padding
  29. import androidx.compose.foundation.lazy.LazyColumn
  30. import androidx.compose.foundation.lazy.items
  31. import androidx.compose.foundation.rememberScrollState
  32. import androidx.compose.foundation.text.KeyboardOptions
  33. import androidx.compose.material3.ButtonColors
  34. import androidx.compose.material3.ButtonDefaults
  35. import androidx.compose.material3.FilledTonalButton
  36. import androidx.compose.material3.Text
  37. import androidx.compose.material3.TextField
  38. import androidx.compose.material3.TextFieldDefaults
  39. import androidx.compose.runtime.Composable
  40. import androidx.compose.runtime.collectAsState
  41. import androidx.compose.runtime.getValue
  42. import androidx.compose.runtime.mutableStateOf
  43. import androidx.compose.runtime.remember
  44. import androidx.compose.runtime.setValue
  45. import androidx.compose.ui.Modifier
  46. import androidx.compose.ui.graphics.Color
  47. import androidx.compose.ui.res.stringResource
  48. import androidx.compose.ui.text.input.KeyboardType
  49. import androidx.compose.ui.unit.dp
  50. import com.google.android.material.floatingactionbutton.FloatingActionButton
  51. import com.nextcloud.android.common.ui.theme.utils.ColorRole
  52. import com.nextcloud.operations.assistant.model.OcsType
  53. import com.nextcloud.ui.composeComponents.SimpleAlertDialog
  54. import com.owncloud.android.R
  55. @OptIn(ExperimentalFoundationApi::class)
  56. @Composable
  57. fun AssistantScreen(viewModel: AssistantViewModel, floatingActionButton: FloatingActionButton) {
  58. // TODO hide sort group, floating action and search bar
  59. val taskTypes by viewModel.taskTypes.collectAsState()
  60. var selectedTaskType: String? by remember {
  61. mutableStateOf(null)
  62. }
  63. var showAddTaskAlertDialog by remember {
  64. mutableStateOf(false)
  65. }
  66. floatingActionButton.setOnClickListener {
  67. showAddTaskAlertDialog = true
  68. }
  69. LazyColumn(
  70. modifier = Modifier
  71. .fillMaxSize()
  72. .padding(16.dp)
  73. ) {
  74. stickyHeader {
  75. taskTypes?.let { it ->
  76. TaskTypesRow(selectedTaskType, data = it.ocs.data.types) { taskId ->
  77. selectedTaskType = taskId
  78. }
  79. }
  80. }
  81. items(taskTypes?.ocs?.data?.types ?: listOf()) {
  82. Text(text = it.toString())
  83. }
  84. }
  85. if (showAddTaskAlertDialog) {
  86. selectedTaskType?.let {
  87. AddTaskAlertDialog(viewModel, it) {
  88. showAddTaskAlertDialog = false
  89. }
  90. }
  91. }
  92. }
  93. @Composable
  94. private fun AddTaskAlertDialog(viewModel: AssistantViewModel, type: String, dismiss: () -> Unit) {
  95. var input by remember {
  96. mutableStateOf("")
  97. }
  98. // TODO add to UI LIB
  99. SimpleAlertDialog(
  100. backgroundColor = Color.White,
  101. textColor = Color.Black,
  102. titleId = R.string.about_title,
  103. description = stringResource(id = R.string.about_title),
  104. dismiss = { dismiss() },
  105. onComplete = { viewModel.createTask(input = input, type = type) },
  106. content = {
  107. TextField(
  108. placeholder = {
  109. Text(
  110. text = stringResource(id = R.string.samples),
  111. )
  112. },
  113. keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
  114. value = input,
  115. onValueChange = {
  116. input = it
  117. },
  118. singleLine = true
  119. )
  120. }
  121. )
  122. }
  123. @Composable
  124. private fun TaskTypesRow(selectedTaskType: String?, data: List<OcsType>, selectTaskType: (String) -> Unit) {
  125. Row(
  126. modifier = Modifier
  127. .fillMaxWidth()
  128. .horizontalScroll(rememberScrollState())
  129. ) {
  130. data.forEach {
  131. FilledTonalButton(
  132. onClick = { selectTaskType(it.id) },
  133. colors = ButtonDefaults.buttonColors(
  134. containerColor = if (selectedTaskType == it.id) {
  135. Color.Unspecified
  136. } else {
  137. Color.Gray
  138. }
  139. )
  140. ) {
  141. Text(text = it.name)
  142. }
  143. Spacer(modifier = Modifier.padding(end = 8.dp))
  144. }
  145. }
  146. }