Skip to content

Commit 25d04a4

Browse files
committed
Move away from using reflection for accessing node fields
Using reflection gives us the declared fields of a class only in alphabetical order, and some information is unnecessarily inside, so we instead just manually give back a list and the data associated with them through companion methods.
1 parent 9c1fc6a commit 25d04a4

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ internal data class Node(
3131
override fun hashCode(): Int {
3232
return id.hashCode()
3333
}
34+
35+
companion object {
36+
fun getNodeFields(): List<String> {
37+
return listOf("id", "parent", "parentId", "props", "state", "rendering")
38+
}
39+
40+
fun getNodeData(node: Node, field: String): String {
41+
return when (field.lowercase()) {
42+
"id" -> node.id
43+
"parent" -> node.parent
44+
"parentid" -> node.parentId
45+
"props" -> node.props
46+
"state" -> node.state
47+
"rendering" -> node.rendering
48+
else -> throw IllegalArgumentException("Unknown field: $field")
49+
}
50+
}
51+
}
3452
}
3553

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

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ import androidx.compose.ui.graphics.Color
3131
import androidx.compose.ui.text.TextStyle
3232
import androidx.compose.ui.text.font.FontStyle
3333
import androidx.compose.ui.text.font.FontWeight
34+
import androidx.compose.ui.text.style.TextAlign
3435
import androidx.compose.ui.unit.dp
3536
import com.squareup.workflow1.traceviewer.model.Node
3637
import com.squareup.workflow1.traceviewer.model.NodeUpdate
37-
import kotlin.reflect.full.memberProperties
3838

3939
/**
4040
* A panel that displays information about the selected workflow node.
@@ -98,19 +98,20 @@ private fun NodePanelDetails(
9898
}
9999
item {
100100
Text(
101-
text = "Workflow Details",
101+
text = node.current.name,
102102
style = MaterialTheme.typography.h6,
103-
modifier = Modifier.padding(top = 8.dp, bottom = 8.dp)
103+
modifier = Modifier.padding(top = 8.dp, bottom = 8.dp),
104+
textAlign = TextAlign.Center
104105
)
105106
}
106107

107-
val fields = Node::class.memberProperties
108+
val fields = Node.getNodeFields()
108109
for (field in fields) {
109-
val currVal = field.get(node.current).toString()
110-
val pastVal = if (node.past != null) field.get(node.past).toString() else null
110+
val currVal = Node.getNodeData(node.current, field)
111+
val pastVal = if (node.past != null) Node.getNodeData(node.past, field) else null
111112
item {
112113
DetailCard(
113-
label = field.name,
114+
label = field,
114115
currValue = currVal,
115116
pastValue = pastVal
116117
)

0 commit comments

Comments
 (0)