Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamdeveloper-lopez/247b5f5180a53a372ecb8a0ceca1aa18 to your computer and use it in GitHub Desktop.
Save iamdeveloper-lopez/247b5f5180a53a372ecb8a0ceca1aa18 to your computer and use it in GitHub Desktop.
abstract class BaseBottomSheetDialogFragment : BottomSheetDialogFragment() {
@LayoutRes
protected abstract fun layoutRes(): Int
protected abstract fun onViewCreated()
private var sheetBehavior: BottomSheetBehavior<*>? = null
private var peekHeight = 0
private var expandedOffset = 0
private var halfExpandedEnabled = false
private var isFitToContents = true
private var isDraggable = true
private var hideable = true
private var cancelable = true
private var canceledOnTouchOutside = true
private var skipCollapsed = true
private var fullScreen = false
private var dimAmount = 0.6f //Default dim amount
private var bottomSheetCallback: BottomSheetBehavior.BottomSheetCallback? = null
fun setFullScreen(fullScreen: Boolean) {
this.fullScreen = fullScreen
}
fun setPeekHeight(peekHeight: Int) {
this.peekHeight = peekHeight
}
fun setExpandedOffset(expandedOffset: Int) {
this.expandedOffset = expandedOffset
}
fun setHalfExpandedEnabled(halfExpandedEnabled: Boolean) {
this.halfExpandedEnabled = halfExpandedEnabled
}
fun setDimAmount(@FloatRange dimAmount: Float) {
this.dimAmount = dimAmount
}
fun setFitToContents(fitToContents: Boolean) {
isFitToContents = fitToContents
}
fun setHideable(hideable: Boolean) {
this.hideable = hideable
}
fun setDraggable(draggable: Boolean) {
isDraggable = draggable
}
override fun setCancelable(cancelable: Boolean) {
this.cancelable = cancelable
}
fun setCanceledOnTouchOutside(canceledOnTouchOutside: Boolean) {
this.canceledOnTouchOutside = canceledOnTouchOutside
}
fun setSkipCollapsed(skipCollapsed: Boolean) {
this.skipCollapsed = skipCollapsed
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(layoutRes(), container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
onViewCreated()
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val dialog = dialog as BottomSheetDialog?
if (dialog != null) {
if (dimAmount != 0.6f) {
val window = dialog.window
val windowParams = window?.attributes
windowParams?.dimAmount = dimAmount
Log.d("DIM", "amount: " + windowParams?.dimAmount)
windowParams?.flags =
windowParams?.flags?.or(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
window?.attributes = windowParams
}
dialog.setCancelable(cancelable)
dialog.setCanceledOnTouchOutside(canceledOnTouchOutside)
dialog.setOnShowListener(DialogInterface.OnShowListener {
sheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetListener?.onShow()
})
dialog.setOnCancelListener(DialogInterface.OnCancelListener {
dismiss()
dialog.dismiss()
bottomSheetListener?.onHide()
})
dialog.setOnDismissListener(DialogInterface.OnDismissListener {
dismiss()
dialog.dismiss()
bottomSheetListener?.onHide()
})
val bottomSheetView =
dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
if (bottomSheetView != null) {
if (bottomSheetView.layoutParams != null && fullScreen) {
bottomSheetView.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
}
sheetBehavior = BottomSheetBehavior.from(bottomSheetView)
if (halfExpandedEnabled) {
sheetBehavior?.halfExpandedRatio = 0.6f
sheetBehavior?.setFitToContents(false)
} else {
sheetBehavior?.setFitToContents(isFitToContents)
}
if (peekHeight != 0) {
sheetBehavior?.peekHeight = peekHeight
}
if (expandedOffset != 0) {
sheetBehavior?.setExpandedOffset(expandedOffset)
}
sheetBehavior?.isHideable = hideable
sheetBehavior?.skipCollapsed = skipCollapsed
sheetBehavior?.addBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
bottomSheetCallback?.onStateChanged(bottomSheet, newState)
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
bottomSheetListener?.onHide()
dismiss()
dialog.dismiss()
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {
bottomSheetCallback?.onSlide(bottomSheet, slideOffset)
}
})
}
}
}
fun hideUponCall() {
if (sheetBehavior != null) {
sheetBehavior!!.state = BottomSheetBehavior.STATE_HIDDEN
if (bottomSheetListener != null) {
bottomSheetListener!!.onHide()
}
dismiss()
if (dialog != null) {
val dialog = dialog as BottomSheetDialog?
dialog!!.dismiss()
}
}
}
fun hideUponCall(changed: Boolean) {
if (sheetBehavior != null) {
sheetBehavior!!.state = BottomSheetBehavior.STATE_HIDDEN
if (bottomSheetListener != null) {
bottomSheetListener!!.onHide(changed)
}
dismiss()
}
}
private var bottomSheetListener: BottomSheetListener? = null
fun setBottomSheetListener(bottomSheetListener: BottomSheetListener?) {
this.bottomSheetListener = bottomSheetListener
}
class SimpleBottomSheetListener : BottomSheetListener {
override fun onShow() {}
override fun onHide() {}
override fun onHide(changed: Boolean) {}
}
interface BottomSheetListener {
fun onShow()
fun onHide()
fun onHide(changed: Boolean)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment