Skip to content

Commit

Permalink
PLATIR-45069: Add theme overrides for AEP Presentables
Browse files Browse the repository at this point in the history
  • Loading branch information
prudrabhat committed Jan 21, 2025
1 parent 66316d1 commit 8f0b5c6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
10 changes: 10 additions & 0 deletions code/core/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>

<resources>
<!-- This theme exists to override specific attributes from the base application theme to make
AEP UI Service Presentables look as desired. This theme intentionally does not have a parent
and should be used in conjunction with applyStyle() on the base application theme. -->
<style name="AepSdkUiService.OverrideTheme" parent="">
<item name="android:background">@android:color/transparent</item>
</style>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal class AlertPresentable(
mainScope
) {
override fun getContent(activityContext: Context): ComposeView {
return ComposeView(activityContext).apply {
return ComposeView(getThemedContext(activityContext)).apply {

Check warning on line 47 in code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/alert/AlertPresentable.kt

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/alert/AlertPresentable.kt#L47

Added line #L47 was not covered by tests
setContent {
AlertScreen(
presentationStateManager = presentationStateManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ package com.adobe.marketing.mobile.services.ui.common

import android.app.Activity
import android.content.Context
import android.view.ContextThemeWrapper
import android.view.View
import android.view.ViewGroup
import androidx.annotation.MainThread
import androidx.annotation.VisibleForTesting
import androidx.compose.ui.platform.ComposeView
import com.adobe.marketing.mobile.core.R
import com.adobe.marketing.mobile.internal.util.ActivityCompatOwnerUtils
import com.adobe.marketing.mobile.services.Log
import com.adobe.marketing.mobile.services.ServiceConstants
Expand Down Expand Up @@ -291,6 +293,37 @@ internal abstract class AEPPresentable<T : Presentation<T>> :
onAnimationComplete()
}

/**
* Returns a themed context that applies overrides to the base context theme
* to ensure that the presentable UI is consistent with the rest of the application while
* ensuring that specific attributes like view background are not inherited from the base theme.
*/
protected fun getThemedContext(context: Context): Context {
// Theme overrides are supported only for API level 23 and above. For lower API levels,
// return the base context as is.
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
return context

Check warning on line 305 in code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/common/AEPPresentable.kt

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/common/AEPPresentable.kt#L305

Added line #L305 was not covered by tests
}

try {
val newTheme = context.resources.newTheme()

Check warning on line 309 in code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/common/AEPPresentable.kt

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/common/AEPPresentable.kt#L308-L309

Added lines #L308 - L309 were not covered by tests
// Apply the base theme attributes to the new theme
newTheme.setTo(context.theme)
val themedContext = ContextThemeWrapper(context, newTheme)

Check warning on line 312 in code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/common/AEPPresentable.kt

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/common/AEPPresentable.kt#L311-L312

Added lines #L311 - L312 were not covered by tests
// Apply the override theme to the themed context
themedContext.theme.applyStyle(R.style.AepSdkUiService_OverrideTheme, true)
return themedContext
} catch (e: Exception) {
Log.error(
ServiceConstants.LOG_TAG,
LOG_SOURCE,
"Error while creating themed context",
e

Check warning on line 321 in code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/common/AEPPresentable.kt

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/common/AEPPresentable.kt#L314-L321

Added lines #L314 - L321 were not covered by tests
)
}
return context

Check warning on line 324 in code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/common/AEPPresentable.kt

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/common/AEPPresentable.kt#L324

Added line #L324 was not covered by tests
}

/**
* Fetches the [ComposeView] associated with the presentable. This ComposeView is used to
* render the UI of the presentable by attaching it to the content view of the activity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal class FloatingButtonPresentable(
}

override fun getContent(activityContext: Context): ComposeView {
return ComposeView(activityContext).apply {
return ComposeView(getThemedContext(activityContext)).apply {

Check warning on line 65 in code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/floatingbutton/FloatingButtonPresentable.kt

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/floatingbutton/FloatingButtonPresentable.kt#L65

Added line #L65 was not covered by tests
setContent {
FloatingButtonScreen(
presentationStateManager = presentationStateManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ internal fun FloatingButton(
modifier = Modifier
.height(heightDp.value)
.width(widthDp.value)
.background(Color.Transparent)

Check warning on line 93 in code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/floatingbutton/views/FloatingButton.kt

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/floatingbutton/views/FloatingButton.kt#L93

Added line #L93 was not covered by tests
.testTag(FloatingButtonTestTags.FLOATING_BUTTON_AREA)
) {
FloatingActionButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal class InAppMessagePresentable(
* @param activityContext the context of the activity
*/
override fun getContent(activityContext: Context): ComposeView {
return ComposeView(activityContext).apply {
return ComposeView(getThemedContext(activityContext)).apply {

Check warning on line 80 in code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/InAppMessagePresentable.kt

View check run for this annotation

Codecov / codecov/patch

code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/InAppMessagePresentable.kt#L80

Added line #L80 was not covered by tests
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
Expand Down

0 comments on commit 8f0b5c6

Please sign in to comment.