Skip to content

Commit 31d2985

Browse files
committed
feat: Various improvements to WhatTheStackActivity
- Remove kotlin synthetics - Use kotlin 1.4 stacktraceToString method - Fix system insets on vertical edges - Enable a horizontally scrolling stacktrace
1 parent aa47df0 commit 31d2985

File tree

6 files changed

+38
-35
lines changed

6 files changed

+38
-35
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ buildscript {
1818
"materialComponents": "1.1.0",
1919
"fragment" : "1.2.4",
2020
"constraintLayout" : "1.1.3",
21-
"insetter" : "0.2.2",
21+
"insetter" : "0.3.1",
2222
"junit" : "4.12",
2323
"androidxTestCore" : "1.2.0",
2424
"androidxTestExt" : "1.1.1",

what-the-stack/build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ android {
3333
targetCompatibility JavaVersion.VERSION_1_8
3434
}
3535

36-
androidExtensions {
37-
experimental = true
38-
}
39-
4036
kotlin {
4137
explicitApi()
4238
}

what-the-stack/src/main/java/com/haroldadmin/whatthestack/ExceptionProcessor.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal fun Throwable.process(): ExceptionData {
2929
val rootCauseOfException = rootCause()
3030
val cause = rootCauseOfException.type()
3131
val message = rootCauseOfException.message ?: "Unknown"
32-
val stacktrace = rootCauseOfException.stacktrace()
32+
val stacktrace = this.stackTraceToString()
3333
return ExceptionData(type, cause, message, stacktrace)
3434
}
3535

@@ -53,12 +53,3 @@ internal tailrec fun Throwable.rootCause(): Throwable {
5353
internal fun Throwable.type(): String {
5454
return this::class.java.simpleName
5555
}
56-
57-
/**
58-
* Returns the stacktrace of the exception in the form of a string.
59-
*/
60-
internal fun Throwable.stacktrace(): String {
61-
val outputStream = StringOutputStream()
62-
printStackTrace(PrintStream(outputStream))
63-
return outputStream.getString()
64-
}

what-the-stack/src/main/java/com/haroldadmin/whatthestack/WhatTheStackActivity.kt

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ import android.content.ClipData
44
import android.content.ClipboardManager
55
import android.content.Context
66
import android.os.Bundle
7+
import android.text.method.ScrollingMovementMethod
78
import android.view.View
9+
import android.widget.TextView
810
import androidx.appcompat.app.AppCompatActivity
11+
import androidx.appcompat.widget.AppCompatTextView
912
import androidx.core.view.updatePadding
13+
import com.google.android.material.button.MaterialButton
1014
import com.google.android.material.snackbar.Snackbar
15+
import dev.chrisbanes.insetter.Insetter
16+
import dev.chrisbanes.insetter.Side
1117
import dev.chrisbanes.insetter.doOnApplyWindowInsets
1218
import kotlinx.android.synthetic.main.activity_what_the_stack.*
1319

@@ -25,31 +31,42 @@ class WhatTheStackActivity : AppCompatActivity() {
2531
super.onCreate(savedInstanceState)
2632
setContentView(R.layout.activity_what_the_stack)
2733

28-
nestedScrollRoot.apply {
29-
systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
30-
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
34+
window.decorView.systemUiVisibility =
35+
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
3136

32-
doOnApplyWindowInsets { view, insets, initialState ->
33-
view.updatePadding(
34-
top = initialState.paddings.top + insets.systemWindowInsetTop
35-
)
36-
}
37-
}
37+
Insetter.builder()
38+
.applySystemWindowInsetsToPadding(Side.LEFT or Side.RIGHT or Side.TOP)
39+
.applyToView(findViewById(R.id.nestedScrollRoot))
3840

3941
val type = intent.getStringExtra(KEY_EXCEPTION_TYPE)
4042
val cause = intent.getStringExtra(KEY_EXCEPTION_CAUSE)
4143
val message = intent.getStringExtra(KEY_EXCEPTION_MESSAGE)
4244
val stackTrace = intent.getStringExtra(KEY_EXCEPTION_STACKTRACE)
4345

44-
stacktrace.text = stackTrace
45-
exceptionName.text = getString(R.string.exception_name, type)
46-
exceptionCause.text = getString(R.string.exception_cause, cause)
47-
exceptionMessage.text = getString(R.string.exception_message, message)
46+
findViewById<TextView>(R.id.stacktrace).apply {
47+
text = stackTrace
48+
setHorizontallyScrolling(true)
49+
movementMethod = ScrollingMovementMethod()
50+
}
51+
52+
findViewById<AppCompatTextView>(R.id.exceptionName).apply {
53+
text = getString(R.string.exception_name, type)
54+
}
55+
56+
findViewById<AppCompatTextView>(R.id.exceptionCause).apply {
57+
text = getString(R.string.exception_cause, cause)
58+
}
4859

49-
copyStacktrace.setOnClickListener {
50-
val clipping = ClipData.newPlainText("stacktrace", stackTrace)
51-
clipboardManager.setPrimaryClip(clipping)
52-
snackbar { R.string.copied_message }
60+
findViewById<AppCompatTextView>(R.id.exceptionMessage).apply {
61+
text = getString(R.string.exception_message, message)
62+
}
63+
64+
findViewById<MaterialButton>(R.id.copyStacktrace).apply {
65+
setOnClickListener {
66+
val clipping = ClipData.newPlainText("stacktrace", stackTrace)
67+
clipboardManager.setPrimaryClip(clipping)
68+
snackbar { R.string.copied_message }
69+
}
5370
}
5471
}
5572

what-the-stack/src/main/res/layout/activity_what_the_stack.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
android:layout_width="wrap_content"
104104
android:layout_height="wrap_content"
105105
android:fontFamily="monospace"
106-
android:scrollHorizontally="true"
107106
android:textAppearance="?attr/textAppearanceBody2"
108107
android:textColor="?attr/colorError"
109108
tools:text="@string/sample_stack_trace" />

what-the-stack/src/test/java/com/haroldadmin/whatthestack/ExceptionProcessingTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal class ExceptionProcessingTest {
3535
val outputStream = StringOutputStream()
3636
exception.printStackTrace(PrintStream(outputStream))
3737

38-
assert(outputStream.getString() == exception.stacktrace())
38+
assert(outputStream.getString() == exception.stackTraceToString())
3939
}
4040

4141
@Test
@@ -45,7 +45,7 @@ internal class ExceptionProcessingTest {
4545

4646
assert(processedData.type == exception.type())
4747
assert(processedData.message == exception.rootCause().message)
48-
assert(processedData.stacktrace == exception.rootCause().stacktrace())
48+
assert(processedData.stacktrace == exception.stackTraceToString())
4949
assert(processedData.cause == exception.rootCause().type())
5050
}
5151
}

0 commit comments

Comments
 (0)