From 4a8eeba10a379224af0e6146576ea3c86df4176a Mon Sep 17 00:00:00 2001 From: Rinat Nurmukhametov Date: Thu, 7 Oct 2021 17:35:18 +0300 Subject: [PATCH 1/2] moved the method to extension --- .../core/FullscreenBottomSheetDialog.kt | 17 ++--------------- utils/build.gradle | 7 +++++++ .../components/utils/ViewExtensions.kt | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/mvi-arch/src/main/java/ru/touchin/roboswag/mvi_arch/core/FullscreenBottomSheetDialog.kt b/mvi-arch/src/main/java/ru/touchin/roboswag/mvi_arch/core/FullscreenBottomSheetDialog.kt index 068c5ca..a9388c2 100644 --- a/mvi-arch/src/main/java/ru/touchin/roboswag/mvi_arch/core/FullscreenBottomSheetDialog.kt +++ b/mvi-arch/src/main/java/ru/touchin/roboswag/mvi_arch/core/FullscreenBottomSheetDialog.kt @@ -2,7 +2,6 @@ package ru.touchin.roboswag.mvi_arch.core import android.app.Dialog import android.content.Context -import android.content.DialogInterface import android.os.Bundle import android.os.Parcelable import android.view.LayoutInflater @@ -16,10 +15,9 @@ import androidx.core.os.bundleOf import androidx.lifecycle.Observer import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider -import com.google.android.material.bottomsheet.BottomSheetBehavior -import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialogFragment import ru.touchin.mvi_arch.R +import ru.touchin.roboswag.components.utils.getResizableShowListener import ru.touchin.roboswag.mvi_arch.di.ViewModelAssistedFactory import ru.touchin.roboswag.mvi_arch.di.ViewModelFactory import ru.touchin.roboswag.mvi_arch.marker.ViewAction @@ -54,17 +52,6 @@ abstract class FullscreenBottomSheetDialog( ).get(ViewModel::class.java) } - private val onShowListener by lazy { - DialogInterface.OnShowListener { dialog -> - (dialog as BottomSheetDialog).findViewById(com.google.android.material.R.id.design_bottom_sheet) - ?.let { BottomSheetBehavior.from(it) } - ?.apply { - state = BottomSheetBehavior.STATE_EXPANDED - skipCollapsed = true - } - } - } - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setStyle(STYLE_NORMAL, R.style.RoundedCornersBottomSheetDialogTheme) @@ -102,7 +89,7 @@ abstract class FullscreenBottomSheetDialog( } override fun setupDialog(dialog: Dialog, style: Int) { - dialog.setOnShowListener(onShowListener) + dialog.setOnShowListener(getResizableShowListener()) } override fun onSaveInstanceState(outState: Bundle) { diff --git a/utils/build.gradle b/utils/build.gradle index 2a490af..fe6fd6a 100644 --- a/utils/build.gradle +++ b/utils/build.gradle @@ -4,6 +4,7 @@ dependencies { implementation project(':kotlin-extensions') implementation "androidx.core:core" implementation "androidx.annotation:annotation" + implementation "com.google.android.material:material" constraints { implementation("androidx.core:core") { @@ -17,5 +18,11 @@ dependencies { require '1.1.0' } } + + implementation("com.google.android.material:material") { + version { + require '1.2.0-rc01' + } + } } } diff --git a/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt b/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt index 9c65ac7..ac3b31a 100644 --- a/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt +++ b/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt @@ -2,9 +2,14 @@ package ru.touchin.roboswag.components.utils import android.app.Activity import android.content.Context +import android.content.DialogInterface import android.content.res.Resources import android.view.View import android.view.inputmethod.InputMethodManager +import android.widget.FrameLayout +import com.google.android.material.bottomsheet.BottomSheetBehavior +import com.google.android.material.bottomsheet.BottomSheetDialog +import com.google.android.material.bottomsheet.BottomSheetDialogFragment /** * Returns string representation of [View]'s ID. @@ -46,3 +51,16 @@ fun View.showSoftInput() { val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) } + +/** + * Returns listener for BottomSheetDialogFragment so that dialog is shifted when the keyboard is opened + */ + +fun BottomSheetDialogFragment.getResizableShowListener() = DialogInterface.OnShowListener { dialog -> + (dialog as BottomSheetDialog).findViewById(com.google.android.material.R.id.design_bottom_sheet) + ?.let { BottomSheetBehavior.from(it) } + ?.apply { + state = BottomSheetBehavior.STATE_EXPANDED + skipCollapsed = true + } +} From 862ab1545078daf215821cf2375a8c04114c741a Mon Sep 17 00:00:00 2001 From: Rinat Nurmukhametov Date: Thu, 7 Oct 2021 18:08:42 +0300 Subject: [PATCH 2/2] refactor extension --- .../core/FullscreenBottomSheetDialog.kt | 4 ++-- .../components/utils/ViewExtensions.kt | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/mvi-arch/src/main/java/ru/touchin/roboswag/mvi_arch/core/FullscreenBottomSheetDialog.kt b/mvi-arch/src/main/java/ru/touchin/roboswag/mvi_arch/core/FullscreenBottomSheetDialog.kt index a9388c2..4735f76 100644 --- a/mvi-arch/src/main/java/ru/touchin/roboswag/mvi_arch/core/FullscreenBottomSheetDialog.kt +++ b/mvi-arch/src/main/java/ru/touchin/roboswag/mvi_arch/core/FullscreenBottomSheetDialog.kt @@ -17,7 +17,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import com.google.android.material.bottomsheet.BottomSheetDialogFragment import ru.touchin.mvi_arch.R -import ru.touchin.roboswag.components.utils.getResizableShowListener +import ru.touchin.roboswag.components.utils.setResizableListener import ru.touchin.roboswag.mvi_arch.di.ViewModelAssistedFactory import ru.touchin.roboswag.mvi_arch.di.ViewModelFactory import ru.touchin.roboswag.mvi_arch.marker.ViewAction @@ -89,7 +89,7 @@ abstract class FullscreenBottomSheetDialog( } override fun setupDialog(dialog: Dialog, style: Int) { - dialog.setOnShowListener(getResizableShowListener()) + dialog.setResizableListener() } override fun onSaveInstanceState(outState: Bundle) { diff --git a/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt b/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt index ac3b31a..942facf 100644 --- a/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt +++ b/utils/src/main/java/ru/touchin/roboswag/components/utils/ViewExtensions.kt @@ -1,6 +1,7 @@ package ru.touchin.roboswag.components.utils import android.app.Activity +import android.app.Dialog import android.content.Context import android.content.DialogInterface import android.content.res.Resources @@ -55,12 +56,13 @@ fun View.showSoftInput() { /** * Returns listener for BottomSheetDialogFragment so that dialog is shifted when the keyboard is opened */ - -fun BottomSheetDialogFragment.getResizableShowListener() = DialogInterface.OnShowListener { dialog -> - (dialog as BottomSheetDialog).findViewById(com.google.android.material.R.id.design_bottom_sheet) - ?.let { BottomSheetBehavior.from(it) } - ?.apply { - state = BottomSheetBehavior.STATE_EXPANDED - skipCollapsed = true - } +fun Dialog.setResizableListener() { + setOnShowListener { dialog -> + (dialog as BottomSheetDialog).findViewById(com.google.android.material.R.id.design_bottom_sheet) + ?.let { BottomSheetBehavior.from(it) } + ?.apply { + state = BottomSheetBehavior.STATE_EXPANDED + skipCollapsed = true + } + } }