Skip to content

Commit

Permalink
Update MainComposeActivity.kt
Browse files Browse the repository at this point in the history
MainComposeActivity now has crashlytics functionality. Can now crash when pressing the button and catch it
  • Loading branch information
millandavid committed May 26, 2023
1 parent 4809ecd commit 3a8569c
Showing 1 changed file with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,66 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.crashlytics.ktx.setCustomKeys
import com.google.firebase.ktx.Firebase
import com.google.samples.quickstart.crash.R
import com.google.samples.quickstart.crash.kotlin.ui.theme.CrashTheme
import kotlinx.coroutines.launch

class MainComposeActivity : ComponentActivity() {

private lateinit var crashlytics: FirebaseCrashlytics
private lateinit var customKeySamples: CustomKeySamples
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Taken from original MainActivity.kt
customKeySamples = CustomKeySamples(applicationContext)
customKeySamples.setSampleCustomKeys()
customKeySamples.updateAndTrackNetworkState()

crashlytics = Firebase.crashlytics

// Log the onCreate event, this will also be printed in logcat
crashlytics.log("onCreate with jetpack compose")

// Add some custom values and identifiers to be included in crash reports
crashlytics.setCustomKeys {
key("MeaningOfLife", 42)
key("LastUIAction", "Test value")
}
crashlytics.setUserId("123456789")

// Report a non-fatal exception, for demonstration purposes
crashlytics.recordException(Exception("Non-fatal exception: something went wrong!"))

setContent {
CrashTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
MainAppView()
MainAppView(crashlytics)
}
}
}
}
}

@Composable
fun MainAppView() {
fun MainAppView(crashlytics: FirebaseCrashlytics) {

val scaffoldState = rememberScaffoldState() // this contains the `SnackbarHostState`
val checkedState = remember { mutableStateOf(true) }

// Log that the Activity was created.
// [START crashlytics_log_event]
crashlytics.log("Activity created")
// [END crashlytics_log_event]

Scaffold(
scaffoldState = scaffoldState,
Expand Down Expand Up @@ -90,6 +123,24 @@ fun MainAppView() {
colors = ButtonDefaults.buttonColors(backgroundColor = colorResource(R.color.colorAccent)),
onClick = {

// crashlytics Functionality
crashlytics.log("Crash button clicked using jetpack compose.")
println("CRASHHHH")

if (checkedState.value) {
try {
throw NullPointerException()
} catch (ex: NullPointerException) {
// [START crashlytics_log_and_report]
crashlytics.log("NPE caught!")
crashlytics.recordException(ex)
// [END crashlytics_log_and_report]
}
} else {
throw NullPointerException()
}


}
) {
Text(
Expand All @@ -109,4 +160,3 @@ fun MainAppView() {
}
})
}

0 comments on commit 3a8569c

Please sign in to comment.