Skip to content

Commit

Permalink
[patch] make RoundTripDetailView scrollable (#3)
Browse files Browse the repository at this point in the history
Wraps in a lazycolumn with items so that it is scrollable. On large payloads you couldnt see the whole thing which is annyoing

Additionally, pretty prints the json so that it is easier to read
  • Loading branch information
bredmond5 authored Jan 14, 2025
1 parent 4e3769c commit f8c974d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.content.SharedPreferences
import io.branch.interfaces.IBranchLoggingCallbacks
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.serialization.json.*
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -25,6 +25,8 @@ class RoundTripStore(context: Context) : IBranchLoggingCallbacks {

private val FAILED = "failed to parse"

private val json = Json { prettyPrint = true }

override fun onBranchLog(logMessage: String?, severityConstantName: String?) {
scope.launch(Dispatchers.Main) {
processLog(logMessage)
Expand Down Expand Up @@ -89,10 +91,15 @@ class RoundTripStore(context: Context) : IBranchLoggingCallbacks {
return regex.find(log)?.value
}

private fun String.prettyPrint(): String {
val jsonElement = json.parseToJsonElement(this)
return json.encodeToString(jsonElement)
}

private fun parseBody(log: String, identifier: String): String? {
val bodyStart = log.indexOf(identifier).takeIf { it != -1 }?.let { it + identifier.length }
return bodyStart?.let {
log.substring(it).trim()
log.substring(it).trim().prettyPrint()
}
}

Expand Down
36 changes: 27 additions & 9 deletions app/src/main/java/io/branch/branchlinksimulator/RoundTripsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,34 @@ fun RoundTripDetailView(roundTrip: RoundTrip) {
.fillMaxSize()
.padding(16.dp)
) {
VariableView(label = "URL", value = roundTrip.url)
Spacer(modifier = Modifier.height(8.dp))
Text("Request", style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.primary)
VariableView(label = "Body", value = roundTrip.request?.body ?: "FAILED")
Spacer(modifier = Modifier.height(16.dp))
roundTrip.response?.let {
Text("Response", style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.primary)
Text("Status Code: ${it.statusCode}", style = MaterialTheme.typography.titleSmall, color = Color.White)
VariableView(label = "Body", value = it.body)
LazyColumn(
modifier = Modifier
.fillMaxSize()
) {
item {
VariableView(label = "URL", value = roundTrip.url)
}
item {
Spacer(modifier = Modifier.height(8.dp))
}
item {
Text("Request", style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.primary)
}
item {
VariableView(label = "Body", value = roundTrip.request?.body ?: "FAILED")
}
item {
Spacer(modifier = Modifier.height(16.dp))
}
item {
roundTrip.response?.let {
Text("Response", style = MaterialTheme.typography.titleMedium, color = MaterialTheme.colorScheme.primary)
Text("Status Code: ${it.statusCode}", style = MaterialTheme.typography.titleSmall, color = Color.White)
VariableView(label = "Body", value = it.body)
}
}
}

}
}

Expand Down

0 comments on commit f8c974d

Please sign in to comment.