|
import androidx.compose.foundation.layout.Box |
|
import androidx.compose.foundation.layout.Column |
|
import androidx.compose.foundation.layout.wrapContentSize |
|
import androidx.compose.material.Button |
|
import androidx.compose.material.Text |
|
import androidx.compose.runtime.Composable |
|
import androidx.compose.runtime.LaunchedEffect |
|
import androidx.compose.runtime.getValue |
|
import androidx.compose.runtime.mutableStateOf |
|
import androidx.compose.runtime.remember |
|
import androidx.compose.runtime.setValue |
|
import androidx.compose.ui.Modifier |
|
|
|
@Composable |
|
fun CursedButtonDemo() { |
|
var counter by remember { mutableStateOf(0) } |
|
Column(modifier = Modifier.wrapContentSize()) { |
|
Text("Counter: $counter") |
|
CursedButton( |
|
onClick = { |
|
counter++ |
|
{ |
|
CursedButton( |
|
onClick = { |
|
counter++ |
|
{ |
|
Text("STOP CLICKING!!") |
|
} |
|
}, |
|
) { |
|
Text("Thanks for humoring me") |
|
} |
|
} |
|
} |
|
) { |
|
Text("Click me!") |
|
} |
|
} |
|
} |
|
|
|
@Composable |
|
fun CursedButtonDemo2() { |
|
var counter by remember { mutableStateOf(0) } |
|
Column(modifier = Modifier.wrapContentSize()) { |
|
Text("Counter: $counter") |
|
CursedButton2( |
|
onClick = { |
|
LaunchedEffect(Unit) { |
|
counter++ |
|
} |
|
CursedButton2( |
|
onClick = { |
|
LaunchedEffect(Unit) { |
|
counter++ |
|
} |
|
Text("STOP CLICKING!!") |
|
}, |
|
) { |
|
Text("Thanks for humoring me") |
|
} |
|
} |
|
) { |
|
Text("Click me!") |
|
} |
|
} |
|
} |
|
|
|
@Composable |
|
fun BlessedButtonDemo() { |
|
var counter by remember { mutableStateOf(0) } |
|
Column(modifier = Modifier.wrapContentSize()) { |
|
Text("Counter: $counter") |
|
when (counter) { |
|
0 -> Button(onClick = { counter++ }) { |
|
Text("Click me!") |
|
} |
|
|
|
1 -> Button(onClick = { counter++ }) { |
|
Text("Thanks for humoring me") |
|
} |
|
|
|
else -> Text("STOP CLICKING!!") |
|
} |
|
} |
|
} |
|
|
|
@Composable |
|
fun CursedButton( |
|
onClick: () -> @Composable () -> Unit, |
|
modifier: Modifier = Modifier, |
|
content: @Composable () -> Unit |
|
) { |
|
val (actualContent, updateContent) = remember { |
|
mutableStateOf<(@Composable () -> Unit)?>(null) |
|
} |
|
|
|
if (actualContent != null) { |
|
Box(modifier = modifier, propagateMinConstraints = true) { |
|
actualContent() |
|
} |
|
} else { |
|
Button( |
|
onClick = { updateContent(onClick()) }, |
|
modifier = modifier |
|
) { |
|
content() |
|
} |
|
} |
|
} |
|
|
|
@Composable |
|
fun CursedButton2( |
|
onClick: @Composable () -> Unit, |
|
modifier: Modifier = Modifier, |
|
content: @Composable () -> Unit |
|
) { |
|
val (actualContent, updateContent) = remember { |
|
mutableStateOf<(@Composable () -> Unit)?>(null) |
|
} |
|
|
|
if (actualContent != null) { |
|
Box(modifier = modifier, propagateMinConstraints = true) { |
|
actualContent() |
|
} |
|
} else { |
|
Button( |
|
onClick = { updateContent(onClick) }, |
|
modifier = modifier |
|
) { |
|
content() |
|
} |
|
} |
|
} |