From c239bbab9e67c7cd5eea942c379de852fbc2a290 Mon Sep 17 00:00:00 2001 From: Justin Fiedler Date: Thu, 27 Jun 2024 16:11:10 -0700 Subject: [PATCH] fix: extract ComposeUtils to share with plugins (#202) * fix: moved compose logic to ComposeUtils --- .../internal/locators/ViewTargetLocators.kt | 19 +++------------- .../android/utilities/ComposeUtils.kt | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 android/src/main/java/com/amplitude/android/utilities/ComposeUtils.kt diff --git a/android/src/main/java/com/amplitude/android/internal/locators/ViewTargetLocators.kt b/android/src/main/java/com/amplitude/android/internal/locators/ViewTargetLocators.kt index 74b96354..1fada5f1 100644 --- a/android/src/main/java/com/amplitude/android/internal/locators/ViewTargetLocators.kt +++ b/android/src/main/java/com/amplitude/android/internal/locators/ViewTargetLocators.kt @@ -1,14 +1,9 @@ package com.amplitude.android.internal.locators -import com.amplitude.android.utilities.LoadClass +import com.amplitude.android.utilities.ComposeUtils import com.amplitude.common.Logger internal object ViewTargetLocators { - private const val COMPOSE_CLASS_NAME = "androidx.compose.ui.node.Owner" - private const val SCROLLING_VIEW_CLASS_NAME = "androidx.core.view.ScrollingView" - private const val COMPOSE_GESTURE_LOCATOR_CLASS_NAME = - "com.amplitude.android.internal.locators.ComposeViewTargetLocator" - /** * A list [ViewTargetLocator]s for classic Android [View][android.view.View]s and Jetpack * Compose. @@ -19,18 +14,10 @@ internal object ViewTargetLocators { val ALL: (Logger) -> List by lazy { { logger -> mutableListOf().apply { - val loadClass = LoadClass() - val isComposeUpstreamAvailable = loadClass.isClassAvailable(COMPOSE_CLASS_NAME, logger) - val isComposeAvailable = - isComposeUpstreamAvailable && - loadClass.isClassAvailable(COMPOSE_GESTURE_LOCATOR_CLASS_NAME, logger) - val isAndroidXScrollViewAvailable = - loadClass.isClassAvailable(SCROLLING_VIEW_CLASS_NAME, logger) - - if (isComposeAvailable) { + if (ComposeUtils.isComposeAvailable()) { add(ComposeViewTargetLocator(logger)) } - add(AndroidViewTargetLocator(isAndroidXScrollViewAvailable)) + add(AndroidViewTargetLocator(ComposeUtils.isAndroidXScrollViewAvailable(logger))) } } } diff --git a/android/src/main/java/com/amplitude/android/utilities/ComposeUtils.kt b/android/src/main/java/com/amplitude/android/utilities/ComposeUtils.kt new file mode 100644 index 00000000..ec4dcf98 --- /dev/null +++ b/android/src/main/java/com/amplitude/android/utilities/ComposeUtils.kt @@ -0,0 +1,22 @@ +package com.amplitude.android.utilities + +import com.amplitude.common.Logger + +class ComposeUtils { + companion object { + private val loadClass = LoadClass() + private const val COMPOSE_CLASS_NAME = "androidx.compose.ui.node.Owner" + private const val SCROLLING_VIEW_CLASS_NAME = "androidx.core.view.ScrollingView" + private const val COMPOSE_GESTURE_LOCATOR_CLASS_NAME = + "com.amplitude.android.internal.locators.ComposeViewTargetLocator" + + fun isComposeAvailable(logger: Logger? = null): Boolean { + return loadClass.isClassAvailable(COMPOSE_CLASS_NAME, logger) && + loadClass.isClassAvailable(COMPOSE_GESTURE_LOCATOR_CLASS_NAME, logger) + } + + fun isAndroidXScrollViewAvailable(logger: Logger? = null): Boolean { + return loadClass.isClassAvailable(SCROLLING_VIEW_CLASS_NAME, logger) + } + } +}