Skip to content

Commit 18a0360

Browse files
committed
minor refactor to increase readability and lower file length
1 parent ac0a137 commit 18a0360

File tree

1 file changed

+124
-130
lines changed

1 file changed

+124
-130
lines changed

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

+124-130
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
206206
if (dragHelper?.viewDragState == ViewDragHelper.STATE_SETTLING) {
207207
dragHelper?.abort()
208208
}
209-
require(state !== PanelState.DRAGGING) { "Panel state cannot be DRAGGING during state set" }
209+
require(state !== PanelState.DRAGGING) { "Panel state can't be DRAGGING during state set" }
210210
if (!isEnabled
211-
|| !firstLayout && slideableView == null
212-
|| state === slideState || slideState === PanelState.DRAGGING
211+
|| (!firstLayout && (slideableView == null))
212+
|| (state === slideState) || (slideState === PanelState.DRAGGING)
213213
) return
214214
if (firstLayout) {
215215
setPanelStateInternal(state)
@@ -314,74 +314,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
314314
} else {
315315
var scrollerInterpolator: Interpolator? = null
316316
if (attrs != null) {
317-
val defAttrs = context.obtainStyledAttributes(attrs, DEFAULT_ATTRS)
318-
try {
319-
val gravity = defAttrs.getInt(0, Gravity.NO_GRAVITY)
320-
setGravity(gravity)
321-
} finally {
322-
defAttrs.recycle()
323-
}
324-
val ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingUpPanelLayout)
325-
try {
326-
panelHeight = ta.getDimensionPixelSize(
327-
R.styleable.SlidingUpPanelLayout_umanoPanelHeight,
328-
-1
329-
)
330-
shadowHeight = ta.getDimensionPixelSize(
331-
R.styleable.SlidingUpPanelLayout_umanoShadowHeight,
332-
-1
333-
)
334-
parallaxOffset = ta.getDimensionPixelSize(
335-
R.styleable.SlidingUpPanelLayout_umanoParallaxOffset,
336-
-1
337-
)
338-
minFlingVelocity = ta.getInt(
339-
R.styleable.SlidingUpPanelLayout_umanoFlingVelocity,
340-
DEFAULT_MIN_FLING_VELOCITY
341-
)
342-
coveredFadeColor = ta.getColor(
343-
R.styleable.SlidingUpPanelLayout_umanoFadeColor,
344-
DEFAULT_FADE_COLOR
345-
)
346-
dragViewResId =
347-
ta.getResourceId(R.styleable.SlidingUpPanelLayout_umanoDragView, -1)
348-
scrollableViewResId =
349-
ta.getResourceId(R.styleable.SlidingUpPanelLayout_umanoScrollableView, -1)
350-
aboveShadowResId =
351-
ta.getResourceId(R.styleable.SlidingUpPanelLayout_umanoAboveShadowStyle, -1)
352-
belowShadowResId =
353-
ta.getResourceId(R.styleable.SlidingUpPanelLayout_umanoBelowShadowStyle, -1)
354-
overlayContent = ta.getBoolean(
355-
R.styleable.SlidingUpPanelLayout_umanoOverlay,
356-
DEFAULT_OVERLAY_FLAG
357-
)
358-
clipPanel = ta.getBoolean(
359-
R.styleable.SlidingUpPanelLayout_umanoClipPanel,
360-
DEFAULT_CLIP_PANEL_FLAG
361-
)
362-
anchorPoint = ta.getFloat(
363-
R.styleable.SlidingUpPanelLayout_umanoAnchorPoint,
364-
DEFAULT_ANCHOR_POINT
365-
)
366-
maxSlideOffset = ta.getFloat(
367-
R.styleable.SlidingUpPanelLayout_umanoMaxSlidingOffset,
368-
DEFAULT_MAX_SLIDING_OFFSET
369-
)
370-
slideState = PanelState.values()[ta.getInt(
371-
R.styleable.SlidingUpPanelLayout_umanoInitialState,
372-
DEFAULT_SLIDE_STATE.ordinal
373-
)]
374-
val interpolatorResId = ta.getResourceId(
375-
R.styleable.SlidingUpPanelLayout_umanoScrollInterpolator,
376-
-1
377-
)
378-
if (interpolatorResId != -1) {
379-
scrollerInterpolator =
380-
AnimationUtils.loadInterpolator(context, interpolatorResId)
381-
}
382-
} finally {
383-
ta.recycle()
384-
}
317+
scrollerInterpolator = initAttributes(context, attrs)
385318
}
386319
val density = context.resources.displayMetrics.density
387320
if (panelHeight == -1) {
@@ -394,30 +327,105 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
394327
parallaxOffset = (DEFAULT_PARALLAX_OFFSET * density).toInt()
395328
}
396329
// If the shadow height is zero, don't show the shadow
397-
shadowDrawable = if (shadowHeight > 0) {
398-
if (isSlidingUp) {
399-
if (aboveShadowResId == -1) {
400-
ContextCompat.getDrawable(context, R.drawable.above_shadow)
401-
} else {
402-
ContextCompat.getDrawable(context, aboveShadowResId)
403-
}
404-
} else {
405-
if (belowShadowResId == -1) {
406-
ContextCompat.getDrawable(context, R.drawable.below_shadow)
407-
} else {
408-
ContextCompat.getDrawable(context, belowShadowResId)
409-
}
410-
}
411-
} else {
412-
null
413-
}
330+
shadowDrawable = makeShadowDrawable(context)
414331
setWillNotDraw(false)
415332
dragHelper = create(this, 0.5f, scrollerInterpolator, DragHelperCallback())
416333
dragHelper?.minVelocity = minFlingVelocity * density
417334
touchEnabled = true
418335
}
419336
}
420337

338+
private fun initAttributes(context: Context, attrs: AttributeSet?): Interpolator? {
339+
val defAttrs = context.obtainStyledAttributes(attrs, DEFAULT_ATTRS)
340+
try {
341+
val gravity = defAttrs.getInt(0, Gravity.NO_GRAVITY)
342+
setGravity(gravity)
343+
} finally {
344+
defAttrs.recycle()
345+
}
346+
val ta = context.obtainStyledAttributes(attrs, R.styleable.SlidingUpPanelLayout)
347+
var scrollerInterpolator: Interpolator? = null
348+
try {
349+
panelHeight = ta.getDimensionPixelSize(
350+
R.styleable.SlidingUpPanelLayout_umanoPanelHeight,
351+
-1
352+
)
353+
shadowHeight = ta.getDimensionPixelSize(
354+
R.styleable.SlidingUpPanelLayout_umanoShadowHeight,
355+
-1
356+
)
357+
parallaxOffset = ta.getDimensionPixelSize(
358+
R.styleable.SlidingUpPanelLayout_umanoParallaxOffset,
359+
-1
360+
)
361+
minFlingVelocity = ta.getInt(
362+
R.styleable.SlidingUpPanelLayout_umanoFlingVelocity,
363+
DEFAULT_MIN_FLING_VELOCITY
364+
)
365+
coveredFadeColor = ta.getColor(
366+
R.styleable.SlidingUpPanelLayout_umanoFadeColor,
367+
DEFAULT_FADE_COLOR
368+
)
369+
dragViewResId =
370+
ta.getResourceId(R.styleable.SlidingUpPanelLayout_umanoDragView, -1)
371+
scrollableViewResId =
372+
ta.getResourceId(R.styleable.SlidingUpPanelLayout_umanoScrollableView, -1)
373+
aboveShadowResId =
374+
ta.getResourceId(R.styleable.SlidingUpPanelLayout_umanoAboveShadowStyle, -1)
375+
belowShadowResId =
376+
ta.getResourceId(R.styleable.SlidingUpPanelLayout_umanoBelowShadowStyle, -1)
377+
overlayContent = ta.getBoolean(
378+
R.styleable.SlidingUpPanelLayout_umanoOverlay,
379+
DEFAULT_OVERLAY_FLAG
380+
)
381+
clipPanel = ta.getBoolean(
382+
R.styleable.SlidingUpPanelLayout_umanoClipPanel,
383+
DEFAULT_CLIP_PANEL_FLAG
384+
)
385+
anchorPoint = ta.getFloat(
386+
R.styleable.SlidingUpPanelLayout_umanoAnchorPoint,
387+
DEFAULT_ANCHOR_POINT
388+
)
389+
maxSlideOffset = ta.getFloat(
390+
R.styleable.SlidingUpPanelLayout_umanoMaxSlidingOffset,
391+
DEFAULT_MAX_SLIDING_OFFSET
392+
)
393+
slideState = PanelState.values()[ta.getInt(
394+
R.styleable.SlidingUpPanelLayout_umanoInitialState,
395+
DEFAULT_SLIDE_STATE.ordinal
396+
)]
397+
val interpolatorResId = ta.getResourceId(
398+
R.styleable.SlidingUpPanelLayout_umanoScrollInterpolator,
399+
-1
400+
)
401+
if (interpolatorResId != -1) {
402+
scrollerInterpolator =
403+
AnimationUtils.loadInterpolator(context, interpolatorResId)
404+
}
405+
} finally {
406+
ta.recycle()
407+
}
408+
return scrollerInterpolator
409+
}
410+
411+
private fun makeShadowDrawable(context: Context) = if (shadowHeight > 0) {
412+
if (isSlidingUp) {
413+
if (aboveShadowResId == -1) {
414+
ContextCompat.getDrawable(context, R.drawable.above_shadow)
415+
} else {
416+
ContextCompat.getDrawable(context, aboveShadowResId)
417+
}
418+
} else {
419+
if (belowShadowResId == -1) {
420+
ContextCompat.getDrawable(context, R.drawable.below_shadow)
421+
} else {
422+
ContextCompat.getDrawable(context, belowShadowResId)
423+
}
424+
}
425+
} else {
426+
null
427+
}
428+
421429
/**
422430
* Set the Drag View after the view is inflated
423431
*/
@@ -493,6 +501,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
493501
}
494502

495503
fun addPanelSlideListener(listener: PanelSlideListener) {
504+
// TODO why do we synchronize again, this is already a costly CopyOnWriteArrayList? Maybe should use Kotlin Collections.synchronizedList() wrapper instead?
496505
synchronized(panelSlideListeners) { panelSlideListeners.add(listener) }
497506
}
498507

@@ -608,11 +617,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
608617
* previous state and the new state. Careful: this process is synchronized to avoid out-of-order
609618
* callbacks. Call this method sparingly.
610619
*/
611-
fun dispatchOnPanelStateChanged(
612-
panel: View,
613-
previousState: PanelState,
614-
newState: PanelState,
615-
) {
620+
fun dispatchOnPanelStateChanged(panel: View, previousState: PanelState, newState: PanelState) {
616621
synchronized(panelSlideListeners) {
617622
for (l in panelSlideListeners) {
618623
l.onPanelStateChanged(panel, previousState, newState)
@@ -622,13 +627,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
622627
}
623628

624629
fun updateObscuredViewVisibility() {
625-
if (childCount == 0) {
626-
return
627-
}
628-
val leftBound = paddingLeft
629-
val rightBound = width - paddingRight
630-
val topBound = paddingTop
631-
val bottomBound = height - paddingBottom
630+
if (childCount == 0) return
632631
val left: Int
633632
val right: Int
634633
val top: Int
@@ -645,10 +644,10 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
645644
left = 0
646645
}
647646
val child = getChildAt(0)
648-
val clampedChildLeft = max(leftBound, child.left)
649-
val clampedChildTop = max(topBound, child.top)
650-
val clampedChildRight = min(rightBound, child.right)
651-
val clampedChildBottom = min(bottomBound, child.bottom)
647+
val clampedChildLeft = max(paddingLeft, child.left)
648+
val clampedChildTop = max(paddingTop, child.top)
649+
val clampedChildRight = min(width - paddingRight, child.right)
650+
val clampedChildBottom = min(height - paddingBottom, child.bottom)
652651
val vis: Int =
653652
if (clampedChildLeft >= left && clampedChildTop >= top && clampedChildRight <= right && clampedChildBottom <= bottom) {
654653
INVISIBLE
@@ -728,13 +727,16 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
728727
// See https://github.com/umano/AndroidSlidingUpPanel/issues/412.
729728
height -= lp.topMargin
730729
}
731-
var childWidthSpec: Int
732-
childWidthSpec = if (lp.width == MarginLayoutParams.WRAP_CONTENT) {
733-
MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST)
734-
} else if (lp.width == MarginLayoutParams.MATCH_PARENT) {
735-
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY)
736-
} else {
737-
MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY)
730+
val childWidthSpec: Int = when (lp.width) {
731+
MarginLayoutParams.WRAP_CONTENT -> {
732+
MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST)
733+
}
734+
MarginLayoutParams.MATCH_PARENT -> {
735+
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY)
736+
}
737+
else -> {
738+
MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY)
739+
}
738740
}
739741
var childHeightSpec: Int
740742
if (lp.height == MarginLayoutParams.WRAP_CONTENT) {
@@ -815,7 +817,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
815817
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
816818
// If the scrollable view is handling touch, never intercept
817819
if (isScrollableViewHandlingTouch || !isTouchEnabled) {
818-
dragHelper!!.abort()
820+
dragHelper?.abort()
819821
return false
820822
}
821823
val action = ev.action
@@ -883,7 +885,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
883885
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
884886
val action = ev.action
885887
if (!isEnabled || !isTouchEnabled || isUnableToDrag && action != MotionEvent.ACTION_DOWN) {
886-
dragHelper!!.abort()
888+
dragHelper?.abort()
887889
return super.dispatchTouchEvent(ev)
888890
}
889891
val x = ev.x
@@ -1138,23 +1140,22 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
11381140
/**
11391141
* Tests scrollability within child views of v given a delta of dx.
11401142
*
1141-
* @param v View to test for horizontal scrollability
1143+
* @param view View to test for horizontal scrollability
11421144
* @param checkV Whether the view v passed should itself be checked for scrollability (true),
11431145
* or just its children (false).
11441146
* @param dx Delta scrolled in pixels
11451147
* @param x X coordinate of the active touch point
11461148
* @param y Y coordinate of the active touch point
11471149
* @return true if child views of v can be scrolled by delta of dx.
11481150
*/
1149-
protected fun canScroll(v: View, checkV: Boolean, dx: Int, x: Int, y: Int): Boolean {
1150-
if (v is ViewGroup) {
1151-
val group = v
1152-
val scrollX = v.getScrollX()
1153-
val scrollY = v.getScrollY()
1154-
val count = group.childCount
1151+
protected fun canScroll(view: View, checkV: Boolean, dx: Int, x: Int, y: Int): Boolean {
1152+
if (view is ViewGroup) {
1153+
val scrollX = view.getScrollX()
1154+
val scrollY = view.getScrollY()
1155+
val count = view.childCount
11551156
// Count backwards - let topmost views consume scroll distance first.
11561157
for (i in count - 1 downTo 0) {
1157-
val child = group.getChildAt(i)
1158+
val child = view.getChildAt(i)
11581159
if (x + scrollX >= child.left && x + scrollX < child.right && y + scrollY >= child.top && y + scrollY < child.bottom &&
11591160
canScroll(
11601161
child, true, dx, x + scrollX - child.left,
@@ -1165,7 +1166,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
11651166
}
11661167
}
11671168
}
1168-
return checkV && v.canScrollHorizontally(-dx)
1169+
return checkV && view.canScrollHorizontally(-dx)
11691170
}
11701171

11711172
override fun generateDefaultLayoutParams(): LayoutParams {
@@ -1234,13 +1235,7 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
12341235
setAllChildrenVisible()
12351236
}
12361237

1237-
override fun onViewPositionChanged(
1238-
changedView: View?,
1239-
left: Int,
1240-
top: Int,
1241-
dx: Int,
1242-
dy: Int,
1243-
) {
1238+
override fun onViewPositionChanged(changedView: View?, left: Int, top: Int, dx: Int, dy: Int) {
12441239
onPanelDragged(top)
12451240
invalidate()
12461241
}
@@ -1304,5 +1299,4 @@ open class SlidingUpPanelLayout @JvmOverloads constructor(
13041299
}
13051300
}
13061301
}
1307-
13081302
}

0 commit comments

Comments
 (0)