Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.

Commit

Permalink
Fix reusability of bottom sheet dialogs. Resolves #1814.
Browse files Browse the repository at this point in the history
  • Loading branch information
afollestad committed Jun 8, 2019
1 parent 500e7be commit ae8bd19
Showing 1 changed file with 29 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.graphics.drawable.GradientDrawable
import android.os.Build.VERSION.SDK_INT
import android.view.LayoutInflater
import android.view.View.GONE
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager.LayoutParams
Expand All @@ -36,6 +37,7 @@ import com.afollestad.materialdialogs.internal.main.DialogLayout
import com.afollestad.materialdialogs.utils.MDUtil.getWidthAndHeight
import com.afollestad.materialdialogs.utils.MDUtil.waitForHeight
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_COLLAPSED
import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_HIDDEN
import kotlin.math.min
import kotlin.properties.Delegates.notNull
Expand Down Expand Up @@ -133,22 +135,11 @@ class BottomSheet(
onHide = {
buttonsLayout?.visibility = GONE
dialog?.dismiss()
dialog = null
}
)

bottomSheetView!!.waitForHeight {
actualPeekHeight = min(defaultPeekHeight, min(this.measuredHeight, defaultPeekHeight))
bottomSheetBehavior?.animatePeekHeight(
view = bottomSheetView!!,
start = 0,
dest = actualPeekHeight,
duration = LAYOUT_PEEK_CHANGE_DURATION_MS,
onEnd = {
invalidateDividers(actualPeekHeight)
}
)
showButtons()
}
}

Expand Down Expand Up @@ -217,6 +208,21 @@ class BottomSheet(
// Clicking outside the bottom sheet dismisses the dialog
rootView?.setOnClickListener { this.dialog?.dismiss() }
}

bottomSheetView!!.waitForHeight {
bottomSheetBehavior?.peekHeight = 0
bottomSheetBehavior?.state = STATE_COLLAPSED
bottomSheetBehavior?.animatePeekHeight(
view = bottomSheetView!!,
start = 0,
dest = actualPeekHeight,
duration = LAYOUT_PEEK_CHANGE_DURATION_MS,
onEnd = {
invalidateDividers(actualPeekHeight)
}
)
showButtons()
}
}

override fun onPostShow(dialog: MaterialDialog) = Unit
Expand All @@ -227,25 +233,19 @@ class BottomSheet(
bottomSheetBehavior!!.state != STATE_HIDDEN
) {
bottomSheetBehavior!!.state = STATE_HIDDEN
hideButtons { cleanup() }
hideButtons()
return true
}
return false
}

private fun cleanup() {
bottomSheetBehavior = null
bottomSheetView = null
buttonsLayout = null
rootView = null
}

private fun showButtons() {
if (!buttonsLayout.shouldBeVisible()) {
return
}
val start = buttonsLayout!!.measuredHeight
buttonsLayout?.translationY = start.toFloat()
buttonsLayout?.visibility = VISIBLE
val animator = animateValues(
from = start,
to = 0,
Expand All @@ -259,20 +259,16 @@ class BottomSheet(
}
}

private fun hideButtons(onEnd: () -> Unit) {
if (buttonsLayout.shouldBeVisible()) {
val animator = animateValues(
from = 0,
to = buttonsLayout!!.measuredHeight,
duration = LAYOUT_PEEK_CHANGE_DURATION_MS,
onUpdate = { buttonsLayout?.translationY = it.toFloat() },
onEnd = onEnd
)
buttonsLayout?.onDetach { animator.cancel() }
animator.start()
return
}
onEnd()
private fun hideButtons() {
if (!buttonsLayout.shouldBeVisible()) return
val animator = animateValues(
from = 0,
to = buttonsLayout!!.measuredHeight,
duration = LAYOUT_PEEK_CHANGE_DURATION_MS,
onUpdate = { buttonsLayout?.translationY = it.toFloat() }
)
buttonsLayout?.onDetach { animator.cancel() }
animator.start()
}

companion object {
Expand Down

0 comments on commit ae8bd19

Please sign in to comment.