Skip to content

Instantly share code, notes, and snippets.

@sigmadeltasoftware
Last active February 27, 2020 06:09
Show Gist options
  • Save sigmadeltasoftware/6abb4328fff763594588bffc81c031c8 to your computer and use it in GitHub Desktop.
Save sigmadeltasoftware/6abb4328fff763594588bffc81c031c8 to your computer and use it in GitHub Desktop.
ModalBottomSheetDialogFragment
package be.sigmadelta.template.common.presentation
import android.content.Context
import android.content.res.Configuration
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.FragmentManager
import be.sigmadelta.template.common.R
import be.sigmadelta.template.common.domain.util.ContainerActivity
import be.sigmadelta.template.common.domain.util.isTablet
import be.sigmadelta.template.common.domain.util.outLine
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
abstract class ModalBottomSheetDialogFragment : BottomSheetDialogFragment() {
open val isFullHeight: Boolean = false
override fun getTheme() = R.style.Theme_LFVP_BottomDialogTheme
override fun onStart() {
super.onStart()
dialog?.let { dialog ->
val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet)
bottomSheet?.layoutParams?.height = ViewGroup.LayoutParams.MATCH_PARENT
view?.post { view ->
(view.parent as? View)?.let { parent ->
val params = parent.layoutParams as? CoordinatorLayout.LayoutParams
when {
parent.isTablet -> {
parent.outLine(true)
params?.behavior = null
bottomSheet.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
view.resizeForTablets()
bottomSheet.requestLayout()
}
else -> {
parent.outLine(false)
val bottomSheetBehavior = params?.behavior as? BottomSheetBehavior<*>?
bottomSheetBehavior?.peekHeight = if (isFullHeight) view.measuredHeight else view.measuredHeight - view.resources.getDimensionPixelSize(R.dimen.modal_bottom_sheet_peek_size)
dialog.window?.attributes?.windowAnimations = R.style.Widget_LFVP_CommonDialogAnimations
}
}
}
}
}
}
private fun View.resizeForTablets() {
// Resize bottom sheet dialog so it doesn't span the entire width past a particular measurement
(activity?.getSystemService(Context.WINDOW_SERVICE) as? WindowManager)?.defaultDisplay
?.let { display ->
DisplayMetrics().also { display.getMetrics(it) }
}?.let { metrics ->
val margins = context?.resources?.getDimensionPixelSize(R.dimen.modal_bottom_sheet_margins)
?: 0
val marginsFactor = if (context.isLandscape) 4 else 1
val width = metrics.widthPixels - (margins * marginsFactor)
val height = metrics.heightPixels - margins
dialog?.window?.setLayout(width, height)
}
}
private val Context?.isLandscape: Boolean
get() = this?.resources?.configuration?.orientation == Configuration.ORIENTATION_LANDSCAPE
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
if (context?.isTablet == true) {
view?.resizeForTablets()
}
}
override fun onDestroyView() {
dialog?.setDismissMessage(null)
super.onDestroyView()
}
override fun show(manager: FragmentManager, tag: String?) {
if (!manager.isStateSaved) {
try {
super.show(manager, tag)
} catch (e: IllegalStateException) {
e.printStackTrace()
}
}
}
interface VisibilityListener {
fun onModalShown()
fun onModalDismissed()
}
}
/**
* Created by Bojan Belic on 26/09/2018
* Copyright © 2018 Sigma Delta Software Solutions - All rights reserved
*/
package be.sigmadelta.template.common.domain.util
import android.graphics.Outline
import android.view.View
import android.view.ViewOutlineProvider
import be.sigmadelta.template.common.R
fun View.outLine(shouldAlsoOutlineBottomCorners: Boolean,
radius: Float = resources.getDimension(R.dimen.default_outline_corner_radius)) {
outlineProvider = object: ViewOutlineProvider() {
override fun getOutline(p0: View?, outline: Outline?) {
if (shouldAlsoOutlineBottomCorners) {
outline?.setRoundRect(0, 0, width, height, radius / 2)
} else {
outline?.setRoundRect(0, 0, width, (height + radius).toInt(), radius)
}
}
}
clipToOutline = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment