Skip to content

Commit

Permalink
Rework panelState to Kotlin property
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed Jan 4, 2025
1 parent cfefb33 commit afc8792
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun setValue(value: PanelState): ViewAction {

override fun perform(uiController: UiController?, view: View) {
val slidingUpPanelLayout = view as SlidingUpPanelLayout
slidingUpPanelLayout.setPanelState(value)
slidingUpPanelLayout.panelState = value
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class DemoActivity : AppCompatActivity() {
}
})
binding.slidingLayout.setFadeOnClickListener {
binding.slidingLayout.setPanelState(PanelState.COLLAPSED)
binding.slidingLayout.panelState = PanelState.COLLAPSED
Log.i(tag, "FadeOnClickListener ${binding.slidingLayout.panelState}")
}
binding.nameMain.text = Html.fromHtml(getString(R.string.hello))
Expand Down Expand Up @@ -116,10 +116,10 @@ class DemoActivity : AppCompatActivity() {
when (item.itemId) {
R.id.action_toggle -> {
if (binding.slidingLayout.panelState != PanelState.HIDDEN) {
binding.slidingLayout.setPanelState(PanelState.HIDDEN)
binding.slidingLayout.panelState = PanelState.HIDDEN
item.setTitle(R.string.action_show)
} else {
binding.slidingLayout.setPanelState(PanelState.COLLAPSED)
binding.slidingLayout.panelState = PanelState.COLLAPSED
item.setTitle(R.string.action_hide)
}
return true
Expand All @@ -128,11 +128,11 @@ class DemoActivity : AppCompatActivity() {
R.id.action_anchor -> {
if (binding.slidingLayout.anchorPoint == 1.0f) {
binding.slidingLayout.anchorPoint = 0.7f
binding.slidingLayout.setPanelState(PanelState.ANCHORED)
binding.slidingLayout.panelState = PanelState.ANCHORED
item.setTitle(R.string.action_anchor_disable)
} else {
binding.slidingLayout.anchorPoint = 1.0f
binding.slidingLayout.setPanelState(PanelState.COLLAPSED)
binding.slidingLayout.panelState = PanelState.COLLAPSED
item.setTitle(R.string.action_anchor_enable)
}
return true
Expand All @@ -144,7 +144,7 @@ class DemoActivity : AppCompatActivity() {
@Deprecated("Deprecated in Java")
override fun onBackPressed() {
if ((binding.slidingLayout.panelState == PanelState.EXPANDED || binding.slidingLayout.panelState == PanelState.ANCHORED)) {
binding.slidingLayout.setPanelState(PanelState.COLLAPSED)
binding.slidingLayout.panelState = PanelState.COLLAPSED
} else {
super.onBackPressed()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,39 +186,40 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(

private var slideState: PanelState = DEFAULT_SLIDE_STATE

val panelState: PanelState
var panelState: PanelState = DEFAULT_SLIDE_STATE
get() = slideState

fun setPanelState(value: PanelState) {
// Abort any running animation, to allow state change
if (dragHelper?.viewDragState == ViewDragHelper.STATE_SETTLING) {
dragHelper?.abort()
}
require(value !== PanelState.DRAGGING) { "Panel state can't be DRAGGING during state set" }
if (!isEnabled ||
(!firstLayout && (slideableView == null)) ||
(value === slideState) || (slideState === PanelState.DRAGGING)
) return
if (firstLayout) {
setPanelStateInternal(value)
} else {
if (slideState === PanelState.HIDDEN) {
slideableView!!.visibility = VISIBLE
requestLayout()
set(value) {
slideState = value
field = value
// Abort any running animation, to allow state change
if (dragHelper?.viewDragState == ViewDragHelper.STATE_SETTLING) {
dragHelper?.abort()
}
when (value) {
PanelState.ANCHORED -> smoothSlideTo(anchorPoint, 0)
PanelState.COLLAPSED -> smoothSlideTo(0f, 0)
PanelState.EXPANDED -> smoothSlideTo(maxSlideOffset, 0)
PanelState.HIDDEN -> {
val newTop = computePanelTopPosition(0.0f) + if (isSlidingUp) +panelHeight else -panelHeight
smoothSlideTo(computeSlideOffset(newTop), 0)
require(value !== PanelState.DRAGGING) { "Panel state can't be DRAGGING during state set" }
if (!isEnabled ||
(!firstLayout && (slideableView == null)) ||
(value === slideState) || (slideState === PanelState.DRAGGING)
) return
if (firstLayout) {
setPanelStateInternal(value)
} else {
if (slideState === PanelState.HIDDEN) {
slideableView!!.visibility = VISIBLE
requestLayout()
}
when (value) {
PanelState.ANCHORED -> smoothSlideTo(anchorPoint, 0)
PanelState.COLLAPSED -> smoothSlideTo(0f, 0)
PanelState.EXPANDED -> smoothSlideTo(maxSlideOffset, 0)
PanelState.HIDDEN -> {
val newTop = computePanelTopPosition(0.0f) + if (isSlidingUp) +panelHeight else -panelHeight
smoothSlideTo(computeSlideOffset(newTop), 0)
}

PanelState.DRAGGING -> Unit
PanelState.DRAGGING -> Unit
}
}
}
}

/**
* If the current slide state is DRAGGING, this will store the last non dragging state
Expand Down

0 comments on commit afc8792

Please sign in to comment.