Skip to content

Commit d4e82f9

Browse files
zielinskimzfacebook-github-bot
authored andcommitted
Replace ProgressSpec with Progress Primitive Component
Summary: As per title - this diff replaces `ProgressSpec` component with a Progress Primitive Component. In this change, we're: 1. Moving `Progress` Primitive Component from Litho sample app to litho widget module. 2. Renaming `Progress` Primitive Component to `ExperimentalProgress`. 3. Renaming `ProgressSpec` to `ProgressComponentSpec` 4. Introducing `ProgressSpec` LayoutSpec which returns either `ProgressComponentSpec` or `ExperimentalProgress` depending on MC value. Reviewed By: apowolny Differential Revision: D65139347 fbshipit-source-id: 5632801d3b62c7fa98cc8b611d4b6732fce1388c
1 parent 748de68 commit d4e82f9

File tree

7 files changed

+100
-24
lines changed

7 files changed

+100
-24
lines changed

litho-core/src/main/java/com/facebook/litho/config/ComponentsConfiguration.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ internal constructor(
280280
*/
281281
@JvmField var usePrimitiveImage: Boolean = false
282282

283+
/**
284+
* This flag is used to enable using PrimitiveComponent implementation of a Progress component.
285+
*/
286+
@JvmField var usePrimitiveProgress: Boolean = false
287+
283288
/** This config will enable logging of interactable components with 0 alpha */
284289
@JvmField var isZeroAlphaLoggingEnabled: Boolean = false
285290

litho-it/src/test/com/facebook/litho/widget/ProgressSpecTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public void testUnsetSize() {
5353
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
5454
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
5555

56-
assertThat(view.getMeasuredWidth()).isEqualTo(ProgressSpec.DEFAULT_SIZE);
57-
assertThat(view.getMeasuredHeight()).isEqualTo(ProgressSpec.DEFAULT_SIZE);
56+
assertThat(view.getMeasuredWidth()).isEqualTo(ProgressComponentSpec.DEFAULT_SIZE);
57+
assertThat(view.getMeasuredHeight()).isEqualTo(ProgressComponentSpec.DEFAULT_SIZE);
5858
}
5959

6060
private LithoView getMountedView() {
61-
Progress.Builder progress = Progress.create(mContext);
61+
ProgressComponent.Builder progress = ProgressComponent.create(mContext);
6262

6363
return ComponentTestHelper.mountComponent(progress);
6464
}

sample/src/main/java/com/facebook/samples/litho/kotlin/primitives/widgets/Progress.kt renamed to litho-widget/src/main/java/com/facebook/litho/widget/ExperimentalProgress.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.facebook.samples.litho.kotlin.primitives.widgets
17+
package com.facebook.litho.widget
1818

1919
import android.graphics.Color
20-
import android.graphics.PorterDuff
2120
import android.graphics.drawable.Drawable
21+
import androidx.core.graphics.BlendModeColorFilterCompat
22+
import androidx.core.graphics.BlendModeCompat
2223
import com.facebook.litho.LithoPrimitive
2324
import com.facebook.litho.PrimitiveComponent
2425
import com.facebook.litho.PrimitiveComponentScope
2526
import com.facebook.litho.Style
26-
import com.facebook.litho.widget.ProgressView
27+
import com.facebook.litho.annotations.ExperimentalLithoApi
2728
import com.facebook.rendercore.Size
2829
import com.facebook.rendercore.SizeConstraints
2930
import com.facebook.rendercore.primitives.LayoutBehavior
@@ -38,33 +39,35 @@ import com.facebook.rendercore.utils.withEqualDimensions
3839
* @param indeterminateDrawable Drawable to be shown to show progress.
3940
* @param color Tint color for the drawable.
4041
*/
41-
class Progress(
42+
@ExperimentalLithoApi
43+
class ExperimentalProgress(
4244
private val color: Int = Color.TRANSPARENT,
4345
private val indeterminateDrawable: Drawable? = null,
4446
private val style: Style? = null
4547
) : PrimitiveComponent() {
4648

47-
@Suppress("DEPRECATION")
4849
override fun PrimitiveComponentScope.render(): LithoPrimitive {
4950
return LithoPrimitive(
5051
layoutBehavior = ProgressLayoutBehavior,
5152
mountBehavior =
5253
MountBehavior(ViewAllocator { context -> ProgressView(context) }) {
5354
bind(indeterminateDrawable, color) { content ->
5455
val defaultIndeterminateDrawable = content.indeterminateDrawable
56+
5557
indeterminateDrawable?.let { content.indeterminateDrawable = indeterminateDrawable }
5658
content.indeterminateDrawable?.let {
5759
if (color != Color.TRANSPARENT) {
58-
content.indeterminateDrawable
59-
.mutate()
60-
.setColorFilter(color, PorterDuff.Mode.MULTIPLY)
60+
content.indeterminateDrawable.mutate().colorFilter =
61+
BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
62+
color, BlendModeCompat.MODULATE)
6163
}
6264
}
6365
onUnbind {
6466
// restore the color first, since it acts on the indeterminateDrawable
6567
if (color != Color.TRANSPARENT && content.indeterminateDrawable != null) {
6668
content.indeterminateDrawable.mutate().clearColorFilter()
6769
}
70+
6871
content.indeterminateDrawable = defaultIndeterminateDrawable
6972
}
7073
}

litho-widget/src/main/java/com/facebook/litho/widget/ProgressSpec.java renamed to litho-widget/src/main/java/com/facebook/litho/widget/ProgressComponentSpec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* @prop color Tint color for the drawable.
5252
*/
5353
@MountSpec(isPureRender = true)
54-
class ProgressSpec {
54+
class ProgressComponentSpec {
5555

5656
static final int DEFAULT_SIZE = 50;
5757

@@ -65,7 +65,7 @@ static void onLoadStyle(ComponentContext c, Output<Drawable> indeterminateDrawab
6565
@OnPrepare
6666
static void onPrepare(
6767
ComponentContext c,
68-
@Prop(optional = true, resType = ResType.DRAWABLE) Drawable indeterminateDrawable,
68+
@Prop(optional = true, resType = ResType.DRAWABLE) @Nullable Drawable indeterminateDrawable,
6969
Output<Drawable> resolvedIndeterminateDrawable) {
7070
if (indeterminateDrawable != null) {
7171
resolvedIndeterminateDrawable.set(indeterminateDrawable);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.facebook.litho.widget
18+
19+
import android.graphics.Color
20+
import android.graphics.drawable.Drawable
21+
import com.facebook.litho.Component
22+
import com.facebook.litho.ComponentContext
23+
import com.facebook.litho.annotations.ExcuseMySpec
24+
import com.facebook.litho.annotations.ExperimentalLithoApi
25+
import com.facebook.litho.annotations.LayoutSpec
26+
import com.facebook.litho.annotations.OnCreateLayout
27+
import com.facebook.litho.annotations.Prop
28+
import com.facebook.litho.annotations.PropDefault
29+
import com.facebook.litho.annotations.Reason
30+
import com.facebook.litho.annotations.ResType
31+
import com.facebook.litho.config.ComponentsConfiguration
32+
33+
/**
34+
* Renders an infinitely spinning progress bar.
35+
*
36+
* @uidocs
37+
* @prop indeterminateDrawable Drawable to be shown to show progress.
38+
* @prop color Tint color for the drawable.
39+
*/
40+
@ExcuseMySpec(reason = Reason.J2K_CONVERSION)
41+
@LayoutSpec
42+
object ProgressSpec {
43+
44+
@PropDefault val color: Int = Color.TRANSPARENT
45+
46+
@OptIn(ExperimentalLithoApi::class)
47+
@OnCreateLayout
48+
fun onCreateLayout(
49+
c: ComponentContext,
50+
@Prop(optional = true, resType = ResType.COLOR) color: Int,
51+
@Prop(optional = true, resType = ResType.DRAWABLE) indeterminateDrawable: Drawable?,
52+
): Component {
53+
return if (ComponentsConfiguration.usePrimitiveProgress) {
54+
ExperimentalProgress(
55+
color = color,
56+
indeterminateDrawable = indeterminateDrawable,
57+
)
58+
} else {
59+
ProgressComponent.create(c).color(color).indeterminateDrawable(indeterminateDrawable).build()
60+
}
61+
}
62+
}

sample/src/main/java/com/facebook/samples/litho/kotlin/primitives/widgets/PrimitiveWidgetsExampleComponent.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import com.facebook.litho.kotlin.widget.Text
3232
import com.facebook.litho.view.background
3333
import com.facebook.litho.view.backgroundColor
3434
import com.facebook.litho.widget.ExperimentalImage
35+
import com.facebook.litho.widget.ExperimentalProgress
3536
import com.facebook.rendercore.dp
3637
import com.facebook.rendercore.drawableRes
3738
import com.facebook.samples.litho.R.drawable.ic_launcher
@@ -59,7 +60,7 @@ class PrimitiveWidgetsExampleComponent : KComponent() {
5960
ExperimentalImage(
6061
drawable = drawableRes(ic_launcher), style = Style.width(100.dp).height(100.dp)))
6162
child(Text("Progress"))
62-
child(Progress(style = Style.width(100.dp).height(100.dp)))
63+
child(ExperimentalProgress(style = Style.width(100.dp).height(100.dp)))
6364
child(Text("Horizontal Scroll"))
6465
child(HorizontalScroll { Row { getComponents(this) } })
6566
child(Text("Vertical Scroll"))

sample/src/test/java/com/facebook/samples/litho/kotlin/primitives/widgets/ProgressTest.kt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ package com.facebook.samples.litho.kotlin.primitives.widgets
1919
import android.graphics.Color
2020
import android.graphics.drawable.ColorDrawable
2121
import com.facebook.litho.Style
22+
import com.facebook.litho.annotations.ExperimentalLithoApi
2223
import com.facebook.litho.core.height
2324
import com.facebook.litho.core.width
2425
import com.facebook.litho.testing.LithoTestRule
2526
import com.facebook.litho.testing.assertj.LithoAssertions.assertThat
2627
import com.facebook.litho.testing.testrunner.LithoTestRunner
28+
import com.facebook.litho.widget.ExperimentalProgress
2729
import com.facebook.litho.widget.ProgressView
2830
import com.facebook.rendercore.px
2931
import junit.framework.Assert.assertNotNull
@@ -33,6 +35,7 @@ import org.junit.Test
3335
import org.junit.runner.RunWith
3436

3537
/** Tests for [ProgressComponent] */
38+
@OptIn(ExperimentalLithoApi::class)
3639
@RunWith(LithoTestRunner::class)
3740
class ProgressTest {
3841

@@ -41,10 +44,10 @@ class ProgressTest {
4144
@Test
4245
fun `ProgressComponent should render`() {
4346
val testLithoView =
44-
lithoViewRule.render { Progress(style = Style.width(100.px).height(100.px)) }
47+
lithoViewRule.render { ExperimentalProgress(style = Style.width(100.px).height(100.px)) }
4548

4649
// should find an Progress in the tree
47-
assertNotNull(testLithoView.findComponent(Progress::class))
50+
assertNotNull(testLithoView.findComponent(ExperimentalProgress::class))
4851

4952
// should mount an Progress
5053
assertThat(testLithoView.lithoView.mountItemCount).isEqualTo(1)
@@ -56,8 +59,8 @@ class ProgressTest {
5659

5760
@Test
5861
fun `same instance should be equivalent`() {
59-
val component = Progress()
60-
val component2 = Progress()
62+
val component = ExperimentalProgress()
63+
val component2 = ExperimentalProgress()
6164

6265
assertThat(component).isEquivalentTo(component2)
6366
assertThat(component).isEquivalentTo(component2, true)
@@ -67,13 +70,15 @@ class ProgressTest {
6770
fun `components with same prop values should be equivalent`() {
6871
val colorDrawable = ColorDrawable(Color.RED)
6972
val color = Color.BLACK
70-
val firstProgressWithColorDrawable = Progress(indeterminateDrawable = colorDrawable)
71-
val secondProgressWithColorDrawable = Progress(indeterminateDrawable = colorDrawable)
72-
val firstProgressWithColor = Progress(color = color)
73-
val secondProgressWithColor = Progress(color = color)
74-
val firstProgressWithBothParams = Progress(indeterminateDrawable = colorDrawable, color = color)
73+
val firstProgressWithColorDrawable = ExperimentalProgress(indeterminateDrawable = colorDrawable)
74+
val secondProgressWithColorDrawable =
75+
ExperimentalProgress(indeterminateDrawable = colorDrawable)
76+
val firstProgressWithColor = ExperimentalProgress(color = color)
77+
val secondProgressWithColor = ExperimentalProgress(color = color)
78+
val firstProgressWithBothParams =
79+
ExperimentalProgress(indeterminateDrawable = colorDrawable, color = color)
7580
val secondProgressWithBothParams =
76-
Progress(indeterminateDrawable = colorDrawable, color = color)
81+
ExperimentalProgress(indeterminateDrawable = colorDrawable, color = color)
7782

7883
assertThat(firstProgressWithColorDrawable).isEquivalentTo(secondProgressWithColorDrawable)
7984
assertThat(firstProgressWithColorDrawable).isEquivalentTo(secondProgressWithColorDrawable, true)

0 commit comments

Comments
 (0)