Skip to content

Commit

Permalink
chore: Move VssPathHierarchy Logic inside VssPropertiesViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
wba2hi committed Jan 30, 2024
1 parent cf2e0ab commit 5b94cb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import org.eclipse.kuksa.testapp.preferences.ConnectionInfoRepository
import org.eclipse.kuksa.testapp.ui.theme.KuksaAppAndroidTheme
import org.eclipse.kuksa.vsscore.annotation.VssDefinition
import org.eclipse.kuksa.vsscore.model.VssSpecification
import java.util.TreeSet

@VssDefinition("vss_rel_4.0.yaml")
class KuksaDataBrokerActivity : ComponentActivity() {
Expand Down Expand Up @@ -327,8 +326,7 @@ class KuksaDataBrokerActivity : ComponentActivity() {
val entriesList = result?.entriesList
val vssPaths = entriesList?.map { it.path } ?: emptyList()

val vssPathHierarchySet = createVssPathHierarchy(vssPaths)
vssPropertiesViewModel.suggestions = vssPathHierarchySet
vssPropertiesViewModel.updateSuggestions(vssPaths)
}

override fun onError(error: Throwable) {
Expand All @@ -337,20 +335,4 @@ class KuksaDataBrokerActivity : ComponentActivity() {
},
)
}

private fun createVssPathHierarchy(paths: List<String>): TreeSet<String> {
val pathSet = TreeSet<String>()

paths.forEach {
pathSet.add(it)

var value = it
while (value.indexOf(".") > -1) {
value = value.substringBeforeLast(".")
pathSet.add(value)
}
}

return pathSet
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.eclipse.kuksa.model.Property
import org.eclipse.kuksa.proto.v1.Types.Datapoint
import org.eclipse.kuksa.proto.v1.Types.Datapoint.ValueCase
import org.eclipse.kuksa.proto.v1.Types.Field
import java.util.TreeSet

class VSSPropertiesViewModel : ViewModel() {
var onGetProperty: (property: Property) -> Unit = { }
Expand All @@ -47,14 +48,16 @@ class VSSPropertiesViewModel : ViewModel() {
var vssProperties: VSSProperties by mutableStateOf(VSSProperties())
private set

val valueTypes: List<ValueCase> = ValueCase.values().toList()
val valueTypes: List<ValueCase> = ValueCase.entries
val fieldTypes: List<Field> = listOf(
Field.FIELD_VALUE,
Field.FIELD_ACTUATOR_TARGET,
Field.FIELD_METADATA,
)

var suggestions: Collection<String> by mutableStateOf(listOf())
private var _suggestions: Collection<String> by mutableStateOf(listOf())
val suggestions
get() = _suggestions

val datapoint: Datapoint
get() = vssProperties.valueType.createDatapoint(vssProperties.value)
Expand All @@ -66,6 +69,26 @@ class VSSPropertiesViewModel : ViewModel() {
fun updateVssProperties(vssProperties: VSSProperties = VSSProperties()) {
this.vssProperties = vssProperties
}

fun updateSuggestions(vssPaths: Collection<String>) {
this._suggestions = createVssPathHierarchy(vssPaths)
}

private fun createVssPathHierarchy(paths: Collection<String>): TreeSet<String> {
val pathSet = TreeSet<String>()

paths.forEach {
pathSet.add(it)

var value = it
while (value.indexOf(".") > -1) {
value = value.substringBeforeLast(".")
pathSet.add(value)
}
}

return pathSet
}
}

@Immutable
Expand Down

0 comments on commit 5b94cb1

Please sign in to comment.