Skip to content

Commit 5ee0c41

Browse files
committed
Use reflection to iterate over Node's properties
This had no visible affect on UI performance
1 parent 77c3d13 commit 5ee0c41

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/model/Node.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,6 @@ internal data class Node(
3131
override fun hashCode(): Int {
3232
return id.hashCode()
3333
}
34-
35-
companion object {
36-
fun getNodeField(node: Node, field: String): String {
37-
return when (field.lowercase()) {
38-
"name" -> node.name
39-
"id" -> node.id
40-
"parent" -> node.parent
41-
"parentid" -> node.parentId
42-
"props" -> node.props
43-
"state" -> node.state
44-
"rendering" -> node.rendering
45-
"children" -> node.children.toString()
46-
else -> throw IllegalArgumentException("Unknown field: $field")
47-
}
48-
}
49-
}
5034
}
5135

5236
internal fun Node.addChild(child: Node): Node {

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/ui/WorkflowInfoPanel.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import androidx.compose.foundation.layout.height
1212
import androidx.compose.foundation.layout.padding
1313
import androidx.compose.foundation.layout.size
1414
import androidx.compose.foundation.lazy.LazyColumn
15-
import androidx.compose.foundation.lazy.items
1615
import androidx.compose.material.Card
1716
import androidx.compose.material.Icon
1817
import androidx.compose.material.IconButton
@@ -35,6 +34,7 @@ import androidx.compose.ui.text.font.FontWeight
3534
import androidx.compose.ui.unit.dp
3635
import com.squareup.workflow1.traceviewer.model.Node
3736
import com.squareup.workflow1.traceviewer.model.NodeDiff
37+
import kotlin.reflect.full.memberProperties
3838

3939
/**
4040
* A panel that displays information about the selected workflow node.
@@ -103,14 +103,18 @@ private fun NodePanelDetails(
103103
modifier = Modifier.padding(top = 8.dp, bottom = 8.dp)
104104
)
105105
}
106-
val fields = listOf("Name", "Id", "Props", "State", "Rendering")
107106

108-
items(fields) { field ->
109-
DetailCard(
110-
label = field,
111-
currValue = Node.getNodeField(node.current, field),
112-
pastValue = if (node.previous != null) Node.getNodeField(node.previous, field) else null
113-
)
107+
val fields = Node::class.memberProperties
108+
for (field in fields) {
109+
val currVal = field.get(node.current).toString()
110+
val pastVal = if (node.previous != null) field.get(node.previous).toString() else null
111+
item {
112+
DetailCard(
113+
label = field.name,
114+
currValue = currVal,
115+
pastValue = pastVal
116+
)
117+
}
114118
}
115119
}
116120
}

0 commit comments

Comments
 (0)