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

BottomSheet bugfixes (crash, animation) #2077

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions bottomsheets/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
ext.module_group = "com.afollestad.material-dialogs"
ext.module_name = "bottomsheets"

apply from: rootProject.file("gradle/android_library_config.gradle")
apply from: project.file("../gradle/android_library_config.gradle")

dependencies {
api project(':core')
api project(':md:core')

implementation deps.kotlin.stdlib8
implementation deps.google_material
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ 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 com.google.android.material.bottomsheet.BottomSheetBehavior.*
import kotlin.math.min
import kotlin.properties.Delegates.notNull

/** @author Aidan Follestad (@afollestad) */
class BottomSheet(
private val layoutMode: LayoutMode = MATCH_PARENT
private val layoutMode: LayoutMode = MATCH_PARENT,
private val initialState: Int = STATE_HALF_EXPANDED,
private val animateInitialState: Boolean = true
) : DialogBehavior {
internal var bottomSheetBehavior: BottomSheetBehavior<ViewGroup>? = null
private lateinit var bottomSheetView: ViewGroup
Expand All @@ -56,6 +57,7 @@ class BottomSheet(
internal var defaultPeekHeight: Int by notNull()
internal var maxPeekHeight: Int = -1
private var actualPeekHeight: Int by notNull()
private var actualFullHeight: Int by notNull()

override fun getThemeRes(isDark: Boolean): Int {
return if (isDark) {
Expand All @@ -73,9 +75,9 @@ class BottomSheet(
dialog: MaterialDialog
): ViewGroup {
rootView = layoutInflater.inflate(
R.layout.md_dialog_base_bottomsheet,
null,
false
R.layout.md_dialog_base_bottomsheet,
null,
false
) as CoordinatorLayout

this.dialog = dialog
Expand All @@ -86,12 +88,13 @@ class BottomSheet(
defaultPeekHeight = (windowHeight * DEFAULT_PEEK_HEIGHT_RATIO).toInt()
actualPeekHeight = defaultPeekHeight
maxPeekHeight = windowHeight
actualFullHeight = windowHeight

setupBottomSheetBehavior()
if (creatingContext is Activity) {
carryOverWindowFlags(
dialogWindow = dialogWindow,
creatingActivity = creatingContext
dialogWindow = dialogWindow,
creatingActivity = creatingContext
)
}

Expand All @@ -110,33 +113,42 @@ class BottomSheet(

private fun setupBottomSheetBehavior() {
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetView)
.apply {
isHideable = true
.apply {
isHideable = true

if (animateInitialState) {
// start at 0 so we can animate it up when the dialog lays out the view
peekHeight = 0

setCallbacks(
onSlide = { currentHeight ->
// Slide the buttons layout down as the bottom sheet is hiding itself
val buttonsLayoutHeight = buttonsLayout.measuredHeight
if (currentHeight in 1..buttonsLayoutHeight) {
val diff = buttonsLayoutHeight - currentHeight
buttonsLayout.translationY = diff.toFloat()
} else if (currentHeight > 0) {
buttonsLayout.translationY = 0f
}
// Show divider over buttons layout if sheet is sliding down
invalidateDividers(currentHeight)
},
onHide = {
buttonsLayout.visibility = GONE
dialog?.dismiss()
}
)
state = STATE_COLLAPSED
} else {
peekHeight = defaultPeekHeight
state = initialState
}

setCallbacks(
onSlide = { currentHeight ->
// Slide the buttons layout down as the bottom sheet is hiding itself
val buttonsLayoutHeight = buttonsLayout.measuredHeight
if (currentHeight in 1..buttonsLayoutHeight) {
val diff = buttonsLayoutHeight - currentHeight
buttonsLayout.translationY = diff.toFloat()
} else if (currentHeight > 0) {
buttonsLayout.translationY = 0f
}
// Show divider over buttons layout if sheet is sliding down
// Show divider over buttons layout if sheet is sliding down
invalidateDividers(currentHeight)
},
onHide = {
buttonsLayout.visibility = GONE
dialog?.dismiss()
}
)
}

bottomSheetView.waitForHeight {
actualPeekHeight = min(defaultPeekHeight, min(this.measuredHeight, defaultPeekHeight))
actualFullHeight = this.measuredHeight
}
}

Expand Down Expand Up @@ -172,11 +184,11 @@ class BottomSheet(
}
window.setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
val lp = LayoutParams()
.apply {
copyFrom(window.attributes)
width = LayoutParams.MATCH_PARENT
height = LayoutParams.MATCH_PARENT
}
.apply {
copyFrom(window.attributes)
width = LayoutParams.MATCH_PARENT
height = LayoutParams.MATCH_PARENT
}
window.attributes = lp
}

Expand All @@ -187,10 +199,10 @@ class BottomSheet(
) {
bottomSheetView.background = GradientDrawable().apply {
this.cornerRadii = floatArrayOf(
cornerRadius, cornerRadius, // top left
cornerRadius, cornerRadius, // top right
0f, 0f, // bottom left
0f, 0f // bottom right
cornerRadius, cornerRadius, // top left
cornerRadius, cornerRadius, // top right
0f, 0f, // bottom left
0f, 0f // bottom right
)
setColor(color)
}
Expand All @@ -207,31 +219,34 @@ class BottomSheet(
bottomSheetBehavior!!.isHideable = false
}

bottomSheetView.waitForHeight {
bottomSheetBehavior?.apply {
peekHeight = 0
state = STATE_COLLAPSED
animatePeekHeight(
if (animateInitialState) {
bottomSheetView.waitForHeight {
bottomSheetBehavior?.apply {
peekHeight = 0
state = initialState
val targetHeight = if (initialState == STATE_HALF_EXPANDED) actualPeekHeight else actualFullHeight
animateViewHeight(
view = bottomSheetView,
start = 0,
dest = actualPeekHeight,
dest = targetHeight,
duration = LAYOUT_PEEK_CHANGE_DURATION_MS,
onEnd = {
invalidateDividers(actualPeekHeight)
invalidateDividers(targetHeight)
}
)
)
}
}
showButtons()
}
showButtons()
}

override fun onPostShow(dialog: MaterialDialog) = Unit

override fun onDismiss(): Boolean {
val sheetBehavior = bottomSheetBehavior
if (dialog != null &&
sheetBehavior != null &&
sheetBehavior.state != STATE_HIDDEN
sheetBehavior != null &&
sheetBehavior.state != STATE_HIDDEN
) {
sheetBehavior.apply {
isHideable = true
Expand All @@ -253,10 +268,10 @@ class BottomSheet(
visibility = VISIBLE
}
val animator = animateValues(
from = start,
to = 0,
duration = BUTTONS_SHOW_DURATION_MS,
onUpdate = { buttonsLayout.translationY = it.toFloat() }
from = start,
to = 0,
duration = BUTTONS_SHOW_DURATION_MS,
onUpdate = { buttonsLayout.translationY = it.toFloat() }
)
buttonsLayout.onDetach { animator.cancel() }
animator.apply {
Expand All @@ -268,10 +283,10 @@ class BottomSheet(
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() }
from = 0,
to = buttonsLayout.measuredHeight,
duration = LAYOUT_PEEK_CHANGE_DURATION_MS,
onUpdate = { buttonsLayout.translationY = it.toFloat() }
)
buttonsLayout.onDetach { animator.cancel() }
animator.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fun MaterialDialog.setPeekHeight(
bottomSheet.defaultPeekHeight = destinationPeekHeight
val bottomSheetBehavior = bottomSheet.bottomSheetBehavior
if (isShowing) {
bottomSheetBehavior?.animatePeekHeight(
bottomSheetBehavior?.animateViewHeight(
view = this.view,
dest = destinationPeekHeight,
duration = LAYOUT_PEEK_CHANGE_DURATION_MS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal fun BottomSheetBehavior<*>.setCallbacks(
})
}

internal fun BottomSheetBehavior<*>.animatePeekHeight(
internal fun BottomSheetBehavior<*>.animateViewHeight(
view: View,
start: Int = peekHeight,
dest: Int,
Expand Down
4 changes: 2 additions & 2 deletions color/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
ext.module_group = "com.afollestad.material-dialogs"
ext.module_name = "color"

apply from: rootProject.file("gradle/android_library_config.gradle")
apply from: project.file("../gradle/android_library_config.gradle")

dependencies {
api project(':core')
api project(':md:core')

implementation deps.kotlin.stdlib8
implementation deps.afollestad.dots_indicator
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext.module_group = "com.afollestad.material-dialogs"
ext.module_name = "core"

apply from: rootProject.file("gradle/android_library_config.gradle")
apply from: project.file("../gradle/android_library_config.gradle")

dependencies {
api deps.androidx.core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package com.afollestad.materialdialogs

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.graphics.Color.TRANSPARENT
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.TypedValue
import android.util.TypedValue.COMPLEX_UNIT_DIP
import android.view.LayoutInflater
Expand Down Expand Up @@ -397,6 +399,9 @@ class MaterialDialog(
override fun dismiss() {
if (dialogBehavior.onDismiss()) return
hideKeyboard()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && windowContext is Activity && windowContext.isDestroyed) {
return
}
super.dismiss()
}

Expand Down
4 changes: 2 additions & 2 deletions datetime/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
ext.module_group = "com.afollestad.material-dialogs"
ext.module_name = "datetime"

apply from: rootProject.file("gradle/android_library_config.gradle")
apply from: project.file("../gradle/android_library_config.gradle")

dependencies {
api project(':core')
api project(':md:core')
api deps.afollestad.date_picker
compileOnly deps.androidx.annotations

Expand Down
5 changes: 2 additions & 3 deletions dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
ext.versions = [
min_sdk: 16,
compile_sdk: 29,
build_tools: "29.0.0",
publish_version: "3.3.0",
publish_version_code: 262
]

ext.deps = [
gradle_plugins: [
android: "com.android.tools.build:gradle:4.1.2",
kotlin: "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61",
android: "com.android.tools.build:gradle:7.0.1",
kotlin: "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30",
spotless: "com.diffplug.spotless:spotless-plugin-gradle:3.27.1",
versions: "com.github.ben-manes:gradle-versions-plugin:0.27.0"
],
Expand Down
6 changes: 3 additions & 3 deletions files/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
ext.module_group = "com.afollestad.material-dialogs"
ext.module_name = "files"

apply from: rootProject.file("gradle/android_library_config.gradle")
apply from: project.file("../gradle/android_library_config.gradle")

dependencies {
api project(':core')
api project(':input')
api project(':md:core')
api project(':md:input')

implementation deps.kotlin.coroutines.android
implementation deps.kotlin.stdlib8
Expand Down
2 changes: 1 addition & 1 deletion gradle/android_application_config.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apply plugin: "com.android.application"
apply from: rootProject.file("gradle/android_common_config.gradle")
apply from: project.file("../gradle/android_common_config.gradle")

if (module_package_id == null) {
throw new IllegalStateException("module_package_id is missing!")
Expand Down
5 changes: 2 additions & 3 deletions gradle/android_common_config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ ext.module_package_id = "${module_group}.${module_name}"
logger.info("Package ID: $module_package_id")

apply plugin: "kotlin-android"
apply from: rootProject.file("dependencies.gradle")
apply from: rootProject.file("gradle/spotless_plugin_config.gradle")
apply from: project.file("../dependencies.gradle")
//apply from: rootProject.file("gradle/spotless_plugin_config.gradle")

android {
compileSdkVersion versions.compile_sdk
buildToolsVersion versions.build_tools

compileOptions {
sourceCompatibility 1.8
Expand Down
9 changes: 7 additions & 2 deletions gradle/android_library_config.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
apply plugin: "com.android.library"

apply from: rootProject.file("gradle/android_common_config.gradle")
apply from: rootProject.file("gradle/android_publish_mavencentral.gradle")
apply from: project.file("../gradle/android_common_config.gradle")

// maven
//apply from: project.file("../gradle/android_publish_mavencentral.gradle")

// jitpack
apply from: project.file("../gradle/android_publish_jitpack.gradle")
10 changes: 10 additions & 0 deletions gradle/android_publish_jitpack.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apply plugin: 'maven-publish'
project.afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
}
}
}
}