diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 493cc5bc..dc6e5b86 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -21,7 +21,13 @@ jobs: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + - name: Set up JDK 11 + uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: '11' + - name: Grant execute permission for gradlew run: chmod +x gradlew - name: cache gradle dependencies diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 742d0f40..0b2e27d7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,7 +10,12 @@ jobs: runs-on: ubuntu-22.04 environment: deployment steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + - name: Set up JDK 11 + uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: '11' - name: Get tag id: vars run: echo ::set-output name=tag::${GITHUB_REF#refs/*/} diff --git a/.github/workflows/snapshot.yml b/.github/workflows/snapshot.yml index b65c32cd..5f5f2f73 100644 --- a/.github/workflows/snapshot.yml +++ b/.github/workflows/snapshot.yml @@ -9,7 +9,12 @@ jobs: runs-on: ubuntu-22.04 environment: deployment steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + - name: Set up JDK 11 + uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: '11' - name: Grant execute permission for gradlew run: chmod +x gradlew - name: cache gradle dependencies diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt b/core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt index 0975d1eb..80598854 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt @@ -700,6 +700,69 @@ open class Analytics protected constructor( return anonymousId() } + /** + * Subscribes to UserInfo state changes. + * + * The handler is called immediately with the current UserInfo, then again whenever + * the user's identity, traits, or referrer changes. The subscription remains active + * for the lifetime of the Analytics instance unless explicitly unsubscribed. + * + * - Parameter handler: A closure called on the main queue with updated UserInfo. + * + * - Returns: A subscription ID that can be passed to `unsubscribe(_:)` to stop + * receiving updates. If you don't need to unsubscribe, you can ignore the return value. + * + * - Note: Multiple calls create multiple independent subscriptions. + * + * ## Example + * ```kotlin + * // Subscribe for the lifetime of Analytics + * analytics.subscribeToUserInfo { userInfo -> + * print("User: ${userInfo.userId ?: userInfo.anonymousId}") + * } + * + * // Subscribe with manual cleanup + * val subscriptionId = analytics.subscribeToUserInfo { userInfo -> + * // ... handle update + * } + * // Later, when you're done... + * analytics.unsubscribe(subscriptionId) + * ``` + * + */ + suspend fun subscribeToUserInfo(handler: (UserInfo) -> Unit) = + store.subscribe( + this, + UserInfo::class, + initialState = true, + handler = handler, + queue = fileIODispatcher + ) + + /** + * Unsubscribes from state updates. + * + * Stops receiving updates for the subscription associated with the given ID. + * After calling this, the handler will no longer be invoked for state changes. + * + * - Parameter id: The subscription ID returned from a previous subscribe call. + * + * - Note: Unsubscribing an already-unsubscribed or invalid ID is a no-op. + * + * ## Example + * ```kotlin + * val id = analytics.subscribeToUserInfo { userInfo -> + * print("User changed: ${userInfo.userId ?: "anonymous"}") + * } + * + * // Later, stop listening + * analytics.unsubscribe(id) + * ``` + */ + suspend fun unsubscribe(id: Int) { + store.unsubscribe(id) + } + /** * Retrieve the version of this library in use. * - Returns: A string representing the version in "BREAKING.FEATURE.FIX" format. diff --git a/core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt b/core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt index 13926816..a0efb34c 100644 --- a/core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt +++ b/core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt @@ -22,7 +22,8 @@ data class Settings( var edgeFunction: JsonObject = emptyJsonObject, var middlewareSettings: JsonObject = emptyJsonObject, var metrics: JsonObject = emptyJsonObject, - var consentSettings: JsonObject = emptyJsonObject + var consentSettings: JsonObject = emptyJsonObject, + var autoInstrumentation: JsonObject = emptyJsonObject ) { inline fun destinationSettings( name: String,