View DialogFragmentComposeView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun DialogFragment.dialogFragmentComposeView( | |
consumeWindowInsets: Boolean, | |
content: @Composable () -> Unit, | |
): View { | |
return DialogFragmentComposeView( | |
context = requireContext(), | |
dialogProvider = { dialog ?: Dialog(requireContext()) }, | |
).apply { | |
consumeWindowInsets = consumeWindowInsets | |
layoutParams = ViewGroup.LayoutParams( |
View DialogFragmentComposeView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
View DialogFragmentComposeView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 🫠 |
View DialogExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DialogExample : DialogFragment() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// Custom style | |
setStyle(STYLE_NORMAL, R.style.FullScreenDialog_Light) | |
} | |
override fun onCreateView( |
View DialogContent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun DialogContent() { | |
var text by remember { mutableStateOf("") } | |
Scaffold( | |
topBar = { TopBar(onBackClicked = {}) }, | |
) { paddingValues -> | |
Column( | |
modifier = Modifier |
View ChooseAttachmentFromStorageUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChooseAttachmentFromStorageUseCase @Inject constructor(...) { | |
suspend fun invoke(...) { | |
val attachmentSource = showPickerBottomSheet(data = ...) | |
... | |
} | |
} |
View MyViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyViewModel : ViewModel(), ... { | |
fun onSomeAction() { | |
viewModelScope.launch { | |
... | |
val dialogResult = showTryAgainDialog(data = ...) | |
... | |
} | |
} | |
} |
View HomeFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -> |
View MyDialogFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} |
View ActivityHandling3Dialogs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : FragmentActivity(), | |
NoticeDialogFragment.NoticeDialogListener, | |
SecondDialogFragment.Listener, | |
ThirdDialogFragment.Listener { | |
private val viewModel by ... | |
fun showNoticeDialog() { | |
val dialog = NoticeDialogFragment() | |
dialog.show(supportFragmentManager, "NoticeDialogFragment") |
NewerOlder