Created
April 22, 2024 13:26
-
-
Save KlassenKonstantin/06a1374cfcb16bdb3789c44b0c9dc41b to your computer and use it in GitHub Desktop.
Crash
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
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.activity.enableEdgeToEdge | |
import androidx.compose.animation.ExperimentalSharedTransitionApi | |
import androidx.compose.animation.SharedTransitionLayout | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.aspectRatio | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.layout.size | |
import androidx.compose.foundation.lazy.LazyColumn | |
import androidx.compose.foundation.lazy.items | |
import androidx.compose.foundation.shape.CircleShape | |
import androidx.compose.material.icons.Icons | |
import androidx.compose.material.icons.rounded.CheckCircle | |
import androidx.compose.material3.HorizontalDivider | |
import androidx.compose.material3.Icon | |
import androidx.compose.material3.ListItem | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.material3.Scaffold | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.runtime.setValue | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.draw.clip | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.unit.dp | |
import de.kuno.disappearingitems.ui.theme.DisappearingItemsTheme | |
import kotlin.random.Random | |
@OptIn(ExperimentalSharedTransitionApi::class) | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
enableEdgeToEdge() | |
setContent { | |
TheTheme { | |
SharedTransitionLayout { // Remove this -> no crash | |
var expandedHeaders by remember { mutableStateOf(setOf<String>()) } | |
val items = buildList { | |
repeat(5) { header -> | |
add(Element.Header(header.toString())) | |
if (expandedHeaders.contains(header.toString())) { | |
repeat(100) { | |
add(Element.Item(header.toString(), it.toString(), Random.nextBoolean())) | |
} | |
} | |
add(Element.Divider("$header-divider")) | |
} | |
} | |
Scaffold( | |
modifier = Modifier.fillMaxSize(), | |
topBar = { | |
Box(modifier = Modifier | |
.fillMaxWidth() | |
.height(100.dp)) | |
} | |
) { innerPadding -> | |
LazyColumn( | |
Modifier.padding(innerPadding) | |
) { | |
items(items, key = { it.id }, contentType = { it.javaClass.simpleName }) { element -> | |
when (element) { | |
is Element.Header -> ListItem( | |
modifier = Modifier | |
.animateItem() | |
.clickable { | |
if (expandedHeaders.contains(element.title)) { | |
expandedHeaders = expandedHeaders | |
.filter { it != element.title } | |
.toSet() | |
} else { | |
expandedHeaders = expandedHeaders + element.title | |
} | |
}, | |
headlineContent = { Text(text = "Header ${element.title}") } | |
) | |
is Element.Item -> { | |
Row( | |
Modifier | |
.animateItem() | |
.padding(16.dp) | |
) { | |
Box( | |
modifier = Modifier | |
.padding(16.dp) | |
.height(48.dp) | |
.aspectRatio(1f, true) | |
.background(Color.Red, CircleShape) | |
) { | |
Text( | |
modifier = Modifier.align(Alignment.Center), | |
text = "A" | |
) | |
if (element.hasCheckmark) { | |
Box( | |
modifier = Modifier | |
.align(Alignment.BottomEnd) | |
.clip(CircleShape) | |
.background(MaterialTheme.colorScheme.surface), | |
) { | |
Icon( | |
modifier = Modifier.size(16.dp), | |
imageVector = Icons.Rounded.CheckCircle, | |
contentDescription = null, | |
) | |
} | |
} | |
} | |
Text(text = element.text) | |
} | |
} | |
is Element.Divider -> { | |
HorizontalDivider(Modifier.animateItem()) | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
sealed interface Element { | |
val id: String | |
data class Header( | |
val title: String, | |
) : Element { | |
override val id = title | |
} | |
data class Divider( | |
override val id: String, | |
) : Element | |
data class Item( | |
private val headerId: String, | |
val text: String, | |
val hasCheckmark: Boolean, | |
) : Element { | |
override val id = "$headerId-$text" | |
} | |
} |
Author
KlassenKonstantin
commented
Apr 22, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment