From f498c8c0c0bed2f74d49d43a11762f670aace0cc Mon Sep 17 00:00:00 2001 From: Anthony Berg Date: Sat, 27 Apr 2024 00:18:02 +0100 Subject: [PATCH] feat(connector): implement save for Action --- .../kotlin/tab/procedure/Actions.kt | 18 +++++++++--------- .../kotlin/tab/procedure/ActionsScreenModel.kt | 12 ++++++++++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/connector/composeApp/src/desktopMain/kotlin/tab/procedure/Actions.kt b/connector/composeApp/src/desktopMain/kotlin/tab/procedure/Actions.kt index d15669f..1cb5bf2 100644 --- a/connector/composeApp/src/desktopMain/kotlin/tab/procedure/Actions.kt +++ b/connector/composeApp/src/desktopMain/kotlin/tab/procedure/Actions.kt @@ -93,7 +93,7 @@ class Actions (dbActions: List) : Screen { } item { - footer(navigator, viewModel) + footer(navigator, viewModel, screenModel) } } @@ -123,7 +123,7 @@ class Actions (dbActions: List) : Screen { Column ( verticalArrangement = Arrangement.spacedBy(itemPadding) ) { - Text(text = "Action ${item.id}") + Text(text = "Action ${item.step + 1}") Row( Modifier.fillMaxWidth(), @@ -132,7 +132,7 @@ class Actions (dbActions: List) : Screen { OutlinedTextField( modifier = Modifier.fillMaxWidth(0.6f), 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") }, singleLine = true ) @@ -140,7 +140,7 @@ class Actions (dbActions: List) : Screen { OutlinedTextField( value = item.goal, 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") }, singleLine = true ) @@ -152,16 +152,16 @@ class Actions (dbActions: List) : Screen { @OptIn(ExperimentalResourceApi::class) @Composable - private fun footer(navigator: Navigator, viewModel: InterfaceState) { + private fun footer(navigator: Navigator, viewModel: InterfaceState, screenModel: ActionsScreenModel) { Row ( Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween ) { // Add Step + // TODO add menu to choose multiple types of items FilledTonalButton( contentPadding = ButtonDefaults.ButtonWithIconContentPadding, onClick = { - // TODO make this a proper data array for each item in checklist val procedureId = viewModel.procedureId if (procedureId != null) { inputs += createEmptyAction(procedureId) @@ -181,7 +181,7 @@ class Actions (dbActions: List) : Screen { contentPadding = ButtonDefaults.ButtonWithIconContentPadding, onClick = { // TODO make checks - // TODO save to database + screenModel.saveActions(inputs) navigator.pop() viewModel.procedureId = null } @@ -197,12 +197,12 @@ class Actions (dbActions: List) : Screen { } private fun createEmptyAction(procedureId: Int): Action { - val index = inputs.size + val index = inputs.size + 1 val action = Action( id = index, procedureId = procedureId, - step = index, + step = index - 1, type = "", goal = "" ) diff --git a/connector/composeApp/src/desktopMain/kotlin/tab/procedure/ActionsScreenModel.kt b/connector/composeApp/src/desktopMain/kotlin/tab/procedure/ActionsScreenModel.kt index bfb4ab2..b003127 100644 --- a/connector/composeApp/src/desktopMain/kotlin/tab/procedure/ActionsScreenModel.kt +++ b/connector/composeApp/src/desktopMain/kotlin/tab/procedure/ActionsScreenModel.kt @@ -26,6 +26,18 @@ class ActionsScreenModel ( fun loadedActions() { mutableState.value = ActionsState.Idle } + + fun saveActions(actions: List) { + 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 {