https://kotlinlang.org logo
#compose
Title
# compose
s

Spikey Sanju

02/27/2021, 10:48 AM
Hi guys, Currently i'm developing one sample app. Whenever I click on icon button I need to change the app theme & Icon. Below code works fine. But is there any way to optimise the below code?... Since I'm repeating the entire code block in both
if
and
else
block?
j

jaqxues

02/27/2021, 10:53 AM
val icon = if (isSytemInDarkTheme) painterResource(...) else painterResource(...) Icon(painter = icon, ...)
1
j

jim

02/27/2021, 10:55 AM
Copy code
@Composable
fun WigglesThemeSwitch(onToggle: ()->Unit) {
	Icon(
		Painter = painterResource(if(isSysteminDarkTheme()) R.drawable.ic_light_off else R.drawable.ic_light_on)
		contentDescription = null,
		modifier = Modifier.size(24.dp, 24.dp).clickable { onToggle() })
	)
}
1
s

Spikey Sanju

02/27/2021, 10:56 AM
Thanks guys it works ♥️
j

jaqxues

02/27/2021, 10:56 AM
Do you know if wrapping lambdas in lambdas decreases performance? I always wondered So instead of
.clickable { onToggle() }
.clickable(action = onToggle)
Intuitively I would say the 2nd option would yield better performance, but I am not sure if Kotlin optimizes this automatically
j

jim

02/27/2021, 10:58 AM
I think kotlin team generally recommends the second option when possible, yes.
(even if for no other reason than aesthetics)
s

Spikey Sanju

02/27/2021, 11:01 AM
2nd approach it's not working for me... Am I doing anything wrong here 😬
It works thanks
a

alorma

02/27/2021, 1:44 PM
There's also a way to change between light / dark on all app