Skip to content

Commit 47259f2

Browse files
authored
Merge pull request #9 from haroldadmin/develop
Improve Exception screen UI
2 parents aa47df0 + 1ec5155 commit 47259f2

File tree

6 files changed

+38
-38
lines changed

6 files changed

+38
-38
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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.haroldadmin.whatthestack
22

33
import android.os.Parcelable
4-
import java.io.PrintStream
54
import kotlinx.android.parcel.Parcelize
65

76
/**
@@ -29,7 +28,7 @@ internal fun Throwable.process(): ExceptionData {
2928
val rootCauseOfException = rootCause()
3029
val cause = rootCauseOfException.type()
3130
val message = rootCauseOfException.message ?: "Unknown"
32-
val stacktrace = rootCauseOfException.stacktrace()
31+
val stacktrace = this.stackTraceToString()
3332
return ExceptionData(type, cause, message, stacktrace)
3433
}
3534

@@ -53,12 +52,3 @@ internal tailrec fun Throwable.rootCause(): Throwable {
5352
internal fun Throwable.type(): String {
5453
return this::class.java.simpleName
5554
}
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 & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ 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
9-
import androidx.core.view.updatePadding
11+
import androidx.appcompat.widget.AppCompatTextView
12+
import com.google.android.material.button.MaterialButton
1013
import com.google.android.material.snackbar.Snackbar
11-
import dev.chrisbanes.insetter.doOnApplyWindowInsets
14+
import dev.chrisbanes.insetter.Insetter
15+
import dev.chrisbanes.insetter.Side
1216
import kotlinx.android.synthetic.main.activity_what_the_stack.*
1317

1418
/**
@@ -25,31 +29,42 @@ class WhatTheStackActivity : AppCompatActivity() {
2529
super.onCreate(savedInstanceState)
2630
setContentView(R.layout.activity_what_the_stack)
2731

28-
nestedScrollRoot.apply {
29-
systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
30-
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
32+
window.decorView.systemUiVisibility =
33+
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
3134

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

3939
val type = intent.getStringExtra(KEY_EXCEPTION_TYPE)
4040
val cause = intent.getStringExtra(KEY_EXCEPTION_CAUSE)
4141
val message = intent.getStringExtra(KEY_EXCEPTION_MESSAGE)
4242
val stackTrace = intent.getStringExtra(KEY_EXCEPTION_STACKTRACE)
4343

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)
44+
findViewById<TextView>(R.id.stacktrace).apply {
45+
text = stackTrace
46+
setHorizontallyScrolling(true)
47+
movementMethod = ScrollingMovementMethod()
48+
}
49+
50+
findViewById<AppCompatTextView>(R.id.exceptionName).apply {
51+
text = getString(R.string.exception_name, type)
52+
}
53+
54+
findViewById<AppCompatTextView>(R.id.exceptionCause).apply {
55+
text = getString(R.string.exception_cause, cause)
56+
}
4857

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

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)