Skip to content

Commit

Permalink
Fix issues on API 14
Browse files Browse the repository at this point in the history
  • Loading branch information
André Sousa committed Oct 31, 2018
1 parent b727804 commit b61190c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.graphics.RectF
import android.os.Build
import android.util.AttributeSet
import android.widget.FrameLayout

Expand All @@ -48,9 +49,17 @@ internal class CornerRadiusFrameLayout : FrameLayout {
)

// Constructor
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
constructor(context: Context) : super(context) {
initView()
}

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
initView()
}

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
initView()
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
Expand All @@ -59,14 +68,14 @@ internal class CornerRadiusFrameLayout : FrameLayout {
resetPath()
}

override fun dispatchDraw(canvas: Canvas) = when {
noCornerRadius -> super.dispatchDraw(canvas)
override fun draw(canvas: Canvas?) = when {
noCornerRadius -> super.draw(canvas)

else -> with(canvas) {
else -> with(canvas!!) {
val save = save()
clipPath(path)

super.dispatchDraw(this)
super.draw(this)
restoreToCount(save)
}
}
Expand All @@ -82,12 +91,13 @@ internal class CornerRadiusFrameLayout : FrameLayout {
outerRadii[2] = radius
outerRadii[3] = radius

noCornerRadius = (radius == 0f)

if (width == 0 || height == 0) {
// Discard invalid events
return
}

noCornerRadius = radius == 0f
resetPath()
invalidate()
}
Expand All @@ -96,6 +106,12 @@ internal class CornerRadiusFrameLayout : FrameLayout {

//region PRIVATE METHODS

private fun initView() {
if (hasMaximumSdk(Build.VERSION_CODES.JELLY_BEAN_MR1)) {
setLayerType(LAYER_TYPE_SOFTWARE, null)
}
}

private fun resetPath() = path.run {
reset()
addRoundRect(rect, outerRadii, Path.Direction.CW)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ package com.andrefrsousa.superbottomsheet
import android.annotation.SuppressLint
import android.app.Dialog
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.ShapeDrawable
import android.os.Build
import android.os.Bundle
import android.support.annotation.CallSuper
Expand All @@ -43,9 +41,7 @@ import android.view.*

abstract class SuperBottomSheetFragment : BottomSheetDialogFragment() {

private lateinit var backgroundDrawable: ShapeDrawable
private lateinit var backgroundShape: SheetRoundRectShape
private lateinit var bottomSheet: CornerRadiusFrameLayout
private lateinit var sheetContainer: CornerRadiusFrameLayout
private lateinit var behavior: BottomSheetBehavior<*>

// Customizable properties
Expand Down Expand Up @@ -123,37 +119,35 @@ abstract class SuperBottomSheetFragment : BottomSheetDialogFragment() {
@UiThread
private fun iniBottomSheetUiComponents() {
// Store views references
bottomSheet = dialog.findViewById(R.id.super_bottom_sheet)
sheetContainer = dialog.findViewById(R.id.super_bottom_sheet)
val touchOutsideView = dialog.findViewById<View>(R.id.touch_outside)

// Hack to find the bottom sheet holder view
initBackgroundShape()
// Set the bottom sheet radius
sheetContainer.setBackgroundColor(getBackgroundColor())
sheetContainer.setCornerRadius(propertyCornerRadius)

// Load bottom sheet behaviour
behavior = BottomSheetBehavior.from(bottomSheet)

// Set background shape
bottomSheet.setBackgroundCompat(backgroundDrawable)
behavior = BottomSheetBehavior.from(sheetContainer)

// Set tablet sheet width when in landscape. This will avoid full bleed sheet
if (context.isTablet() && !context.isInPortrait()) {
val layoutParams = bottomSheet.layoutParams
val layoutParams = sheetContainer.layoutParams
layoutParams.width = resources.getDimensionPixelSize(R.dimen.super_bottom_sheet_width)
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
bottomSheet.layoutParams = layoutParams
sheetContainer.layoutParams = layoutParams
}

// If is always expanded, there is no need to set the peek height
if (!propertyIsAlwaysExpanded) {
behavior.peekHeight = getPeekHeight()

bottomSheet.run {
sheetContainer.run {
minimumHeight = behavior.peekHeight
}
} else {
val layoutParams = bottomSheet.layoutParams
val layoutParams = sheetContainer.layoutParams
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
bottomSheet.layoutParams = layoutParams
sheetContainer.layoutParams = layoutParams
}

// Only skip the collapse state when the device is in landscape or the sheet is always expanded
Expand All @@ -165,17 +159,17 @@ abstract class SuperBottomSheetFragment : BottomSheetDialogFragment() {
setStatusBarColor(Color.TRANSPARENT)

// Load content container height
bottomSheet.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
sheetContainer.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
if (bottomSheet.height > 0) {
bottomSheet.viewTreeObserver.removeOnPreDrawListener(this)
if (sheetContainer.height > 0) {
sheetContainer.viewTreeObserver.removeOnPreDrawListener(this)

// If the content sheet is expanded set the background and status bar properties
if (bottomSheet.height == touchOutsideView.height) {
if (sheetContainer.height == touchOutsideView.height) {
setStatusBarColor(0f)

if (propertyAnimateCornerRadius) {
setBackgroundShapeRadius(0f)
sheetContainer.setCornerRadius(0f)
}
}
}
Expand All @@ -195,6 +189,11 @@ abstract class SuperBottomSheetFragment : BottomSheetDialogFragment() {
}

override fun onSlide(bottomSheet: View, slideOffset: Float) {
if (propertyAnimateCornerRadius) {
val radius = propertyCornerRadius - (propertyCornerRadius * slideOffset)
sheetContainer.setCornerRadius(radius)
}

if (!canSetStatusBarColor) {
return
}
Expand All @@ -211,11 +210,6 @@ abstract class SuperBottomSheetFragment : BottomSheetDialogFragment() {

val invertOffset = 1 - (1 * slideOffset)
setStatusBarColor(invertOffset)

if (propertyAnimateCornerRadius) {
val radius = propertyCornerRadius - (propertyCornerRadius * slideOffset)
setBackgroundShapeRadius(radius)
}
}
})
}
Expand All @@ -237,30 +231,6 @@ abstract class SuperBottomSheetFragment : BottomSheetDialogFragment() {

//endregion

//region BACKGROUND

private fun initBackgroundShape() {
backgroundShape = SheetRoundRectShape(propertyCornerRadius)
backgroundDrawable = ShapeDrawable(backgroundShape)

backgroundDrawable.paint.run {
color = getBackgroundColor()
style = Paint.Style.FILL
isAntiAlias = true
flags = Paint.ANTI_ALIAS_FLAG
}

bottomSheet.setCornerRadius(propertyCornerRadius)
}

private fun setBackgroundShapeRadius(radius: Float) {
backgroundShape.setRadius(radius)
backgroundDrawable.shape = backgroundShape
bottomSheet.setCornerRadius(radius)
}

//endregion

//region PUBLIC

@Dimension
Expand Down
2 changes: 2 additions & 0 deletions lib/src/main/java/com/andrefrsousa/superbottomsheet/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import android.support.annotation.ColorInt

internal fun hasMinimumSdk(minimumSdk: Int) = Build.VERSION.SDK_INT >= minimumSdk

internal fun hasMaximumSdk(maximumSdk: Int) = Build.VERSION.SDK_INT <= maximumSdk

@ColorInt
internal fun calculateColor(@ColorInt to: Int, ratio: Float): Int {
val alpha = (MAX_ALPHA - (MAX_ALPHA * ratio)).toInt()
Expand Down

0 comments on commit b61190c

Please sign in to comment.