-
-
Notifications
You must be signed in to change notification settings - Fork 475
feat: Add compose module (navigation) #2121
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9ab0299
feat: androidx-compose support for navigation
romtsn 9660cd1
Merge branch 'feat/androidx-navigation' into feat/androidx-compose
romtsn 048df40
api dump
romtsn 6a3c581
Merge branch 'feat/androidx-navigation' into feat/androidx-compose
romtsn 28e2606
Address PR reviews
romtsn 721c35c
Merge branch 'feat/androidx-navigation' into feat/androidx-compose
romtsn 828fbcf
Fix test
romtsn 6815f62
Rename extension method to withSentryObservableEffect
romtsn dc1e5fa
api dump
romtsn 17f80f5
feat: Compose sample (#2122)
romtsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| public final class io/sentry/compose/BuildConfig { | ||
| public static final field BUILD_TYPE Ljava/lang/String; | ||
| public static final field DEBUG Z | ||
| public static final field LIBRARY_PACKAGE_NAME Ljava/lang/String; | ||
| public static final field VERSION_NAME Ljava/lang/String; | ||
| public fun <init> ()V | ||
| } | ||
|
|
||
| public final class io/sentry/compose/SentryNavigationIntegrationKt { | ||
| public static final fun withSentryObservableEffect (Landroidx/navigation/NavHostController;Landroidx/compose/runtime/Composer;I)Landroidx/navigation/NavHostController; | ||
| } | ||
|
|
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import io.gitlab.arturbosch.detekt.Detekt | ||
| import io.gitlab.arturbosch.detekt.extensions.DetektExtension | ||
|
|
||
| plugins { | ||
| kotlin("multiplatform") | ||
| id("com.android.library") | ||
| id("org.jetbrains.compose") | ||
| jacoco | ||
| id(Config.QualityPlugins.gradleVersions) | ||
| id(Config.QualityPlugins.detektPlugin) | ||
| `maven-publish` // necessary for publishMavenLocal task to publish correct artifacts | ||
| } | ||
|
|
||
| kotlin { | ||
| explicitApi() | ||
|
|
||
| android { | ||
| publishLibraryVariants("release") | ||
| } | ||
| jvm("desktop") { | ||
| compilations.all { | ||
| kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
| } | ||
| } | ||
|
|
||
| sourceSets.all { | ||
| // Allow all experimental APIs, since MPP projects are themselves experimental | ||
| languageSettings.apply { | ||
| optIn("kotlin.Experimental") | ||
| optIn("kotlin.ExperimentalMultiplatform") | ||
| } | ||
| } | ||
|
|
||
| sourceSets { | ||
| val commonMain by getting { | ||
| dependencies { | ||
| api(compose.runtime) | ||
| api(compose.ui) | ||
|
|
||
| implementation(Config.Libs.kotlinStdLib) | ||
| } | ||
| } | ||
| val androidMain by getting { | ||
| dependencies { | ||
| api(projects.sentry) | ||
| api(projects.sentryAndroidNavigation) | ||
|
|
||
| api(Config.Libs.composeNavigation) | ||
| implementation(Config.Libs.lifecycleCommonJava8) | ||
| } | ||
| } | ||
| val androidTest by getting { | ||
| dependencies { | ||
| implementation(Config.TestLibs.kotlinTestJunit) | ||
| implementation(Config.TestLibs.mockitoKotlin) | ||
| implementation(Config.TestLibs.mockitoInline) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| android { | ||
| compileSdk = Config.Android.compileSdkVersion | ||
|
|
||
| defaultConfig { | ||
| targetSdk = Config.Android.targetSdkVersion | ||
| minSdk = Config.Android.minSdkVersionCompose | ||
|
|
||
| // for AGP 4.1 | ||
| buildConfigField("String", "VERSION_NAME", "\"${project.version}\"") | ||
| } | ||
|
|
||
| sourceSets["main"].apply { | ||
| manifest.srcFile("src/androidMain/AndroidManifest.xml") | ||
| } | ||
|
|
||
| buildTypes { | ||
| getByName("debug") | ||
| getByName("release") { | ||
| consumerProguardFiles("proguard-rules.pro") | ||
| } | ||
| } | ||
|
|
||
| testOptions { | ||
| animationsDisabled = true | ||
| unitTests.apply { | ||
| isReturnDefaultValues = true | ||
| isIncludeAndroidResources = true | ||
| } | ||
| } | ||
|
|
||
| lint { | ||
| warningsAsErrors = true | ||
| checkDependencies = true | ||
|
|
||
| // We run a full lint analysis as build part in CI, so skip vital checks for assemble tasks. | ||
| checkReleaseBuilds = false | ||
| } | ||
|
|
||
| variantFilter { | ||
| if (Config.Android.shouldSkipDebugVariant(buildType.name)) { | ||
| ignore = true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| tasks.withType<Test> { | ||
| configure<JacocoTaskExtension> { | ||
| isIncludeNoLocationClasses = false | ||
| } | ||
| } | ||
|
|
||
| tasks.withType<Detekt> { | ||
| // Target version of the generated JVM bytecode. It is used for type resolution. | ||
| jvmTarget = JavaVersion.VERSION_1_8.toString() | ||
| } | ||
|
|
||
| configure<DetektExtension> { | ||
| buildUponDefaultConfig = true | ||
| allRules = true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| kotlin.mpp.stability.nowarn=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| ##---------------Begin: proguard configuration for Compose ---------- | ||
|
|
||
| # To ensure that stack traces is unambiguous | ||
| # https://developer.android.com/studio/build/shrink-code#decode-stack-trace | ||
| -keepattributes LineNumberTable,SourceFile | ||
|
|
||
| ##---------------End: proguard configuration for Compose ---------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest package="io.sentry.compose"/> |
52 changes: 52 additions & 0 deletions
52
sentry-compose/src/androidMain/kotlin/io/sentry/compose/SentryNavigationIntegration.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package io.sentry.compose | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.DisposableEffect | ||
| import androidx.compose.runtime.NonRestartableComposable | ||
| import androidx.compose.ui.platform.LocalLifecycleOwner | ||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.lifecycle.LifecycleEventObserver | ||
| import androidx.lifecycle.LifecycleOwner | ||
| import androidx.navigation.NavController | ||
| import androidx.navigation.NavHostController | ||
| import io.sentry.HubAdapter | ||
| import io.sentry.IHub | ||
| import io.sentry.android.navigation.SentryNavigationListener | ||
|
|
||
| internal class SentryLifecycleObserver( | ||
| private val navController: NavController, | ||
| private val hub: IHub = HubAdapter.getInstance(), | ||
| private val navListener: NavController.OnDestinationChangedListener = | ||
| SentryNavigationListener(hub) | ||
|
marandaneto marked this conversation as resolved.
|
||
| ) : LifecycleEventObserver { | ||
|
|
||
| override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { | ||
| if (event == Lifecycle.Event.ON_RESUME) { | ||
| navController.addOnDestinationChangedListener(navListener) | ||
| } else if (event == Lifecycle.Event.ON_PAUSE) { | ||
| navController.removeOnDestinationChangedListener(navListener) | ||
| } | ||
| } | ||
|
|
||
| fun dispose() { | ||
| navController.removeOnDestinationChangedListener(navListener) | ||
| } | ||
| } | ||
|
|
||
| // As described in https://developer.android.com/codelabs/jetpack-compose-advanced-state-side-effects#6 | ||
| @Composable | ||
| @NonRestartableComposable | ||
| public fun NavHostController.withSentryObservableEffect(): NavHostController { | ||
| val lifecycle = LocalLifecycleOwner.current.lifecycle | ||
| DisposableEffect(lifecycle, this) { | ||
| val observer = SentryLifecycleObserver(this@withSentryObservableEffect) | ||
|
|
||
| lifecycle.addObserver(observer) | ||
|
|
||
| onDispose { | ||
| observer.dispose() | ||
| lifecycle.removeObserver(observer) | ||
| } | ||
| } | ||
| return this | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
sentry-compose/src/androidTest/kotlin/io/sentry/compose/SentryLifecycleObserverTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package io.sentry.compose | ||
|
|
||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.navigation.NavController | ||
| import com.nhaarman.mockitokotlin2.mock | ||
| import com.nhaarman.mockitokotlin2.verify | ||
| import io.sentry.IHub | ||
| import io.sentry.android.navigation.SentryNavigationListener | ||
| import kotlin.test.Test | ||
|
|
||
| internal class SentryLifecycleObserverTest { | ||
|
|
||
| class Fixture { | ||
| val navListener = mock<SentryNavigationListener>() | ||
| val hub = mock<IHub>() | ||
| val navController = mock<NavController>() | ||
|
|
||
| fun getSut(): SentryLifecycleObserver { | ||
| return SentryLifecycleObserver(navController, hub, navListener) | ||
| } | ||
| } | ||
|
|
||
| private val fixture = Fixture() | ||
|
|
||
| @Test | ||
| fun `onResume adds navigation listener`() { | ||
| val sut = fixture.getSut() | ||
|
|
||
| sut.onStateChanged(mock(), Lifecycle.Event.ON_RESUME) | ||
|
|
||
| verify(fixture.navController).addOnDestinationChangedListener(fixture.navListener) | ||
| } | ||
|
|
||
| @Test | ||
| fun `onPause removes navigation listener`() { | ||
| val sut = fixture.getSut() | ||
|
|
||
| sut.onStateChanged(mock(), Lifecycle.Event.ON_PAUSE) | ||
|
|
||
| verify(fixture.navController).removeOnDestinationChangedListener(fixture.navListener) | ||
| } | ||
|
|
||
| @Test | ||
| fun `dispose removes navigation listener`() { | ||
| val sut = fixture.getSut() | ||
|
|
||
| sut.dispose() | ||
|
|
||
| verify(fixture.navController).removeOnDestinationChangedListener(fixture.navListener) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.