Skip to content

Commit

Permalink
Convert SeekBarMountable to Primitive
Browse files Browse the repository at this point in the history
Summary: As per title

Reviewed By: mkarpio

Differential Revision: D47359059

fbshipit-source-id: b285bf5624c06c69a5e897eccc3616f485fb1bf0
  • Loading branch information
zielinskimz authored and facebook-github-bot committed Jul 14, 2023
1 parent 581f9e5 commit a1851e7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 71 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a1851e7

Please sign in to comment.