|
| 1 | +package com.iterable.iterableapi |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.Color |
| 5 | +import android.graphics.drawable.ColorDrawable |
| 6 | +import android.graphics.drawable.Drawable |
| 7 | +import android.graphics.drawable.TransitionDrawable |
| 8 | +import android.view.View |
| 9 | +import android.view.Window |
| 10 | +import androidx.core.graphics.ColorUtils |
| 11 | + |
| 12 | +internal class InAppAnimationService { |
| 13 | + |
| 14 | + fun createInAppBackgroundDrawable(hexColor: String?, alpha: Double): ColorDrawable? { |
| 15 | + val backgroundColor = try { |
| 16 | + if (!hexColor.isNullOrEmpty()) { |
| 17 | + Color.parseColor(hexColor) |
| 18 | + } else { |
| 19 | + Color.BLACK |
| 20 | + } |
| 21 | + } catch (e: IllegalArgumentException) { |
| 22 | + IterableLogger.w(TAG, "Invalid background color: $hexColor. Using BLACK.", e) |
| 23 | + Color.BLACK |
| 24 | + } |
| 25 | + |
| 26 | + val backgroundWithAlpha = ColorUtils.setAlphaComponent( |
| 27 | + backgroundColor, |
| 28 | + (alpha * 255).toInt() |
| 29 | + ) |
| 30 | + |
| 31 | + return ColorDrawable(backgroundWithAlpha) |
| 32 | + } |
| 33 | + |
| 34 | + fun animateWindowBackground(window: Window, from: Drawable, to: Drawable, shouldAnimate: Boolean) { |
| 35 | + if (shouldAnimate) { |
| 36 | + val layers = arrayOf(from, to) |
| 37 | + val transition = TransitionDrawable(layers) |
| 38 | + window.setBackgroundDrawable(transition) |
| 39 | + transition.startTransition(ANIMATION_DURATION_MS) |
| 40 | + } else { |
| 41 | + window.setBackgroundDrawable(to) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + fun showInAppBackground(window: Window, hexColor: String?, alpha: Double, shouldAnimate: Boolean) { |
| 46 | + val backgroundDrawable = createInAppBackgroundDrawable(hexColor, alpha) |
| 47 | + |
| 48 | + if (backgroundDrawable == null) { |
| 49 | + IterableLogger.w(TAG, "Failed to create background drawable") |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if (shouldAnimate) { |
| 54 | + val transparentDrawable = ColorDrawable(Color.TRANSPARENT) |
| 55 | + animateWindowBackground(window, transparentDrawable, backgroundDrawable, true) |
| 56 | + } else { |
| 57 | + window.setBackgroundDrawable(backgroundDrawable) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + fun showAndAnimateWebView(webView: View, shouldAnimate: Boolean, context: Context?) { |
| 62 | + if (shouldAnimate && context != null) { |
| 63 | + webView.alpha = 0f |
| 64 | + webView.visibility = View.VISIBLE |
| 65 | + webView.animate() |
| 66 | + .alpha(1.0f) |
| 67 | + .setDuration(ANIMATION_DURATION_MS.toLong()) |
| 68 | + .start() |
| 69 | + } else { |
| 70 | + webView.alpha = 1.0f |
| 71 | + webView.visibility = View.VISIBLE |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + fun hideInAppBackground(window: Window, hexColor: String?, alpha: Double, shouldAnimate: Boolean) { |
| 76 | + if (shouldAnimate) { |
| 77 | + val backgroundDrawable = createInAppBackgroundDrawable(hexColor, alpha) |
| 78 | + val transparentDrawable = ColorDrawable(Color.TRANSPARENT) |
| 79 | + |
| 80 | + if (backgroundDrawable != null) { |
| 81 | + animateWindowBackground(window, backgroundDrawable, transparentDrawable, true) |
| 82 | + } |
| 83 | + } else { |
| 84 | + window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fun prepareViewForDisplay(view: View) { |
| 89 | + view.alpha = 0f |
| 90 | + view.visibility = View.INVISIBLE |
| 91 | + } |
| 92 | + |
| 93 | + companion object { |
| 94 | + private const val ANIMATION_DURATION_MS = 300 |
| 95 | + private const val TAG = "InAppAnimService" |
| 96 | + } |
| 97 | +} |
| 98 | + |
0 commit comments