Skip to content

Commit 06a519e

Browse files
frebibElementBot
and
ElementBot
authored
Pretty-print event JSON in debug view (#2771)
* Pretty-print event JSON in debug view Significantly improves readability Signed-off-by: Joe Groocock <[email protected]> * Update screenshots Signed-off-by: Joe Groocock <[email protected]> --------- Signed-off-by: Joe Groocock <[email protected]> Co-authored-by: ElementBot <[email protected]>
1 parent c690b38 commit 06a519e

File tree

12 files changed

+18
-12
lines changed

12 files changed

+18
-12
lines changed

changelog.d/2771.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pretty-print event JSON in debug viewer

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/debug/EventDebugInfoView.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ import io.element.android.libraries.designsystem.theme.components.Scaffold
5959
import io.element.android.libraries.designsystem.theme.components.Text
6060
import io.element.android.libraries.designsystem.theme.components.TopAppBar
6161
import io.element.android.libraries.matrix.api.core.EventId
62+
import org.json.JSONException
63+
import org.json.JSONObject
6264

6365
/**
6466
* Screen used to display debug info for events.
@@ -109,18 +111,27 @@ fun EventDebugInfoView(
109111
}
110112
if (originalJson != null) {
111113
item {
112-
CollapsibleSection(title = "Original JSON:", text = originalJson, initiallyExpanded = sectionsInitiallyExpanded)
114+
CollapsibleSection(title = "Original JSON:", text = prettyJSON(originalJson), initiallyExpanded = sectionsInitiallyExpanded)
113115
}
114116
}
115117
if (latestEditedJson != null) {
116118
item {
117-
CollapsibleSection(title = "Latest edited JSON:", text = latestEditedJson, initiallyExpanded = sectionsInitiallyExpanded)
119+
CollapsibleSection(title = "Latest edited JSON:", text = prettyJSON(latestEditedJson), initiallyExpanded = sectionsInitiallyExpanded)
118120
}
119121
}
120122
}
121123
}
122124
}
123125

126+
private fun prettyJSON(maybeJSON: String): String {
127+
return try {
128+
JSONObject(maybeJSON).toString(2)
129+
} catch (e: JSONException) {
130+
// Prefer not pretty-printing over crashing if the data is not actually JSON
131+
maybeJSON
132+
}
133+
}
134+
124135
@Composable
125136
private fun CollapsibleSection(
126137
title: String,

tests/uitests/src/test/snapshots/images/ui_S_t[f.messages.impl.timeline.components.event_TimelineItemEncryptedView_null_TimelineItemEncryptedView-Day-37_37_null,NEXUS_5,1.0,en].png

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/uitests/src/test/snapshots/images/ui_S_t[f.messages.impl.timeline.components.event_TimelineItemEncryptedView_null_TimelineItemEncryptedView-Night-37_38_null,NEXUS_5,1.0,en].png

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)