Skip to content

Commit 6a056ca

Browse files
committed
Fix PR comments
1 parent 4a8d29d commit 6a056ca

File tree

10 files changed

+204
-178
lines changed

10 files changed

+204
-178
lines changed

workflow-trace-viewer/api/workflow-trace-viewer.api

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,3 @@ public final class com/squareup/workflow1/traceviewer/ui/control/ComposableSingl
3333
public final fun getLambda-1$wf1_workflow_trace_viewer ()Lkotlin/jvm/functions/Function3;
3434
}
3535

36-
public final class com/squareup/workflow1/traceviewer/util/parser/JsonParserKt {
37-
public static final field ROOT_ID Ljava/lang/String;
38-
}
39-

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import androidx.compose.ui.Alignment
1919
import androidx.compose.ui.Modifier
2020
import androidx.compose.ui.geometry.Offset
2121
import androidx.compose.ui.layout.onGloballyPositioned
22+
import androidx.compose.ui.layout.onSizeChanged
2223
import androidx.compose.ui.unit.IntSize
2324
import androidx.compose.ui.unit.dp
25+
import androidx.compose.ui.window.WindowPosition.PlatformDefault.x
2426
import com.squareup.workflow1.traceviewer.model.Node
2527
import com.squareup.workflow1.traceviewer.model.NodeUpdate
2628
import com.squareup.workflow1.traceviewer.ui.RightInfoPanel
@@ -63,8 +65,8 @@ internal fun App(
6365
}
6466

6567
Box(
66-
modifier = modifier.onGloballyPositioned {
67-
appWindowSize = it.size
68+
modifier = modifier.onSizeChanged {
69+
appWindowSize = it
6870
}
6971
) {
7072
fun resetStates() {
@@ -113,17 +115,20 @@ internal fun App(
113115
// frameSize has not been updated yet, so on the first frame, frameSize = nodeLocations.size = 0,
114116
// and it will append a new map
115117
while (nodeLocations.size <= frameSize) {
116-
nodeLocations.add(mutableStateMapOf())
118+
nodeLocations += mutableStateMapOf()
117119
}
118120
}
119121

122+
val frameNodeLocations = nodeLocations[frameInd]
120123
SearchBox(
121-
nodes = nodeLocations[frameInd].keys.toList(),
124+
nodes = frameNodeLocations.keys.toList(),
122125
onSearch = { name ->
123126
sandboxState.scale = 1f
124-
val node = nodeLocations[frameInd].keys.firstOrNull { it.name == name }
125-
val newX = sandboxState.offset.x - nodeLocations[frameInd][node]!!.x + appWindowSize.width / 2
126-
val newY = sandboxState.offset.y - nodeLocations[frameInd][node]!!.y + appWindowSize.height / 2
127+
val node = frameNodeLocations.keys.first { it.name == name }
128+
val newX = (sandboxState.offset.x - frameNodeLocations.getValue(node).x
129+
+ appWindowSize.width / 2)
130+
val newY = (sandboxState.offset.y - frameNodeLocations.getValue(node).y
131+
+ appWindowSize.height / 2)
127132
sandboxState.offset = Offset(x = newX, y = newY)
128133
},
129134
)
@@ -216,7 +221,7 @@ internal sealed interface TraceMode {
216221
/**
217222
* Allows users to select from multiple devices that are currently running.
218223
*/
219-
internal fun listDevices(): List<String> {
224+
private fun listDevices(): List<String> {
220225
val process = ProcessBuilder("adb", "devices", "-l").start()
221226
process.waitFor()
222227
// We drop the header "List of devices attached"

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,15 @@ internal data class Node(
3333
}
3434

3535
companion object {
36-
fun getNodeFields(): List<String> {
37-
return listOf("Props", "State")
38-
}
39-
40-
fun getNodeData(node: Node, field: String): String {
41-
return when (field.lowercase()) {
42-
"props" -> node.props
43-
"state" -> node.state
44-
else -> throw IllegalArgumentException("Unknown field: $field")
45-
}
46-
}
36+
val nodeFields: List<String> = listOf("Props", "State")
37+
}
38+
}
39+
40+
internal fun Node.getNodeData(field: String): String {
41+
return when (field.lowercase()) {
42+
"props" -> props
43+
"state" -> state
44+
else -> throw IllegalArgumentException("Unknown field: $field")
4745
}
4846
}
4947

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import androidx.compose.ui.text.style.TextOverflow
3636
import androidx.compose.ui.unit.dp
3737
import com.squareup.workflow1.traceviewer.model.Node
3838
import com.squareup.workflow1.traceviewer.model.NodeUpdate
39+
import com.squareup.workflow1.traceviewer.model.getNodeData
3940
import com.squareup.workflow1.traceviewer.util.parser.computeAnnotatedDiff
4041

4142
/**
@@ -123,10 +124,10 @@ private fun NodePanelDetails(
123124
)
124125
}
125126

126-
val fields = Node.getNodeFields()
127+
val fields = Node.nodeFields
127128
for (field in fields) {
128-
val currVal = Node.getNodeData(node.current, field)
129-
val pastVal = if (node.past != null) Node.getNodeData(node.past, field) else null
129+
val currVal = node.current.getNodeData(field)
130+
val pastVal = if (node.past != null) node.past.getNodeData(field) else null
130131
item {
131132
DetailCard(
132133
label = field,

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import androidx.compose.foundation.layout.Row
1313
import androidx.compose.foundation.layout.fillMaxSize
1414
import androidx.compose.foundation.layout.fillMaxWidth
1515
import androidx.compose.foundation.layout.padding
16+
import androidx.compose.foundation.layout.wrapContentSize
1617
import androidx.compose.foundation.shape.RoundedCornerShape
1718
import androidx.compose.material.Text
1819
import androidx.compose.runtime.Composable
20+
import androidx.compose.runtime.DisposableEffect
1921
import androidx.compose.runtime.LaunchedEffect
2022
import androidx.compose.runtime.getValue
2123
import androidx.compose.runtime.remember
@@ -75,7 +77,7 @@ internal fun DrawTree(
7577
isAffected = isAffected,
7678
isExpanded = isExpanded,
7779
onNodeSelect = onNodeSelect,
78-
onExpandToggle = { expandedNodes[node.id] = !expandedNodes[node.id]!! },
80+
onExpandToggle = { expandedNodes[node.id] = !expandedNodes.getValue(node.id) },
7981
storeNodeLocation = storeNodeLocation
8082
)
8183

@@ -126,8 +128,9 @@ private fun UnaffectedChildrenGroup(
126128
if (children.isEmpty()) return
127129

128130
val groupKey = "${node.id}_unaffected_group"
129-
LaunchedEffect(Unit) {
131+
DisposableEffect(Unit) {
130132
expandedNodes[groupKey] = false
133+
onDispose {}
131134
}
132135
val isGroupExpanded = expandedNodes[groupKey] == true
133136

@@ -325,7 +328,10 @@ private fun DrawNode(
325328
storeNodeLocation(node, offsetToCenter)
326329
}
327330
) {
328-
Column(horizontalAlignment = Alignment.CenterHorizontally) {
331+
Column(
332+
horizontalAlignment = Alignment.CenterHorizontally,
333+
modifier = Modifier.wrapContentSize()
334+
) {
329335
Row(
330336
verticalAlignment = Alignment.CenterVertically,
331337
horizontalArrangement = Arrangement.spacedBy(4.dp)
@@ -358,16 +364,14 @@ private fun NodeTooltip(
358364
nodeState: NodeState,
359365
modifier: Modifier = Modifier
360366
) {
361-
Box(
367+
Text(
362368
modifier = modifier
369+
.wrapContentSize()
363370
.clip(RoundedCornerShape(4.dp))
364371
.background(Color.Black.copy(alpha = 0.3f))
365-
.padding(horizontal = 8.dp, vertical = 4.dp)
366-
) {
367-
Text(
368-
text = nodeState.name,
369-
color = Color.White,
370-
fontSize = 12.sp
371-
)
372-
}
372+
.padding(horizontal = 8.dp, vertical = 4.dp),
373+
text = nodeState.name,
374+
color = Color.White,
375+
fontSize = 12.sp
376+
)
373377
}

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ import androidx.compose.material.Card
1010
import androidx.compose.material.ExperimentalMaterialApi
1111
import androidx.compose.material.Text
1212
import androidx.compose.runtime.Composable
13+
import androidx.compose.runtime.key
1314
import androidx.compose.ui.Alignment
1415
import androidx.compose.ui.Modifier
1516
import androidx.compose.ui.graphics.Color
1617
import androidx.compose.ui.unit.dp
1718

19+
/**
20+
* Only give back the specific emulator device, i.e. "emulator-5554"
21+
*/
22+
private val emulatorRegex = Regex("""\bemulator-\d+\b""")
23+
1824
@OptIn(ExperimentalMaterialApi::class)
1925
@Composable
2026
internal fun DisplayDevices(
@@ -35,25 +41,25 @@ internal fun DisplayDevices(
3541
return@Box
3642
}
3743

38-
val emulatorRegex = Regex("""\bemulator-\d+\b""")
3944
Column {
4045
devices.forEach { device ->
41-
Card(
42-
onClick = {
43-
// Only give back the specific emulator device, i.e. "emulator-5554"
44-
emulatorRegex.find(device)?.value?.let { emulator ->
45-
onDeviceSelect(emulator)
46-
}
47-
},
48-
shape = RoundedCornerShape(16.dp),
49-
border = BorderStroke(1.dp, Color.Gray),
50-
modifier = Modifier.padding(4.dp),
51-
elevation = 2.dp
52-
) {
53-
Text(
54-
text = device,
55-
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
56-
)
46+
key(device) {
47+
Card(
48+
onClick = {
49+
emulatorRegex.find(device)?.value?.let { emulator ->
50+
onDeviceSelect(emulator)
51+
}
52+
},
53+
shape = RoundedCornerShape(16.dp),
54+
border = BorderStroke(1.dp, Color.Gray),
55+
modifier = Modifier.padding(4.dp),
56+
elevation = 2.dp
57+
) {
58+
Text(
59+
text = device,
60+
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
61+
)
62+
}
5763
}
5864
}
5965
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.compose.material3.SearchBarColors
1515
import androidx.compose.material3.SearchBarDefaults
1616
import androidx.compose.runtime.Composable
1717
import androidx.compose.runtime.getValue
18+
import androidx.compose.runtime.key
1819
import androidx.compose.runtime.mutableStateOf
1920
import androidx.compose.runtime.remember
2021
import androidx.compose.runtime.setValue
@@ -65,18 +66,20 @@ internal fun SearchBox(
6566
val relevantNodes = nodes.filter { it.name.contains(searchText, ignoreCase = true) }
6667
Column {
6768
relevantNodes.take(5).forEach { node ->
68-
ListItem(
69-
headlineContent = { Text(node.name) },
70-
modifier = Modifier
71-
.clickable {
72-
onSearch(node.name)
73-
expanded = false
74-
},
75-
colors = ListItemDefaults.colors(
76-
containerColor = Color.White,
77-
headlineColor = Color.Black
69+
key(node.id) {
70+
ListItem(
71+
headlineContent = { Text(node.name) },
72+
modifier = Modifier
73+
.clickable {
74+
onSearch(node.name)
75+
expanded = false
76+
},
77+
colors = ListItemDefaults.colors(
78+
containerColor = Color.White,
79+
headlineColor = Color.Black
80+
)
7881
)
79-
)
82+
}
8083
}
8184
}
8285
}

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/util/SandboxBackground.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal fun SandboxBackground(
3838
sandboxState.offset += translation
3939
}
4040
}
41-
.pointerInput(Unit) {
41+
.pointerInput(appWindowSize) {
4242
// Zooming capabilities: watches for any scroll events and immediately consumes changes.
4343
// - This is AI generated.
4444
awaitEachGesture {

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/util/parser/DiffUtils.kt

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import com.github.difflib.text.DiffRow.Tag
88
import com.github.difflib.text.DiffRowGenerator
99
import com.squareup.workflow1.traceviewer.util.parser.DiffStyles.buildStringWithStyle
1010

11+
/**
12+
* Matching for the type name of a field. This is used to pull out the name to compare
13+
* Loading() -> Loading; Idle() -> Idle; CheckoutAppletWorkflow() -> CheckoutAppletWorkflow
14+
*/
15+
private val stateRegex = Regex("""^(\w+)\(""")
16+
1117
/**
1218
* Generates a field-level word-diff for each node's states.
1319
*
@@ -116,35 +122,36 @@ internal fun computeAnnotatedDiff(
116122
* Parses the full diff within Tag.CHANGED to give back a list of operations to perform
117123
*/
118124
private fun parseChangedDiff(fullDiff: String): List<Pair<SpanStyle, String>> {
119-
val operations: MutableList<Pair<SpanStyle, String>> = mutableListOf()
120-
var i = 0
121-
while (i < fullDiff.length) {
122-
when {
123-
fullDiff.startsWith("--", i) -> {
124-
val end = fullDiff.indexOf("--", i + 2)
125-
if (end != -1) {
126-
val removed = fullDiff.substring(i + 2, end)
127-
operations.add(DiffStyles.DELETE to removed)
128-
i = end + 2
125+
val operations = buildList {
126+
var i = 0
127+
while (i < fullDiff.length) {
128+
when {
129+
fullDiff.startsWith("--", i) -> {
130+
val end = fullDiff.indexOf("--", i + 2)
131+
if (end != -1) {
132+
val removed = fullDiff.substring(i + 2, end)
133+
add(DiffStyles.DELETE to removed)
134+
i = end + 2
135+
}
129136
}
130-
}
131137

132-
fullDiff.startsWith("++", i) -> {
133-
val end = fullDiff.indexOf("++", i + 2)
134-
if (end != -1) {
135-
val added = fullDiff.substring(i + 2, end)
136-
operations.add(DiffStyles.INSERT to added)
137-
i = end + 2
138+
fullDiff.startsWith("++", i) -> {
139+
val end = fullDiff.indexOf("++", i + 2)
140+
if (end != -1) {
141+
val added = fullDiff.substring(i + 2, end)
142+
add(DiffStyles.INSERT to added)
143+
i = end + 2
144+
}
138145
}
139-
}
140146

141-
else -> {
142-
val nextTagStart = listOf(
143-
fullDiff.indexOf("--", i),
144-
fullDiff.indexOf("++", i)
145-
).filter { it >= 0 }.minOrNull() ?: fullDiff.length
146-
operations.add(DiffStyles.UNCHANGED to fullDiff.substring(i, nextTagStart))
147-
i = nextTagStart
147+
else -> {
148+
val nextTagStart = listOf(
149+
fullDiff.indexOf("--", i),
150+
fullDiff.indexOf("++", i)
151+
).filter { it >= 0 }.minOrNull() ?: fullDiff.length
152+
add(DiffStyles.UNCHANGED to fullDiff.substring(i, nextTagStart))
153+
i = nextTagStart
154+
}
148155
}
149156
}
150157
}
@@ -216,7 +223,6 @@ private fun getFieldsAsList(field: String): List<String> {
216223
}
217224

218225
private fun extractTypeName(field: String): String {
219-
val stateRegex = Regex("""^(\w+)\(""")
220226
// If regex doesn't match, that means it's likely "kotlin.Unit" or "0"
221227
return stateRegex.find(field)?.groupValues?.get(1) ?: field
222228
}

0 commit comments

Comments
 (0)