-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix in-app messages overlay background color being ignored from …
…message payload (#485)
- Loading branch information
1 parent
37d3dde
commit dba96e0
Showing
10 changed files
with
235 additions
and
124 deletions.
There are no files selected for viewing
This file contains 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 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 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
31 changes: 31 additions & 0 deletions
31
...napp/src/main/java/io/customer/messaginginapp/gist/utilities/MessageOverlayColorParser.kt
This file contains 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,31 @@ | ||
package io.customer.messaginginapp.gist.utilities | ||
|
||
internal object MessageOverlayColorParser { | ||
|
||
/** | ||
* The expected color is formatted as #RRGGBBAA with alpha channel at the end, we need | ||
* to reformat it to be #AARRGGBB to be usable on Android | ||
*/ | ||
fun parseColor(color: String?): String? { | ||
if (color == null) { | ||
return null | ||
} | ||
|
||
val cleanColor = color.removePrefix("#") | ||
|
||
if (doesNotHaveExpectedColorCharCount(cleanColor)) { | ||
return null | ||
} | ||
|
||
val red = cleanColor.substring(0, 2) | ||
val green = cleanColor.substring(2, 4) | ||
val blue = cleanColor.substring(4, 6) | ||
val alpha = if (cleanColor.length == 8) cleanColor.substring(6, 8) else "" | ||
|
||
return "#$alpha$red$green$blue" | ||
} | ||
|
||
private fun doesNotHaveExpectedColorCharCount(color: String): Boolean { | ||
return color.length != 6 && color.length != 8 | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
messaginginapp/src/main/java/io/customer/messaginginapp/gist/utilities/ModalAnimationUtil.kt
This file contains 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,119 @@ | ||
package io.customer.messaginginapp.gist.utilities | ||
|
||
import android.animation.AnimatorSet | ||
import android.animation.ObjectAnimator | ||
import android.graphics.Color | ||
import android.graphics.drawable.ColorDrawable | ||
import android.view.View | ||
import androidx.annotation.ColorInt | ||
import io.customer.sdk.core.di.SDKComponent | ||
|
||
internal object ModalAnimationUtil { | ||
|
||
const val FALLBACK_COLOR_STRING = "#33000000" | ||
|
||
private const val TRANSLATION_ANIMATION_DURATION = 150L | ||
private const val ALPHA_ANIMATION_DURATION = 150L | ||
private const val COLOR_ANIMATION_DURATION = 100L | ||
|
||
private val logger = SDKComponent.logger | ||
|
||
fun createAnimationSetInFromTop(target: View, overlayEndColor: String): AnimatorSet { | ||
return createEnterAnimation(target, overlayEndColor, -100f) | ||
} | ||
|
||
fun createAnimationSetInFromBottom(target: View, overlayEndColor: String): AnimatorSet { | ||
return createEnterAnimation(target, overlayEndColor, 100f) | ||
} | ||
|
||
fun createAnimationSetOutToTop(target: View): AnimatorSet { | ||
return createExitAnimation(target, -100f) | ||
} | ||
|
||
fun createAnimationSetOutToBottom(target: View): AnimatorSet { | ||
return createExitAnimation(target, 100f) | ||
} | ||
|
||
private fun createEnterAnimation( | ||
target: View, | ||
overlayEndColor: String, | ||
translationYStart: Float | ||
): AnimatorSet { | ||
val translationYAnimator = ObjectAnimator.ofFloat(target, View.TRANSLATION_Y, translationYStart, 0f).apply { | ||
duration = TRANSLATION_ANIMATION_DURATION | ||
} | ||
val alphaAnimator = ObjectAnimator.ofFloat(target, View.ALPHA, 0f, 1f).apply { | ||
duration = ALPHA_ANIMATION_DURATION | ||
} | ||
val translationAndAlphaSet = AnimatorSet().apply { | ||
playTogether(translationYAnimator, alphaAnimator) | ||
} | ||
target.alpha = 0f | ||
|
||
val backgroundColorAnimator = ObjectAnimator.ofArgb( | ||
target, | ||
"backgroundColor", | ||
Color.TRANSPARENT, | ||
parseColorSafely(overlayEndColor) | ||
).apply { | ||
duration = COLOR_ANIMATION_DURATION | ||
startDelay = 0 | ||
} | ||
val colorSet = AnimatorSet().apply { | ||
play(backgroundColorAnimator) | ||
} | ||
|
||
return AnimatorSet().apply { | ||
playSequentially(translationAndAlphaSet, colorSet) | ||
} | ||
} | ||
|
||
private fun createExitAnimation(target: View, translationYEnd: Float): AnimatorSet { | ||
val backgroundColor = extractBackgroundColor(target) | ||
val backgroundColorAnimator = ObjectAnimator.ofArgb( | ||
target, | ||
"backgroundColor", | ||
parseColorSafely(backgroundColor), | ||
Color.TRANSPARENT | ||
).apply { | ||
duration = COLOR_ANIMATION_DURATION | ||
startDelay = 0 | ||
} | ||
val colorSet = AnimatorSet().apply { | ||
play(backgroundColorAnimator) | ||
} | ||
|
||
val translationYAnimator = ObjectAnimator.ofFloat(target, View.TRANSLATION_Y, 0f, translationYEnd).apply { | ||
duration = TRANSLATION_ANIMATION_DURATION | ||
} | ||
val alphaAnimator = ObjectAnimator.ofFloat(target, View.ALPHA, 1f, 0f).apply { | ||
duration = ALPHA_ANIMATION_DURATION | ||
} | ||
val translationAndAlphaSet = AnimatorSet().apply { | ||
playTogether(translationYAnimator, alphaAnimator) | ||
} | ||
|
||
return AnimatorSet().apply { | ||
playSequentially(colorSet, translationAndAlphaSet) | ||
} | ||
} | ||
|
||
@ColorInt | ||
private fun parseColorSafely(color: String): Int { | ||
return try { | ||
Color.parseColor(color) | ||
} catch (ignored: IllegalArgumentException) { | ||
logger.error(ignored.message ?: "Error parsing in-app overlay color") | ||
Color.parseColor(FALLBACK_COLOR_STRING) | ||
} | ||
} | ||
|
||
private fun extractBackgroundColor(target: View): String { | ||
val backgroundDrawable = target.background | ||
if (backgroundDrawable is ColorDrawable) { | ||
return String.format("#%08X", backgroundDrawable.color) | ||
} | ||
|
||
return FALLBACK_COLOR_STRING | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
messaginginapp/src/main/res/animator/animate_in_from_bottom.xml
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
messaginginapp/src/main/res/animator/animate_in_from_top.xml
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
messaginginapp/src/main/res/animator/animate_out_to_bottom.xml
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.