Skip to content

Instantly share code, notes, and snippets.

View k0siara's full-sized avatar
🧐
Investigating a bug

Patryk Kosieradzki k0siara

🧐
Investigating a bug
View GitHub Profile
fun DialogFragment.dialogFragmentComposeView(
consumeWindowInsets: Boolean,
content: @Composable () -> Unit,
): View {
return DialogFragmentComposeView(
context = requireContext(),
dialogProvider = { dialog ?: Dialog(requireContext()) },
).apply {
consumeWindowInsets = consumeWindowInsets
layoutParams = ViewGroup.LayoutParams(
internal class DialogFragmentComposeView(
context: Context,
private val dialogProvider: () -> Dialog,
) : AbstractComposeView(context, null, 0),
DialogWindowProvider {
// The fix is here
override val window get() = dialogProvider().window!!
private val content = mutableStateOf<(@Composable () -> Unit)?>(null)
fun DialogFragment.dialogFragmentComposeView(
content: @Composable () -> Unit
): ComposeView {
return ComposeView(requireContext()).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
// Another interesting flaw. If you use other strategies in DialogFragment you may get crashes 🫠
class DialogExample : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Custom style
setStyle(STYLE_NORMAL, R.style.FullScreenDialog_Light)
}
override fun onCreateView(
@Composable
fun DialogContent() {
var text by remember { mutableStateOf("") }
Scaffold(
topBar = { TopBar(onBackClicked = {}) },
) { paddingValues ->
Column(
modifier = Modifier
@k0siara
k0siara / ChooseAttachmentFromStorageUseCase.kt
Created November 5, 2022 21:23
ChooseAttachmentFromStorageUseCase.kt
class ChooseAttachmentFromStorageUseCase @Inject constructor(...) {
suspend fun invoke(...) {
val attachmentSource = showPickerBottomSheet(data = ...)
...
}
}
@k0siara
k0siara / MyViewModel.kt
Created November 5, 2022 21:22
MyViewModel.kt
class MyViewModel : ViewModel(), ... {
fun onSomeAction() {
viewModelScope.launch {
...
val dialogResult = showTryAgainDialog(data = ...)
...
}
}
}
@k0siara
k0siara / HomeFragment.kt
Created November 5, 2022 21:21
HomeFragment.kt
class HomeFragment : Fragment(R.layout.home_fragment) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
findNavController()
.currentBackStackEntry
?.savedStateHandle
?.getLiveData<String>("my-result-key")
?.observe(viewLifecycleOwner) { result ->
@k0siara
k0siara / MyDialogFragment.kt
Created November 5, 2022 21:20
MyDialogFragment.kt
class MyDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
AlertDialog.Builder(requireContext())
.setMessage(getString(R.string.order_confirmation))
.setPositiveButton(getString(R.string.ok)) { _, _ ->
findNavController()
.previousBackStackEntry
?.savedStateHandle?.set("my-result-key", "example-data")
}
@k0siara
k0siara / ActivityHandling3Dialogs.kt
Created November 5, 2022 21:17
ActivityHandling3Dialogs.kt
class MainActivity : FragmentActivity(),
NoticeDialogFragment.NoticeDialogListener,
SecondDialogFragment.Listener,
ThirdDialogFragment.Listener {
private val viewModel by ...
fun showNoticeDialog() {
val dialog = NoticeDialogFragment()
dialog.show(supportFragmentManager, "NoticeDialogFragment")