Skip to content

Commit

Permalink
chore: Remember selected item on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrylo committed Mar 25, 2024
1 parent e5c6398 commit 79dbe87
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@
package org.eclipse.kuksa.testapp.databroker.view.suggestions

interface SuggestionAdapter<T : Any> {
fun toString(item: T): String
val items: Collection<T>

val startingItem: T

fun toString(item: T): String = item.toString()
}

class DefaultSuggestionAdapter<T : Any> : SuggestionAdapter<T> {
class DefaultSuggestionAdapter<T : Any>(override val items: Collection<T> = emptyList()) : SuggestionAdapter<T> {
override val startingItem: T
get() = items.first()

override fun toString(item: T): String {
return item.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ import org.eclipse.kuksa.testapp.R

@Composable
fun <T : Any> SuggestionTextView(
suggestions: Collection<T>,
adapter: SuggestionAdapter<T> = DefaultSuggestionAdapter(),
value: String = "",
onItemSelected: ((T?) -> Unit)? = null,
onValueChanged: ((String) -> Unit)? = null,
label: @Composable (() -> Unit)? = null,
singleLine: Boolean = false,
modifier: Modifier,
) {
val suggestions = adapter.items

var text by remember {
mutableStateOf(value)
mutableStateOf(adapter.toString(adapter.startingItem))
}

val heightTextFields by remember {
Expand Down Expand Up @@ -190,19 +190,19 @@ fun <T : Any> SuggestionTextView(
modifier = Modifier.heightIn(max = 150.dp),
) {
items(
suggestions.filter {
adapter.toString(it).lowercase().contains(text.lowercase())
suggestions.filter { item ->
adapter.toString(item).lowercase().contains(text.lowercase())
},
) {
) { item ->
SuggestionItem(
item = it,
itemText = adapter.toString(it),
) { item ->
text = adapter.toString(item)
item = item,
itemText = adapter.toString(item),
) { suggestionItem ->
text = adapter.toString(suggestionItem)
expanded = false
focusManager.clearFocus()
onValueChanged?.invoke(text)
onItemSelected?.invoke(it)
onItemSelected?.invoke(suggestionItem)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ import org.eclipse.kuksa.vss.VssVehicle
import org.eclipse.kuksa.vsscore.model.VssNode
import org.eclipse.kuksa.vsscore.model.VssSignal

private class VssNodeSuggestionAdapter(
override val items: Collection<VssNode>,
override val startingItem: VssNode,
) : SuggestionAdapter<VssNode> {
override fun toString(item: VssNode): String {
return item.vssPath
}
}

@Composable
fun VssNodesView(viewModel: VssNodesViewModel) {
val focusManager = LocalFocusManager.current
Expand All @@ -72,15 +81,9 @@ fun VssNodesView(viewModel: VssNodesViewModel) {
var currentNode = viewModel.node

val isUpdatePossible = currentNode is VssSignal<*>
val adapter = object : SuggestionAdapter<VssNode> {
override fun toString(item: VssNode): String {
return item.vssPath
}
}
val adapter = VssNodeSuggestionAdapter(viewModel.nodes, currentNode)

SuggestionTextView(
value = "Vehicle",
suggestions = viewModel.nodes,
adapter = adapter,
onItemSelected = {
val vssNode = it ?: VssVehicle()
Expand Down Expand Up @@ -245,7 +248,14 @@ private fun VssSignalInformation(
val vssSignal = viewModel.node as VssSignal<*>

var inputValue: String by remember(vssSignal, viewModel.updateCounter) {
mutableStateOf(vssSignal.value.toString())
val value = if (vssSignal.value is Array<*>) {
val valueArray = vssSignal.value as Array<*>
valueArray.joinToString()
} else {
vssSignal.value.toString()
}

mutableStateOf(value)
}

Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,30 @@ import androidx.compose.ui.unit.dp
import org.eclipse.kuksa.proto.v1.Types
import org.eclipse.kuksa.testapp.databroker.view.DefaultEdgePadding
import org.eclipse.kuksa.testapp.databroker.view.DefaultElementPadding
import org.eclipse.kuksa.testapp.databroker.view.suggestions.SuggestionAdapter
import org.eclipse.kuksa.testapp.databroker.view.suggestions.SuggestionTextView
import org.eclipse.kuksa.testapp.databroker.vsspaths.viewmodel.VSSPathsViewModel
import org.eclipse.kuksa.testapp.extension.compose.Headline
import org.eclipse.kuksa.testapp.extension.compose.SimpleExposedDropdownMenuBox
import org.eclipse.kuksa.testapp.ui.theme.KuksaAppAndroidTheme

private class VssPathSuggestionAdapter(
override val items: Collection<String>,
override val startingItem: String,
) : SuggestionAdapter<String>

@Composable
fun VssPathsView(viewModel: VSSPathsViewModel) {
val dataBrokerProperty = viewModel.dataBrokerProperty
val suggestionAdapter = VssPathSuggestionAdapter(viewModel.suggestions, dataBrokerProperty.vssPath)

var expanded by remember { mutableStateOf(false) }

Column {
Headline(name = "VSS Paths")

SuggestionTextView(
suggestions = viewModel.suggestions,
value = dataBrokerProperty.vssPath,
adapter = suggestionAdapter,
onValueChanged = {
val newVssProperties = dataBrokerProperty.copy(
vssPath = it,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ val Types.Metadata.valueType: ValueCase
*/
val <T : Any> VssSignal<T>.datapoint: Datapoint
get() {
// TODO: Only supports string arrays for now, IntArray, DoubleArray etc. are not supported yet
// TODO: Only supports string arrays for now, IntArray, DoubleArray etc. are not supported yet because
// TODO: IntArrays are custom types which to not implement the Array interface and can't be cast to it.
val stringValue = if (value::class.java.isArray) {
val valueArray = value as Array<*>
valueArray.joinToString()
Expand Down

0 comments on commit 79dbe87

Please sign in to comment.