feat(connector): implement save for Action

This commit is contained in:
Anthony Berg 2024-04-27 00:18:02 +01:00
parent 07b3cfdb26
commit f498c8c0c0
2 changed files with 21 additions and 9 deletions

View File

@ -93,7 +93,7 @@ class Actions (dbActions: List<Action>) : Screen {
} }
item { item {
footer(navigator, viewModel) footer(navigator, viewModel, screenModel)
} }
} }
@ -123,7 +123,7 @@ class Actions (dbActions: List<Action>) : Screen {
Column ( Column (
verticalArrangement = Arrangement.spacedBy(itemPadding) verticalArrangement = Arrangement.spacedBy(itemPadding)
) { ) {
Text(text = "Action ${item.id}") Text(text = "Action ${item.step + 1}")
Row( Row(
Modifier.fillMaxWidth(), Modifier.fillMaxWidth(),
@ -132,7 +132,7 @@ class Actions (dbActions: List<Action>) : Screen {
OutlinedTextField( OutlinedTextField(
modifier = Modifier.fillMaxWidth(0.6f), modifier = Modifier.fillMaxWidth(0.6f),
value = item.type, value = item.type,
onValueChange = { inputs[item.id] = inputs[item.id].copy(type = it) }, onValueChange = { inputs[item.id - 1] = inputs[item.id - 1].copy(type = it) },
label = { Text("Dataref Name") }, label = { Text("Dataref Name") },
singleLine = true singleLine = true
) )
@ -140,7 +140,7 @@ class Actions (dbActions: List<Action>) : Screen {
OutlinedTextField( OutlinedTextField(
value = item.goal, value = item.goal,
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
onValueChange = { inputs[item.id] = inputs[item.id].copy(goal = it) }, onValueChange = { inputs[item.id - 1] = inputs[item.id - 1].copy(goal = it) },
label = { Text("Desired State") }, label = { Text("Desired State") },
singleLine = true singleLine = true
) )
@ -152,16 +152,16 @@ class Actions (dbActions: List<Action>) : Screen {
@OptIn(ExperimentalResourceApi::class) @OptIn(ExperimentalResourceApi::class)
@Composable @Composable
private fun footer(navigator: Navigator, viewModel: InterfaceState) { private fun footer(navigator: Navigator, viewModel: InterfaceState, screenModel: ActionsScreenModel) {
Row ( Row (
Modifier.fillMaxWidth(), Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween horizontalArrangement = Arrangement.SpaceBetween
) { ) {
// Add Step // Add Step
// TODO add menu to choose multiple types of items
FilledTonalButton( FilledTonalButton(
contentPadding = ButtonDefaults.ButtonWithIconContentPadding, contentPadding = ButtonDefaults.ButtonWithIconContentPadding,
onClick = { onClick = {
// TODO make this a proper data array for each item in checklist
val procedureId = viewModel.procedureId val procedureId = viewModel.procedureId
if (procedureId != null) { if (procedureId != null) {
inputs += createEmptyAction(procedureId) inputs += createEmptyAction(procedureId)
@ -181,7 +181,7 @@ class Actions (dbActions: List<Action>) : Screen {
contentPadding = ButtonDefaults.ButtonWithIconContentPadding, contentPadding = ButtonDefaults.ButtonWithIconContentPadding,
onClick = { onClick = {
// TODO make checks // TODO make checks
// TODO save to database screenModel.saveActions(inputs)
navigator.pop() navigator.pop()
viewModel.procedureId = null viewModel.procedureId = null
} }
@ -197,12 +197,12 @@ class Actions (dbActions: List<Action>) : Screen {
} }
private fun createEmptyAction(procedureId: Int): Action { private fun createEmptyAction(procedureId: Int): Action {
val index = inputs.size val index = inputs.size + 1
val action = Action( val action = Action(
id = index, id = index,
procedureId = procedureId, procedureId = procedureId,
step = index, step = index - 1,
type = "", type = "",
goal = "" goal = ""
) )

View File

@ -26,6 +26,18 @@ class ActionsScreenModel (
fun loadedActions() { fun loadedActions() {
mutableState.value = ActionsState.Idle mutableState.value = ActionsState.Idle
} }
fun saveActions(actions: List<Action>) {
screenModelScope.launch {
val procedureId = interfaceState.procedureId ?: return@launch
// Delete all previous items of the actions from the database
db.deleteActionByProcedure(procedureId = procedureId)
// Add the new actions to the database
db.createActionFromList(actions = actions)
}
}
} }
sealed class ActionsState { sealed class ActionsState {