Skip to content

Commit

Permalink
chore: Force update value on GET
Browse files Browse the repository at this point in the history
- Remove redundant DataBroker prefix from compose view files
  • Loading branch information
Chrylo committed Mar 22, 2024
1 parent e1c03bf commit c8be9c2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ class KuksaDataBrokerActivity : ComponentActivity() {
if (result == null) return

vssNodesViewModel.updateNode(result)

outputViewModel.addOutputEntry("Fetched node: $result")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import org.eclipse.kuksa.testapp.extension.compose.RememberCountdown
import org.eclipse.kuksa.testapp.preferences.ConnectionInfoRepository

@Composable
fun DataBrokerConnectionView(viewModel: ConnectionViewModel) {
fun ConnectionView(viewModel: ConnectionViewModel) {
val keyboardController = LocalSoftwareKeyboardController.current

val repositoryConnectionInfoState =
Expand Down Expand Up @@ -221,7 +221,7 @@ private fun ConnectedPreview() {
val connectionInfoRepository = ConnectionInfoRepository(LocalContext.current)
val viewModel = ConnectionViewModel(connectionInfoRepository)
Surface {
DataBrokerConnectionView(viewModel = viewModel)
ConnectionView(viewModel = viewModel)
}
}

Expand All @@ -232,6 +232,6 @@ private fun DisconnectedPreview() {
val viewModel = ConnectionViewModel(connectionInfoRepository)
viewModel.updateConnectionState(ConnectionViewState.CONNECTING)
Surface {
DataBrokerConnectionView(viewModel = viewModel)
ConnectionView(viewModel = viewModel)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import org.eclipse.kuksa.testapp.R
import org.eclipse.kuksa.testapp.databroker.connection.view.connection.DataBrokerConnectionView
import org.eclipse.kuksa.testapp.databroker.connection.view.connection.ConnectionView
import org.eclipse.kuksa.testapp.databroker.connection.viewmodel.ConnectionViewModel
import org.eclipse.kuksa.testapp.databroker.viewmodel.OutputViewModel
import org.eclipse.kuksa.testapp.databroker.viewmodel.TopAppBarViewModel
import org.eclipse.kuksa.testapp.databroker.viewmodel.TopAppBarViewModel.DataBrokerMode
import org.eclipse.kuksa.testapp.databroker.vssnodes.view.DataBrokerVssNodesView
import org.eclipse.kuksa.testapp.databroker.vssnodes.view.VssNodesView
import org.eclipse.kuksa.testapp.databroker.vssnodes.viewmodel.VssNodesViewModel
import org.eclipse.kuksa.testapp.databroker.vsspaths.view.DataBrokerVssPathsView
import org.eclipse.kuksa.testapp.databroker.vsspaths.view.VssPathsView
import org.eclipse.kuksa.testapp.databroker.vsspaths.viewmodel.VSSPathsViewModel
import org.eclipse.kuksa.testapp.extension.compose.Headline
import org.eclipse.kuksa.testapp.extension.compose.OverflowMenu
Expand Down Expand Up @@ -100,13 +100,13 @@ fun DataBrokerView(
) {
Column {
if (!connectionViewModel.isConnected) {
DataBrokerConnectionView(connectionViewModel)
ConnectionView(connectionViewModel)
}
val dataBrokerMode = topAppBarViewModel.dataBrokerMode
if (connectionViewModel.isConnected) {
when (dataBrokerMode) {
DataBrokerMode.VSS_PATH -> DataBrokerVssPathsView(vssPathsViewModel)
DataBrokerMode.VSS_FILE -> DataBrokerVssNodesView(vssNodesViewModel)
DataBrokerMode.VSS_PATH -> VssPathsView(vssPathsViewModel)
DataBrokerMode.VSS_FILE -> VssNodesView(vssNodesViewModel)
}
}
Spacer(modifier = Modifier.padding(top = DefaultElementPadding))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ import org.eclipse.kuksa.vsscore.model.VssSignal
private const val Tag = "DataBrokerVssNodesView"

@Composable
fun DataBrokerVssNodesView(viewModel: VssNodesViewModel) {
fun VssNodesView(viewModel: VssNodesViewModel) {
Column {
Headline(name = "Generated VSS Nodes")

var currentVssSignal: VssSignal<*>? = null
var currentNode = viewModel.node

val currentNode = viewModel.node
val isUpdatePossible = currentNode is VssSignal<*>
val adapter = object : SuggestionAdapter<VssNode> {
override fun toString(item: VssNode): String {
Expand All @@ -93,12 +92,12 @@ fun DataBrokerVssNodesView(viewModel: VssNodesViewModel) {
)
Spacer(modifier = Modifier.padding(top = DefaultElementPadding))
VssNodeInformation(
currentNode,
viewModel,
modifier = Modifier
.fillMaxWidth()
.padding(start = DefaultEdgePadding, end = DefaultEdgePadding),
onSignalChanged = { updatedSignal ->
currentVssSignal = updatedSignal
currentNode = updatedSignal
},
)
Spacer(modifier = Modifier.padding(top = DefaultElementPadding))
Expand All @@ -116,8 +115,8 @@ fun DataBrokerVssNodesView(viewModel: VssNodesViewModel) {
}
Button(
onClick = {
currentVssSignal?.let {
viewModel.onUpdateSignal(it)
(currentNode as VssSignal<*>?)?.let { vssSignal ->
viewModel.onUpdateSignal(vssSignal)
}
},
enabled = isUpdatePossible,
Expand Down Expand Up @@ -146,11 +145,12 @@ fun DataBrokerVssNodesView(viewModel: VssNodesViewModel) {

@Composable
private fun VssNodeInformation(
vssNode: VssNode,
viewModel: VssNodesViewModel,
modifier: Modifier = Modifier,
isShowingInformation: Boolean = false,
onSignalChanged: (VssSignal<*>) -> Unit = {},
) {
val vssNode = viewModel.node
val isVssSignal = vssNode is VssSignal<*>
var isShowingNodeInformation by remember { mutableStateOf(isShowingInformation) }

Expand Down Expand Up @@ -213,8 +213,7 @@ private fun VssNodeInformation(
overflow = TextOverflow.Ellipsis,
)
if (isVssSignal) {
val vssSignal = vssNode as VssSignal<*>
VssSignalInformation(vssSignal, onSignalChanged = onSignalChanged)
VssSignalInformation(viewModel, onSignalChanged = onSignalChanged)
}
}
}
Expand All @@ -230,13 +229,14 @@ private fun VssNodeInformation(

@Composable
private fun VssSignalInformation(
vssSignal: VssSignal<*>,
viewModel: VssNodesViewModel,
modifier: Modifier = Modifier,
onSignalChanged: (VssSignal<*>) -> Unit = {},
) {
val boldSpanStyle = SpanStyle(fontWeight = FontWeight.Bold)
val vssSignal = viewModel.node as VssSignal<*>

var inputValue: String by remember(vssSignal) {
var inputValue: String by remember(vssSignal, viewModel.updateCounter) {
mutableStateOf(vssSignal.value.toString())
}

Expand All @@ -254,6 +254,7 @@ private fun VssSignalInformation(
onValueChange = { newValue ->
inputValue = newValue

// Try and forget approach: Try to brute force the random input string into the VssSignal.
@Suppress("TooGenericExceptionCaught")
try {
val datapoint = vssSignal.valueCase.createDatapoint(newValue.trim())
Expand All @@ -277,22 +278,24 @@ private fun VssSignalInformation(
@Composable
private fun Preview() {
KuksaAppAndroidTheme {
DataBrokerVssNodesView(VssNodesViewModel())
VssNodesView(VssNodesViewModel())
}
}

@Preview
@Composable
private fun NodeInformationPreview() {
KuksaAppAndroidTheme {
VssNodeInformation(VssVehicle(), isShowingInformation = true)
VssNodeInformation(VssNodesViewModel(), isShowingInformation = true)
}
}

@Preview(showBackground = true)
@Composable
private fun SignalInformationPreview() {
KuksaAppAndroidTheme {
VssNodeInformation(VssHeartRate(), isShowingInformation = true)
val vssNodesViewModel = VssNodesViewModel()
vssNodesViewModel.updateNode(VssHeartRate())
VssNodeInformation(vssNodesViewModel, isShowingInformation = true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ class VssNodesViewModel : ViewModel() {
var node: VssNode by mutableStateOf(vssVehicle)
private set

var updateCounter: Int by mutableStateOf(0)
private set

fun updateNode(node: VssNode) {
this.node = node
updateCounter++
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import org.eclipse.kuksa.testapp.extension.compose.SimpleExposedDropdownMenuBox
import org.eclipse.kuksa.testapp.ui.theme.KuksaAppAndroidTheme

@Composable
fun DataBrokerVssPathsView(viewModel: VSSPathsViewModel) {
fun VssPathsView(viewModel: VSSPathsViewModel) {
val dataBrokerProperty = viewModel.dataBrokerProperty
var expanded by remember { mutableStateOf(false) }

Expand Down Expand Up @@ -210,6 +210,6 @@ fun DataBrokerVssPathsView(viewModel: VSSPathsViewModel) {
@Composable
fun Preview() {
KuksaAppAndroidTheme {
DataBrokerVssPathsView(VSSPathsViewModel())
VssPathsView(VSSPathsViewModel())
}
}

0 comments on commit c8be9c2

Please sign in to comment.