nowinandroid icon indicating copy to clipboard operation
nowinandroid copied to clipboard

[FR]: Misused of DisposableEffect instead of LaunchedEffect

Open mhdabbaghy opened this issue 2 years ago • 6 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Describe the problem

in MainActivity to update the dark content of the system bars to match the theme, there is a DisposableEffect which onDispose method is empty with no comment

DisposableEffect(systemUiController, darkTheme) {
    systemUiController.systemBarsDarkContentEnabled = !darkTheme
    onDispose {}
}

this code exists in MainActivity:103

Describe the solution

I can update it and use LaunchedEffect instead

Additional context

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

mhdabbaghy avatar Dec 29 '22 17:12 mhdabbaghy

I think DisposableEffect is more suitable. LaunchedEffect is used to call suspend functions other than normal functions

Sent from my 2201117TG using FastHub

hoc081098 avatar Dec 29 '22 17:12 hoc081098

@hoc081098 I do agree with you about not using suspend function but we are not disposing anything at all

mhdabbaghy avatar Dec 29 '22 17:12 mhdabbaghy

I think, an empty onDispose{} is not bad

hoc081098 avatar Dec 30 '22 13:12 hoc081098

@mhdabbaghy I also think this is the right usage of DisposableEffect based on it's javadoc. The onDispose method is empty because there is not special code associated to this callback, but it has to be returned from the DisposableEffect call. This issue should be closed, unless you have additionnal feedback :)

SimonMarquis avatar Aug 24 '23 18:08 SimonMarquis

@SimonMarquis It's a bit of a conflict with the official docs 👀

截圖 2023-10-08 00 05 34

dev-weiqi avatar Oct 07 '23 16:10 dev-weiqi

Agreed that this is not an ideal use of DisposableEffect, as it leads to an empty onDispose which is generally to be avoided.

The reason DisposableEffect is used over LaunchedEffect in this particular case is to update the system UI for a new dark mode setting as soon as possible: If the code was in a LaunchedEffect, it would run slightly later due to dispatching than when it runs now when it is placed in a DisposableEffect, and window flags are especially sensitive to timing.

alexvanyo avatar Oct 09 '23 19:10 alexvanyo