Pretty-print event JSON in debug view (#2771)

* Pretty-print event JSON in debug view

Significantly improves readability

Signed-off-by: Joe Groocock <me@frebib.net>

* Update screenshots

Signed-off-by: Joe Groocock <me@frebib.net>

---------

Signed-off-by: Joe Groocock <me@frebib.net>
Co-authored-by: ElementBot <benoitm+elementbot@element.io>
This commit is contained in:
Joe Groocock
2024-04-30 15:08:04 +01:00
committed by GitHub
parent 2559d26dc0
commit 3e9ae00090
12 changed files with 18 additions and 12 deletions

1
changelog.d/2771.feature Normal file
View File

@@ -0,0 +1 @@
Pretty-print event JSON in debug viewer

View File

@@ -59,6 +59,8 @@ import io.element.android.libraries.designsystem.theme.components.Scaffold
import io.element.android.libraries.designsystem.theme.components.Text
import io.element.android.libraries.designsystem.theme.components.TopAppBar
import io.element.android.libraries.matrix.api.core.EventId
import org.json.JSONException
import org.json.JSONObject
/**
* Screen used to display debug info for events.
@@ -109,18 +111,27 @@ fun EventDebugInfoView(
}
if (originalJson != null) {
item {
CollapsibleSection(title = "Original JSON:", text = originalJson, initiallyExpanded = sectionsInitiallyExpanded)
CollapsibleSection(title = "Original JSON:", text = prettyJSON(originalJson), initiallyExpanded = sectionsInitiallyExpanded)
}
}
if (latestEditedJson != null) {
item {
CollapsibleSection(title = "Latest edited JSON:", text = latestEditedJson, initiallyExpanded = sectionsInitiallyExpanded)
CollapsibleSection(title = "Latest edited JSON:", text = prettyJSON(latestEditedJson), initiallyExpanded = sectionsInitiallyExpanded)
}
}
}
}
}
private fun prettyJSON(maybeJSON: String): String {
return try {
JSONObject(maybeJSON).toString(2)
} catch (e: JSONException) {
// Prefer not pretty-printing over crashing if the data is not actually JSON
maybeJSON
}
}
@Composable
private fun CollapsibleSection(
title: String,