@@ -6,13 +6,24 @@ import androidx.compose.runtime.mutableStateListOf
66import androidx.compose.runtime.saveable.listSaver
77import androidx.compose.runtime.saveable.rememberSaveable
88import androidx.compose.runtime.snapshots.SnapshotStateList
9+ import io.sentry.compose.navigation3.BackStackEntryMapper
910import io.sentry.samples.android.navigation.common.NavArgs
1011import io.sentry.samples.android.navigation.common.RouteNames
1112import io.sentry.samples.android.navigation.common.RouteSpec
1213import io.sentry.samples.android.navigation.common.RouteSpecs
1314import io.sentry.samples.android.navigation.common.displayRoute
15+ import kotlinx.serialization.EncodeDefault
16+ import kotlinx.serialization.ExperimentalSerializationApi
17+ import kotlinx.serialization.SerialName
18+ import kotlinx.serialization.Serializable
1419
15- internal fun Nav3Route.displayRoute (): String = routeSpec().displayRoute(arguments)
20+ internal fun Nav3Route.displayRoute (): String =
21+ when (this ) {
22+ // Built without the back stack entry mappers, whose cost the performance scenario measures
23+ // only through SentryNavEffect.
24+ is Nav3Route .Performance -> " /$previewName "
25+ else -> routeSpec().displayRoute(arguments)
26+ }
1627
1728internal fun Nav3Route.routeSpec (): RouteSpec =
1829 when (this ) {
@@ -228,98 +239,115 @@ internal fun SnapshotStateList<Nav3Route>.replacePerformanceTop(generation: Int)
228239 set(lastIndex, Nav3Route .Performance (index = lastIndex, generation = generation))
229240}
230241
242+ /* *
243+ * The sample's back stack keys. Each is `@Serializable`, so
244+ * `BackStackEntryMapper.SerialNameAndProperties` can map it: the route's `@SerialName` becomes its
245+ * Sentry name, and each property's `@SerialName` an argument name. Both match the Nav2 sample's
246+ * [RouteNames] and [NavArgs], so the two samples send the same data. Every other member is a
247+ * getter, so it isn't recorded as an argument.
248+ */
231249internal sealed interface Nav3Route {
250+ /* *
251+ * The route's name as Sentry records it, without the leading `/`. Read from the same mapper
252+ * `SentryNavEffect` uses, so screens, tags, and transaction checks can't drift from it.
253+ */
232254 val routeName: String
255+ get() =
256+ checkNotNull(BackStackEntryMapper .SerialName .map(this )) {
257+ " ${this ::class .simpleName} can't be named by BackStackEntryMapper.SerialName."
258+ }
259+ .name
260+
261+ /* * The route's arguments as Sentry records them, read from the same mapper as [routeName]. */
233262 val arguments: Map <String , Any ?>
234- get() = emptyMap ()
263+ get() = BackStackEntryMapper . SerialNameAndProperties .map( this )?.arguments.orEmpty ()
235264
236265 val previewName: String
237266 get() = routeName
238267
239268 val performanceSeed: Int
240269 get() = hashCode()
241270
242- data object Landing : Nav3Route {
243- override val routeName: String = RouteNames .LANDING
244- }
271+ @Serializable @SerialName(RouteNames .LANDING ) data object Landing : Nav3Route
245272
246- data object SingleStack : Nav3Route {
247- override val routeName: String = RouteNames .HOME
248- }
273+ @Serializable @SerialName(RouteNames .HOME ) data object SingleStack : Nav3Route
249274
250- data object Custom : Nav3Route {
251- override val routeName: String = " Custom"
252- }
275+ @Serializable @SerialName(RouteNames .CUSTOM ) data object Custom : Nav3Route
253276
254- data object MultiActivity : Nav3Route {
255- override val routeName: String = " MultiActivity"
256- }
277+ @Serializable @SerialName(" MultiActivity" ) data object MultiActivity : Nav3Route
257278
258- data object MultiFragment : Nav3Route {
259- override val routeName: String = " MultiFragment"
260- }
279+ @Serializable @SerialName(" MultiFragment" ) data object MultiFragment : Nav3Route
261280
262- data object FragmentOverlay : Nav3Route {
263- override val routeName: String = " FragmentOverlay"
264- }
281+ @Serializable @SerialName(" FragmentOverlay" ) data object FragmentOverlay : Nav3Route
265282
266- data object ViewPager2 : Nav3Route {
267- override val routeName: String = " ViewPager2"
268- }
283+ @Serializable @SerialName(" ViewPager2" ) data object ViewPager2 : Nav3Route
269284
270- data object DeepLink : Nav3Route {
271- override val routeName: String = RouteNames .DEEP_LINK
272- }
285+ @Serializable @SerialName(RouteNames .DEEP_LINK ) data object DeepLink : Nav3Route
273286
274- data object ProductList : Nav3Route {
275- override val routeName: String = RouteNames .PRODUCT_LIST
276- }
287+ @Serializable @SerialName(RouteNames .PRODUCT_LIST ) data object ProductList : Nav3Route
277288
289+ @OptIn(ExperimentalSerializationApi ::class )
290+ @Serializable
291+ @SerialName(RouteNames .PRODUCT_DETAIL )
278292 data class ProductDetail (
279- val productId : String ,
280- val source : String ,
293+ @SerialName(NavArgs .PRODUCT_ID ) val productId : String ,
294+ @SerialName(NavArgs .SOURCE ) val source : String ,
295+ // Omitted from the arguments when null, like the Nav2 sample.
296+ @EncodeDefault(EncodeDefault .Mode .NEVER )
297+ @SerialName(NavArgs .CAMPAIGN )
281298 val campaign : String? = null ,
282299 ) : Nav3Route {
283- override val routeName: String = RouteNames .PRODUCT_DETAIL
284- override val arguments: Map <String , Any ?> =
285- mapOf (
286- NavArgs .PRODUCT_ID to productId,
287- NavArgs .SOURCE to source,
288- NavArgs .CAMPAIGN to campaign,
289- )
290- .filterValues { it != null }
291- override val previewName: String = " ProductDetail($productId )"
300+ override val previewName: String
301+ get() = " ProductDetail($productId )"
292302 }
293303
294- data class Checkout (val productId : String ) : Nav3Route {
295- override val routeName: String = RouteNames .CHECKOUT
296- override val arguments: Map <String , Any ?> = mapOf (NavArgs .PRODUCT_ID to productId)
297- override val previewName: String = " Checkout($productId )"
304+ @Serializable
305+ @SerialName(RouteNames .CHECKOUT )
306+ data class Checkout (@SerialName(NavArgs .PRODUCT_ID ) val productId : String ) : Nav3Route {
307+ override val previewName: String
308+ get() = " Checkout($productId )"
298309 }
299310
300- data class Confirmation (val orderId : String ) : Nav3Route {
301- override val routeName: String = RouteNames .CONFIRMATION
302- override val arguments: Map <String , Any ?> = mapOf (NavArgs .ORDER_ID to orderId)
303- override val previewName: String = " Confirmation($orderId )"
311+ @Serializable
312+ @SerialName(RouteNames .CONFIRMATION )
313+ data class Confirmation (@SerialName(NavArgs .ORDER_ID ) val orderId : String ) : Nav3Route {
314+ override val previewName: String
315+ get() = " Confirmation($orderId )"
304316 }
305317
306- data class PromoDialog (val promoId : String ) : Nav3Route {
307- override val routeName: String = RouteNames .PROMO_DIALOG
308- override val arguments: Map <String , Any ?> = mapOf (NavArgs .PROMO_ID to promoId)
309- override val previewName: String = " PromoDialog($promoId )"
318+ @Serializable
319+ @SerialName(RouteNames .PROMO_DIALOG )
320+ data class PromoDialog (@SerialName(NavArgs .PROMO_ID ) val promoId : String ) : Nav3Route {
321+ override val previewName: String
322+ get() = " PromoDialog($promoId )"
310323 }
311324
312- data class ShareSheet (val productId : String ) : Nav3Route {
313- override val routeName: String = RouteNames .SHARE_SHEET
314- override val arguments: Map <String , Any ?> = mapOf (NavArgs .PRODUCT_ID to productId)
315- override val previewName: String = " ShareSheet($productId )"
325+ @Serializable
326+ @SerialName(RouteNames .SHARE_SHEET )
327+ data class ShareSheet (@SerialName(NavArgs .PRODUCT_ID ) val productId : String ) : Nav3Route {
328+ override val previewName: String
329+ get() = " ShareSheet($productId )"
316330 }
317331
332+ @Serializable
333+ @SerialName(Performance .NAME )
318334 data class Performance (val index : Int , val generation : Int ) : Nav3Route {
319- override val routeName: String = " Performance"
320- override val arguments: Map <String , Any ?> = mapOf (" index" to index, " generation" to generation)
321- override val previewName: String = " Performance($index :$generation )"
322- override val performanceSeed: Int = 31 * index + generation
335+ /* *
336+ * A constant rather than the mapper's result, so screens and tags don't run the mapper outside
337+ * the performance scenario's timed extraction, e.g. in its disabled baseline.
338+ */
339+ override val routeName: String
340+ get() = NAME
341+
342+ override val previewName: String
343+ get() = " Performance($index :$generation )"
344+
345+ override val performanceSeed: Int
346+ get() = 31 * index + generation
347+
348+ companion object {
349+ const val NAME = " Performance"
350+ }
323351 }
324352}
325353
0 commit comments