Skip to content

Commit 6b5fdf1

Browse files
committed
Rework panelState to Kotlin property
1 parent 4dac516 commit 6b5fdf1

3 files changed

Lines changed: 36 additions & 35 deletions

File tree

app/src/androidTest/kotlin/info/hannes/slidingup/demo/tools/Matcher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fun setValue(value: PanelState): ViewAction {
3636

3737
override fun perform(uiController: UiController?, view: View) {
3838
val seekBar = view as SlidingUpPanelLayout
39-
seekBar.setPanelState(value)
39+
seekBar.panelState = value
4040
}
4141
}
4242
}

app/src/main/kotlin/info/hannes/slidinguppanel/demo/DemoActivity.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class DemoActivity : AppCompatActivity() {
8080
}
8181
})
8282
binding.slidingLayout.setFadeOnClickListener {
83-
binding.slidingLayout.setPanelState(PanelState.COLLAPSED)
83+
binding.slidingLayout.panelState = PanelState.COLLAPSED
8484
Log.i(tag, "FadeOnClickListener ${binding.slidingLayout.panelState}")
8585
}
8686
binding.nameMain.text = Html.fromHtml(getString(R.string.hello))
@@ -116,10 +116,10 @@ class DemoActivity : AppCompatActivity() {
116116
when (item.itemId) {
117117
R.id.action_toggle -> {
118118
if (binding.slidingLayout.panelState != PanelState.HIDDEN) {
119-
binding.slidingLayout.setPanelState(PanelState.HIDDEN)
119+
binding.slidingLayout.panelState = PanelState.HIDDEN
120120
item.setTitle(R.string.action_show)
121121
} else {
122-
binding.slidingLayout.setPanelState(PanelState.COLLAPSED)
122+
binding.slidingLayout.panelState = PanelState.COLLAPSED
123123
item.setTitle(R.string.action_hide)
124124
}
125125
return true
@@ -128,11 +128,11 @@ class DemoActivity : AppCompatActivity() {
128128
R.id.action_anchor -> {
129129
if (binding.slidingLayout.anchorPoint == 1.0f) {
130130
binding.slidingLayout.anchorPoint = 0.7f
131-
binding.slidingLayout.setPanelState(PanelState.ANCHORED)
131+
binding.slidingLayout.panelState = PanelState.ANCHORED
132132
item.setTitle(R.string.action_anchor_disable)
133133
} else {
134134
binding.slidingLayout.anchorPoint = 1.0f
135-
binding.slidingLayout.setPanelState(PanelState.COLLAPSED)
135+
binding.slidingLayout.panelState = PanelState.COLLAPSED
136136
item.setTitle(R.string.action_anchor_enable)
137137
}
138138
return true
@@ -144,7 +144,7 @@ class DemoActivity : AppCompatActivity() {
144144
@Deprecated("Deprecated in Java")
145145
override fun onBackPressed() {
146146
if ((binding.slidingLayout.panelState == PanelState.EXPANDED || binding.slidingLayout.panelState == PanelState.ANCHORED)) {
147-
binding.slidingLayout.setPanelState(PanelState.COLLAPSED)
147+
binding.slidingLayout.panelState = PanelState.COLLAPSED
148148
} else {
149149
super.onBackPressed()
150150
}

library/src/main/kotlin/com/sothree/slidinguppanel/SlidingUpPanelLayout.kt

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -187,40 +187,41 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
187187

188188
private var slideState: PanelState = DEFAULT_SLIDE_STATE
189189

190-
val panelState: PanelState
190+
var panelState: PanelState = slideState
191191
get() = slideState
192-
193-
fun setPanelState(state: PanelState) {
194-
// Abort any running animation, to allow state change
195-
if (dragHelper?.viewDragState == ViewDragHelper.STATE_SETTLING) {
196-
dragHelper?.abort()
197-
}
198-
require(state !== PanelState.DRAGGING) { "Panel state can't be DRAGGING during state set" }
199-
if (!isEnabled ||
200-
(!firstLayout && (slideableView == null)) ||
201-
(state === slideState) || (slideState === PanelState.DRAGGING)
202-
) return
203-
if (firstLayout) {
204-
setPanelStateInternal(state)
205-
} else {
206-
if (slideState === PanelState.HIDDEN) {
207-
slideableView!!.visibility = VISIBLE
208-
requestLayout()
192+
set(value) {
193+
slideState = value
194+
field = value
195+
// Abort any running animation, to allow state change
196+
if (dragHelper?.viewDragState == ViewDragHelper.STATE_SETTLING) {
197+
dragHelper?.abort()
209198
}
210-
when (state) {
211-
PanelState.ANCHORED -> smoothSlideTo(anchorPoint, 0)
212-
PanelState.COLLAPSED -> smoothSlideTo(0f, 0)
213-
PanelState.EXPANDED -> smoothSlideTo(maxSlideOffset, 0)
214-
PanelState.HIDDEN -> {
215-
val newTop =
216-
computePanelTopPosition(0.0f) + if (isSlidingUp) +panelHeight else -panelHeight
217-
smoothSlideTo(computeSlideOffset(newTop), 0)
199+
require(value !== PanelState.DRAGGING) { "Panel state can't be DRAGGING during state set" }
200+
if (!isEnabled ||
201+
(!firstLayout && (slideableView == null)) ||
202+
(value === slideState) || (slideState === PanelState.DRAGGING)
203+
)
204+
return
205+
if (firstLayout) {
206+
setPanelStateInternal(value)
207+
} else {
208+
if (slideState === PanelState.HIDDEN) {
209+
slideableView!!.visibility = VISIBLE
210+
requestLayout()
218211
}
212+
when (value) {
213+
PanelState.ANCHORED -> smoothSlideTo(anchorPoint, 0)
214+
PanelState.COLLAPSED -> smoothSlideTo(0f, 0)
215+
PanelState.EXPANDED -> smoothSlideTo(maxSlideOffset, 0)
216+
PanelState.HIDDEN -> {
217+
val newTop = computePanelTopPosition(0.0f) + if (isSlidingUp) +panelHeight else -panelHeight
218+
smoothSlideTo(computeSlideOffset(newTop), 0)
219+
}
219220

220-
PanelState.DRAGGING -> Unit
221+
PanelState.DRAGGING -> Unit
222+
}
221223
}
222224
}
223-
}
224225

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

0 commit comments

Comments
 (0)