From 9111a6eec716f09dc126f5323dfd7bc9edd54338 Mon Sep 17 00:00:00 2001 From: Alexander Capehart Date: Tue, 11 Jul 2023 15:39:58 -0600 Subject: [PATCH] ui: tweak bottom sheet dialog transitions Use a native slide animation for entering/editing bottom sheet dialogs, which looks a good amount nicer overall. --- .../ViewBindingBottomSheetDialogFragment.kt | 43 +++++++++++++++---- .../main/res/anim/bottom_sheet_slide_in.xml | 25 +++++++++++ .../main/res/anim/bottom_sheet_slide_out.xml | 10 +++++ app/src/main/res/values/styles_ui.xml | 8 +++- 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 app/src/main/res/anim/bottom_sheet_slide_in.xml create mode 100644 app/src/main/res/anim/bottom_sheet_slide_out.xml diff --git a/app/src/main/java/org/oxycblt/auxio/ui/ViewBindingBottomSheetDialogFragment.kt b/app/src/main/java/org/oxycblt/auxio/ui/ViewBindingBottomSheetDialogFragment.kt index 2072d62e7..99be86b6f 100644 --- a/app/src/main/java/org/oxycblt/auxio/ui/ViewBindingBottomSheetDialogFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/ui/ViewBindingBottomSheetDialogFragment.kt @@ -18,13 +18,16 @@ package org.oxycblt.auxio.ui +import android.content.Context import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.appcompat.app.AlertDialog +import androidx.annotation.StyleRes import androidx.fragment.app.DialogFragment import androidx.viewbinding.ViewBinding +import com.google.android.material.bottomsheet.BottomSheetBehavior +import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialogFragment import org.oxycblt.auxio.util.logD import org.oxycblt.auxio.util.unlikelyToBeNull @@ -39,13 +42,8 @@ abstract class ViewBindingBottomSheetDialogFragment : BottomSheetDialogFragment() { private var _binding: VB? = null - /** - * Configure the [AlertDialog.Builder] during [onCreateDialog]. - * - * @param builder The [AlertDialog.Builder] to configure. - * @see onCreateDialog - */ - protected open fun onConfigDialog(builder: AlertDialog.Builder) {} + override fun onCreateDialog(savedInstanceState: Bundle?): BottomSheetDialog = + RealAnimationBottomSheetDialog(requireContext(), theme) /** * Inflate the [ViewBinding] during [onCreateView]. @@ -108,4 +106,33 @@ abstract class ViewBindingBottomSheetDialogFragment : _binding = null logD("Fragment destroyed") } + + private inner class RealAnimationBottomSheetDialog + @JvmOverloads + constructor(context: Context, @StyleRes theme: Int = 0) : BottomSheetDialog(context, theme) { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + // The dialog already supplies an implementation for dismissing with the normal + // bottom sheet sliding, which is odd. It works well save the scrim not actually + // activating until the sheet is out of view, but that is tolerated for now. + // TODO: Replace with custom impl that runs the scrim animation and bottom sheet + // animation in parallel. Might just switch back to the stock animation if I can + // eliminate the opacity. + dismissWithAnimation = true + } + + override fun onStart() { + super.onStart() + // We have to manually trigger a hidden -> expanded transition when the dialog + // is initially opened. Hence, we set the state to hidden now and then as soon + // as we're drawing updating it to expanded. + behavior.state = BottomSheetBehavior.STATE_HIDDEN + requireView().post { behavior.state = BottomSheetBehavior.STATE_EXPANDED } + } + + override fun dismiss() { + super.dismiss() + behavior.state = BottomSheetBehavior.STATE_HIDDEN + } + } } diff --git a/app/src/main/res/anim/bottom_sheet_slide_in.xml b/app/src/main/res/anim/bottom_sheet_slide_in.xml new file mode 100644 index 000000000..b2aa8afdb --- /dev/null +++ b/app/src/main/res/anim/bottom_sheet_slide_in.xml @@ -0,0 +1,25 @@ + + + + + + + diff --git a/app/src/main/res/anim/bottom_sheet_slide_out.xml b/app/src/main/res/anim/bottom_sheet_slide_out.xml new file mode 100644 index 000000000..b8ade7731 --- /dev/null +++ b/app/src/main/res/anim/bottom_sheet_slide_out.xml @@ -0,0 +1,10 @@ + + + + + + diff --git a/app/src/main/res/values/styles_ui.xml b/app/src/main/res/values/styles_ui.xml index 0f5675812..98228e1aa 100644 --- a/app/src/main/res/values/styles_ui.xml +++ b/app/src/main/res/values/styles_ui.xml @@ -50,7 +50,13 @@ + +