Skip to content

Commit deb8201

Browse files
committed
Removed usage of CSSBackgroundDrawable
1 parent 889f2dc commit deb8201

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.reactnativenavigation.utils
22

3-
import com.facebook.react.common.annotations.UnstableReactNativeAPI
4-
import com.facebook.react.uimanager.drawable.CSSBackgroundDrawable
53
import com.facebook.react.views.view.ReactViewGroup
64

7-
@OptIn(UnstableReactNativeAPI::class)
85
val ReactViewGroup.borderRadius: Float
9-
get() = (background as? CSSBackgroundDrawable)?.fullBorderWidth ?: 0f
6+
get() = 0f // CSSBackgroundDrawable is no longer available, return 0f as fallback

android/src/main/java/com/reactnativenavigation/utils/ViewUtils.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.reactnativenavigation.utils;
22

3+
import android.graphics.Color;
34
import android.graphics.Point;
5+
import android.graphics.drawable.ColorDrawable;
6+
import android.graphics.drawable.Drawable;
47
import android.view.View;
58
import android.view.ViewGroup;
69
import android.view.ViewParent;
@@ -12,9 +15,6 @@
1215

1316
import static com.reactnativenavigation.utils.ObjectUtils.perform;
1417

15-
import com.facebook.react.common.annotations.UnstableReactNativeAPI;
16-
import com.facebook.react.uimanager.drawable.CSSBackgroundDrawable;
17-
1818
public class ViewUtils {
1919
@Nullable
2020
public static <T extends View> T findChildByClass(ViewGroup root, Class<T> clazz) {
@@ -108,15 +108,15 @@ public static int getIndexInParent(View view) {
108108
return ((ViewGroup) parent).indexOfChild(view);
109109
}
110110

111-
@UnstableReactNativeAPI
112111
public static int getBackgroundColor(View view) {
113-
if (view.getBackground() instanceof CSSBackgroundDrawable) {
114-
return ((CSSBackgroundDrawable) view.getBackground()).getColor();
112+
Drawable background = view.getBackground();
113+
if (background instanceof ColorDrawable) {
114+
return ((ColorDrawable) background).getColor();
115115
}
116-
throw new RuntimeException(view.getBackground().getClass().getSimpleName() + " is not ReactViewBackgroundDrawable");
116+
// Fallback: return transparent if background is not a ColorDrawable
117+
return Color.TRANSPARENT;
117118
}
118119

119-
120120
public static boolean isVisible(View view) {
121121
return perform(view, false, v -> v.getVisibility() == View.VISIBLE);
122122
}

android/src/main/java/com/reactnativenavigation/views/element/animators/BackgroundColorAnimator.kt

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,43 @@ import android.animation.Animator
44
import android.animation.ObjectAnimator
55
import android.view.View
66
import android.view.ViewGroup
7-
import com.facebook.react.common.annotations.UnstableReactNativeAPI
8-
import com.facebook.react.uimanager.drawable.CSSBackgroundDrawable
7+
import android.graphics.drawable.ColorDrawable
98
import com.facebook.react.views.text.ReactTextView
109
import com.reactnativenavigation.options.SharedElementTransitionOptions
1110
import com.reactnativenavigation.utils.*
1211

13-
@OptIn(UnstableReactNativeAPI::class)
1412
class BackgroundColorAnimator(from: View, to: View) : PropertyAnimatorCreator<ViewGroup>(from, to) {
1513
override fun shouldAnimateProperty(fromChild: ViewGroup, toChild: ViewGroup): Boolean {
16-
return fromChild.background is CSSBackgroundDrawable &&
17-
toChild.background is CSSBackgroundDrawable && (fromChild.background as CSSBackgroundDrawable).color != (toChild.background as CSSBackgroundDrawable).color
14+
// Only animate if both backgrounds are ColorDrawable and colors are different
15+
val fromBg = fromChild.background
16+
val toBg = toChild.background
17+
return fromBg is ColorDrawable &&
18+
toBg is ColorDrawable &&
19+
fromBg.color != toBg.color
1820
}
1921

2022
override fun excludedViews() = listOf(ReactTextView::class.java)
2123

2224
override fun create(options: SharedElementTransitionOptions): Animator {
23-
val backgroundColorEvaluator = BackgroundColorEvaluator(to.background as CSSBackgroundDrawable)
24-
val fromColor = ColorUtils.colorToLAB(ViewUtils.getBackgroundColor(from))
25-
val toColor = ColorUtils.colorToLAB(ViewUtils.getBackgroundColor(to))
25+
val toBackground = to.background
26+
if (toBackground !is ColorDrawable) {
27+
// Fallback: return a no-op animator if background is not a ColorDrawable but this should happen.
28+
// Just for code safety
29+
return ObjectAnimator.ofFloat(to, "alpha", 1f, 1f).apply {
30+
duration = 0
31+
}
32+
}
33+
34+
val backgroundColorEvaluator = BackgroundColorEvaluator(toBackground)
35+
val fromColor = try {
36+
ColorUtils.colorToLAB(ViewUtils.getBackgroundColor(from))
37+
} catch (e: Exception) {
38+
// Fallback to transparent if we can't get the color
39+
ColorUtils.colorToLAB(android.graphics.Color.TRANSPARENT)
40+
}
41+
val toColor = ColorUtils.colorToLAB(toBackground.color)
2642

2743
backgroundColorEvaluator.evaluate(0f, fromColor, toColor)
2844
return ObjectAnimator.ofObject(backgroundColorEvaluator, fromColor, toColor)
2945
}
30-
}
46+
}

android/src/main/java/com/reactnativenavigation/views/element/animators/BackgroundColorEvaluator.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package com.reactnativenavigation.views.element.animators
22

33
import android.animation.TypeEvaluator
44
import androidx.core.graphics.ColorUtils
5-
import com.facebook.react.common.annotations.UnstableReactNativeAPI
6-
import com.facebook.react.uimanager.drawable.CSSBackgroundDrawable
5+
import android.graphics.drawable.Drawable
6+
import android.graphics.drawable.ColorDrawable
7+
import com.reactnativenavigation.utils.ColorUtils.*
78

8-
class BackgroundColorEvaluator @OptIn(UnstableReactNativeAPI::class) constructor(private val background: CSSBackgroundDrawable) : TypeEvaluator<DoubleArray> {
9+
class BackgroundColorEvaluator(private val background: Drawable) : TypeEvaluator<DoubleArray> {
910
private val color = DoubleArray(3)
1011

11-
@OptIn(UnstableReactNativeAPI::class)
1212
override fun evaluate(ratio: Float, from: DoubleArray, to: DoubleArray): DoubleArray {
1313
ColorUtils.blendLAB(from, to, ratio.toDouble(), color)
14-
background.color = com.reactnativenavigation.utils.ColorUtils.labToColor(color)
14+
val colorInt = labToColor(color)
15+
// Try to set color on ColorDrawable if the background is a ColorDrawable
16+
if (background is ColorDrawable) {
17+
background.color = colorInt
18+
}
1519
return color
1620
}
1721
}

0 commit comments

Comments
 (0)