Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Add version of withSentryObservableEffect that has better inter… #3411

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,23 @@ internal class SentryLifecycleObserver(
* A [DisposableEffect] that captures a [Breadcrumb] and starts an [ITransaction] and sends
* them to Sentry for every navigation event when being attached to the respective [NavHostController].
*
* @param enableNavigationBreadcrumbs Whether the integration should capture breadcrumbs for
* navigation events.
* @param enableNavigationTracing Whether the integration should start a new [ITransaction]
* with [SentryOptions.idleTimeout] for navigation events.
* @param navListener An instance of a [SentryNavigationListener] that is shared with other sentry integrations, like
* the fragment navigation integration.
*/
@Composable
@NonRestartableComposable
public fun NavHostController.withSentryObservableEffect(
enableNavigationBreadcrumbs: Boolean = true,
enableNavigationTracing: Boolean = true
fun NavHostController.withSentryObservableEffect(
navListener: SentryNavigationListener
): NavHostController {
val enableBreadcrumbsSnapshot by rememberUpdatedState(enableNavigationBreadcrumbs)
val enableTracingSnapshot by rememberUpdatedState(enableNavigationTracing)

val navListenerSnapshot by rememberUpdatedState(navListener)

// As described in https://developer.android.com/codelabs/jetpack-compose-advanced-state-side-effects#6
val lifecycle = LocalLifecycleOwner.current.lifecycle
DisposableEffect(lifecycle, this) {
val observer = SentryLifecycleObserver(
this@withSentryObservableEffect,
navListener = SentryNavigationListener(
enableNavigationBreadcrumbs = enableBreadcrumbsSnapshot,
enableNavigationTracing = enableTracingSnapshot,
traceOriginAppendix = TRACE_ORIGIN_APPENDIX
)
navListener = navListenerSnapshot
)

lifecycle.addObserver(observer)
Expand All @@ -84,6 +77,32 @@ public fun NavHostController.withSentryObservableEffect(
return this
}

/**
* A [DisposableEffect] that captures a [Breadcrumb] and starts an [ITransaction] and sends
* them to Sentry for every navigation event when being attached to the respective [NavHostController].
* This version of withSentryObservableEffect should be used if you are working purely with Compose.
*
* @param enableNavigationBreadcrumbs Whether the integration should capture breadcrumbs for
* navigation events.
* @param enableNavigationTracing Whether the integration should start a new [ITransaction]
* with [SentryOptions.idleTimeout] for navigation events.
*/
@Composable
@NonRestartableComposable
fun NavHostController.withSentryObservableEffect(
enableNavigationBreadcrumbs: Boolean = true,
enableNavigationTracing: Boolean = true
): NavHostController {
val enableBreadcrumbsSnapshot by rememberUpdatedState(enableNavigationBreadcrumbs)
val enableTracingSnapshot by rememberUpdatedState(enableNavigationTracing)

return withSentryObservableEffect(navListener = SentryNavigationListener(
enableNavigationBreadcrumbs = enableBreadcrumbsSnapshot,
enableNavigationTracing = enableTracingSnapshot,
traceOriginAppendix = TRACE_ORIGIN_APPENDIX
))
}

/**
* A [DisposableEffect] that captures a [Breadcrumb] and starts an [ITransaction] and sends
* them to Sentry for every navigation event when being attached to the respective [NavHostController].
Expand Down
Loading