Skip to content

Instantly share code, notes, and snippets.

@VitalyPeryatin
Created June 6, 2022 07:25
Show Gist options
  • Save VitalyPeryatin/22e6995c287232c214106a145fcc9c13 to your computer and use it in GitHub Desktop.
Save VitalyPeryatin/22e6995c287232c214106a145fcc9c13 to your computer and use it in GitHub Desktop.
Dialog in compose
import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
abstract class BaseBottomSheetDialog : BottomSheetDialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
dialog.setOnShowListener { currentDialog ->
val d = currentDialog as BottomSheetDialog
val bottomSheet = d.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout
BottomSheetBehavior.from<FrameLayout?>(bottomSheet).state =
BottomSheetBehavior.STATE_EXPANDED
}
dialog.behavior.skipCollapsed = true
return dialog
}
fun show(fragmentManager: FragmentManager) {
show(fragmentManager, tag)
}
fun showDialog(activity: FragmentActivity) {
if (!isAdded && !activity.isFinishing) {
show(activity.supportFragmentManager)
}
}
}
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.fragment.app.FragmentActivity
import com.afterglow.core_base.BaseBottomSheetDialog
import com.afterglow.core_base.clickableSingle
import com.afterglow.core_ui.SelectableItem
import com.afterglow.core_utils.extraNotNull
import com.afterglow.core_utils.setResult
import com.afterglow.feature_select_item_sheet_api.navigation.SelectItemSheetRouter
import com.afterglow.feature_select_item_sheet_impl.R
class SelectItemBottomDialog: BaseBottomSheetDialog() {
private val items: ArrayList<SelectableItem> by extraNotNull(EXTRA_ITEMS, arrayListOf())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NO_TITLE, R.style.SelectItemBottomSheetDialogTheme)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setContent {
MaterialTheme(
colors = if (isSystemInDarkTheme()) darkColors() else lightColors()
) {
...
}
}
}
}
companion object {
private const val EXTRA_ITEMS = "items"
fun newInstance(items: ArrayList<SelectableItem>): SelectItemBottomDialog {
return SelectItemBottomDialog().apply {
arguments = Bundle().apply {
putParcelableArrayList(EXTRA_ITEMS, items)
}
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="SelectItemBottomSheetDialogTheme" parent="@style/Theme.Design.Light.BottomSheetDialog">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@color/navigation_bar_color</item>
<item name="android:navigationBarDividerColor">@color/navigation_bar_color</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:colorBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">false</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment