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
@k0siara
k0siara / HandleEventsWithSharedFlow.kt
Created July 14, 2021 20:15
HandleEventsWithSharedFlow
// ViewModel with SharedFlow
abstract class BaseViewModel<STATE, EVENT : UiEvent, EFFECT : UiEffect>(
initialState: UiState<STATE> = UiState.Loading
) : ViewModel() {
...
private val _event: MutableSharedFlow<EVENT> = MutableSharedFlow()
val event = _event.asSharedFlow()
@k0siara
k0siara / HandleEventsWithSharedFlowExample.kt
Created July 14, 2021 20:18
HandleEventsWithSharedFlowExample
class AddEmployeeViewModel(
private val employeeRepository: EmployeeRepository
) :
BaseViewModel<AddEmployeeContract.State, AddEmployeeContract.Event, AddEmployeeContract.Effect>(
initialState = AddEmployeeContract.State.Loading()
) {
...
override fun handleEvent(event: AddEmployeeContract.Event) {
@k0siara
k0siara / SafeUseOfFragmentManager.kt
Last active May 16, 2023 03:54
SafeUseOfFragmentManager.kt
fun confirmStartGame() {
val fm = supportFragmentManager
if (fm.isStateSaved) return
    val newFragment = StartGameDialogFragment()
    newFragment.show(newFragment, "game")
}
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 🫠
@Composable
fun DialogContent() {
var text by remember { mutableStateOf("") }
Scaffold(
topBar = { TopBar(onBackClicked = {}) },
) { paddingValues ->
Column(
modifier = Modifier
class DialogExample : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Custom style
setStyle(STYLE_NORMAL, R.style.FullScreenDialog_Light)
}
override fun onCreateView(
@k0siara
k0siara / CustomDialog.kt
Last active November 6, 2022 09:58
CustomDialog.kt
class CustomDialog : DialogFragment(R.layout.custom_dialog) {
private val binding by viewBinding<CustomDialogBinding>()
private val viewModel by viewModels<CustomDialogViewModel>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Setup UI and other stuff ...
}
}
@k0siara
k0siara / ChooseAttachmentFromStorageUseCase.kt
Created November 5, 2022 21:23
ChooseAttachmentFromStorageUseCase.kt
class ChooseAttachmentFromStorageUseCase @Inject constructor(...) {
suspend fun invoke(...) {
val attachmentSource = showPickerBottomSheet(data = ...)
...
}
}