Skip to content

Commit a582fda

Browse files
romtsndesignedbyz
andauthored
Expose new withSentryObservableEffect method overload with SentryNavigationListener (#4143)
* FEAT: Add version of withSentryObservableEffect that has better interop with fragment navigation * Changelog * pr id --------- Co-authored-by: Andy Zolyak <[email protected]>
1 parent b79b57c commit a582fda

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
### Features
1010

1111
- Create onCreate and onStart spans for all Activities ([#4025](https://github.com/getsentry/sentry-java/pull/4025))
12-
- Add split apks info to the `App` context ([#3193](https://github.com/getsentry/sentry-java/pull/3193)))
12+
- Add split apks info to the `App` context ([#3193](https://github.com/getsentry/sentry-java/pull/3193))
13+
- Expose new `withSentryObservableEffect` method overload that accepts `SentryNavigationListener` as a parameter ([#4143](https://github.com/getsentry/sentry-java/pull/4143))
14+
- This allows sharing the same `SentryNavigationListener` instance across fragments and composables to preserve the trace
1315

1416
### Fixes
1517

sentry-compose/api/android/sentry-compose.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public final class io/sentry/compose/SentryModifier {
1818
}
1919

2020
public final class io/sentry/compose/SentryNavigationIntegrationKt {
21+
public static final fun withSentryObservableEffect (Landroidx/navigation/NavHostController;Lio/sentry/android/navigation/SentryNavigationListener;Landroidx/compose/runtime/Composer;I)Landroidx/navigation/NavHostController;
2122
public static final fun withSentryObservableEffect (Landroidx/navigation/NavHostController;ZZLandroidx/compose/runtime/Composer;II)Landroidx/navigation/NavHostController;
2223
}
2324

sentry-compose/src/androidMain/kotlin/io/sentry/compose/SentryNavigationIntegration.kt

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,22 @@ internal class SentryLifecycleObserver(
4848
* A [DisposableEffect] that captures a [Breadcrumb] and starts an [ITransaction] and sends
4949
* them to Sentry for every navigation event when being attached to the respective [NavHostController].
5050
*
51-
* @param enableNavigationBreadcrumbs Whether the integration should capture breadcrumbs for
52-
* navigation events.
53-
* @param enableNavigationTracing Whether the integration should start a new [ITransaction]
54-
* with [SentryOptions.idleTimeout] for navigation events.
51+
* @param navListener An instance of a [SentryNavigationListener] that is shared with other sentry integrations, like
52+
* the fragment navigation integration.
5553
*/
5654
@Composable
5755
@NonRestartableComposable
5856
public fun NavHostController.withSentryObservableEffect(
59-
enableNavigationBreadcrumbs: Boolean = true,
60-
enableNavigationTracing: Boolean = true
57+
navListener: SentryNavigationListener
6158
): NavHostController {
62-
val enableBreadcrumbsSnapshot by rememberUpdatedState(enableNavigationBreadcrumbs)
63-
val enableTracingSnapshot by rememberUpdatedState(enableNavigationTracing)
59+
val navListenerSnapshot by rememberUpdatedState(navListener)
6460

6561
// As described in https://developer.android.com/codelabs/jetpack-compose-advanced-state-side-effects#6
6662
val lifecycle = LocalLifecycleOwner.current.lifecycle
6763
DisposableEffect(lifecycle, this) {
6864
val observer = SentryLifecycleObserver(
6965
this@withSentryObservableEffect,
70-
navListener = SentryNavigationListener(
71-
enableNavigationBreadcrumbs = enableBreadcrumbsSnapshot,
72-
enableNavigationTracing = enableTracingSnapshot,
73-
traceOriginAppendix = TRACE_ORIGIN_APPENDIX
74-
)
66+
navListener = navListenerSnapshot
7567
)
7668

7769
lifecycle.addObserver(observer)
@@ -84,6 +76,34 @@ public fun NavHostController.withSentryObservableEffect(
8476
return this
8577
}
8678

79+
/**
80+
* A [DisposableEffect] that captures a [Breadcrumb] and starts an [ITransaction] and sends
81+
* them to Sentry for every navigation event when being attached to the respective [NavHostController].
82+
* This version of withSentryObservableEffect should be used if you are working purely with Compose.
83+
*
84+
* @param enableNavigationBreadcrumbs Whether the integration should capture breadcrumbs for
85+
* navigation events.
86+
* @param enableNavigationTracing Whether the integration should start a new [ITransaction]
87+
* with [SentryOptions.idleTimeout] for navigation events.
88+
*/
89+
@Composable
90+
@NonRestartableComposable
91+
public fun NavHostController.withSentryObservableEffect(
92+
enableNavigationBreadcrumbs: Boolean = true,
93+
enableNavigationTracing: Boolean = true
94+
): NavHostController {
95+
val enableBreadcrumbsSnapshot by rememberUpdatedState(enableNavigationBreadcrumbs)
96+
val enableTracingSnapshot by rememberUpdatedState(enableNavigationTracing)
97+
98+
return withSentryObservableEffect(
99+
navListener = SentryNavigationListener(
100+
enableNavigationBreadcrumbs = enableBreadcrumbsSnapshot,
101+
enableNavigationTracing = enableTracingSnapshot,
102+
traceOriginAppendix = TRACE_ORIGIN_APPENDIX
103+
)
104+
)
105+
}
106+
87107
/**
88108
* A [DisposableEffect] that captures a [Breadcrumb] and starts an [ITransaction] and sends
89109
* them to Sentry for every navigation event when being attached to the respective [NavHostController].

0 commit comments

Comments
 (0)