diff --git a/sample/src/main/java/com/facebook/samples/litho/kotlin/animations/dynamicprops/SeekBarMountable.kt b/sample/src/main/java/com/facebook/samples/litho/kotlin/animations/dynamicprops/SeekBarMountable.kt deleted file mode 100644 index 409e09d6098..00000000000 --- a/sample/src/main/java/com/facebook/samples/litho/kotlin/animations/dynamicprops/SeekBarMountable.kt +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.facebook.samples.litho.kotlin.animations.dynamicprops - -import android.content.Context -import android.widget.SeekBar -import com.facebook.litho.MeasureScope -import com.facebook.litho.MountableComponent -import com.facebook.litho.MountableComponentScope -import com.facebook.litho.MountableRenderResult -import com.facebook.litho.SimpleMountable -import com.facebook.litho.Style -import com.facebook.rendercore.MeasureResult - -class SeekBarMountable( - private val onProgressChange: (Float) -> Unit, - private val initialValue: Float = 0f, - private val style: Style? = null -) : MountableComponent() { - - override fun MountableComponentScope.render(): MountableRenderResult { - return MountableRenderResult(Mountable(onProgressChange, initialValue), style) - } - - private class Mountable( - private val onProgressChange: (Float) -> Unit, - private val initialValue: Float - ) : SimpleMountable(RenderType.VIEW) { - - private val MAX = 256 - - override fun createContent(context: Context): SeekBar = SeekBar(context) - - override fun mount(c: Context, content: SeekBar, layoutData: Any?) { - content.max = MAX - content.progress = (initialValue * MAX).toInt() - content.setOnSeekBarChangeListener( - object : SeekBar.OnSeekBarChangeListener { - override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { - onProgressChange(progress / MAX.toFloat()) - } - - override fun onStartTrackingTouch(seekBar: SeekBar) = Unit - - override fun onStopTrackingTouch(seekBar: SeekBar) = Unit - }) - } - - override fun unmount(c: Context, content: SeekBar, layoutData: Any?) { - content.setOnSeekBarChangeListener(null) - } - - override fun MeasureScope.measure(widthSpec: Int, heightSpec: Int): MeasureResult = - withEqualSize(widthSpec, heightSpec) - } -} diff --git a/sample/src/main/java/com/facebook/samples/litho/kotlin/animations/dynamicprops/SeekBarPrimitive.kt b/sample/src/main/java/com/facebook/samples/litho/kotlin/animations/dynamicprops/SeekBarPrimitive.kt new file mode 100644 index 00000000000..0e11ef16440 --- /dev/null +++ b/sample/src/main/java/com/facebook/samples/litho/kotlin/animations/dynamicprops/SeekBarPrimitive.kt @@ -0,0 +1,70 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.facebook.samples.litho.kotlin.animations.dynamicprops + +import android.widget.SeekBar +import com.facebook.litho.LithoPrimitive +import com.facebook.litho.PrimitiveComponent +import com.facebook.litho.PrimitiveComponentScope +import com.facebook.litho.Style +import com.facebook.rendercore.primitives.EqualDimensionsLayoutBehavior +import com.facebook.rendercore.primitives.ViewAllocator + +class SeekBarPrimitive( + private val onProgressChange: (Float) -> Unit, + private val initialValue: Float = 0f, + private val style: Style? = null +) : PrimitiveComponent() { + + private val MAX = 256 + + override fun PrimitiveComponentScope.render(): LithoPrimitive { + return LithoPrimitive( + layoutBehavior = EqualDimensionsLayoutBehavior(), + mountBehavior = + MountBehavior(ViewAllocator { context -> SeekBar(context) }) { + bind(initialValue) { content -> + val defaultMax = content.max + content.max = MAX + content.progress = (initialValue * MAX).toInt() + onUnbind { + content.max = defaultMax + content.progress = 0 + } + } + + bind(onProgressChange) { content -> + content.setOnSeekBarChangeListener( + object : SeekBar.OnSeekBarChangeListener { + override fun onProgressChanged( + seekBar: SeekBar, + progress: Int, + fromUser: Boolean + ) { + onProgressChange(progress / MAX.toFloat()) + } + + override fun onStartTrackingTouch(seekBar: SeekBar) = Unit + + override fun onStopTrackingTouch(seekBar: SeekBar) = Unit + }) + onUnbind { content.setOnSeekBarChangeListener(null) } + } + }, + style = style) + } +} diff --git a/sample/src/main/java/com/facebook/samples/litho/kotlin/animations/dynamicprops/SeekBarSpec.kt b/sample/src/main/java/com/facebook/samples/litho/kotlin/animations/dynamicprops/SeekBarSpec.kt index 9a0a953dd4a..40d3bc3cc38 100644 --- a/sample/src/main/java/com/facebook/samples/litho/kotlin/animations/dynamicprops/SeekBarSpec.kt +++ b/sample/src/main/java/com/facebook/samples/litho/kotlin/animations/dynamicprops/SeekBarSpec.kt @@ -85,7 +85,7 @@ fun ResourcesScope.SeekBar( ): Component = Row { label?.let { child(Text(label, style = Style.margin(end = 10.dp))) } child( - SeekBarMountable( + SeekBarPrimitive( initialValue = initialValue, onProgressChange = onProgressChanged, style = Style.height(14.dp).flex(grow = 1f) + style))