Trejkaz
12/24/2024, 12:50 PMTrejkaz
12/24/2024, 12:51 PMTrejkaz
12/24/2024, 1:08 PMimport androidx.compose.animation.core.tween
import androidx.compose.foundation.VerticalScrollbar
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollbarAdapter
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.darkColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
@OptIn(ExperimentalUuidApi::class)
data class HistoryEntry(val id: Any = Uuid.random())
class AppState {
val outputState = LazyListState()
val history: MutableList<HistoryEntry> = mutableStateListOf()
fun execute(scope: CoroutineScope) {
history.add(HistoryEntry())
scope.launch {
outputState.animateScrollToItem(history.lastIndex)
}
}
}
@Composable
fun MainContent(appState: AppState, scope: CoroutineScope) {
Column(Modifier.fillMaxSize().padding(16.dp)) {
Box(modifier = Modifier.weight(1.0f)) {
// Switch which of these is commented out for interesting results:
val lazyListState = appState.outputState
// val lazyListState = rememberLazyListState()
LazyColumn(
modifier = Modifier
.testTag("HistoryList")
.fillMaxSize()
.wrapContentHeight(Alignment.Bottom),
state = lazyListState,
) {
items(items = appState.history, key = HistoryEntry::id) { item ->
Box(
modifier = Modifier
.animateItem(
fadeInSpec = tween(durationMillis = 2000),
fadeOutSpec = tween(durationMillis = 2000),
placementSpec = tween(durationMillis = 1000),
)
) {
Column {
val text = item.id.toString()
Text(text = text, maxLines = 1)
}
}
}
}
VerticalScrollbar(
adapter = rememberScrollbarAdapter(lazyListState),
modifier = Modifier
.align(Alignment.CenterEnd)
.fillMaxHeight(),
)
}
Button(onClick = { appState.execute(scope) }) {
Text("Add Item")
}
}
}
fun main() = application {
val appState = remember { AppState() }
MaterialTheme(colorScheme = darkColorScheme()) {
Window(onCloseRequest = ::exitApplication) {
val scope = rememberCoroutineScope()
MainContent(appState, scope)
}
}
}
Trejkaz
12/24/2024, 1:31 PManimateScrollToItem
also fixes itTrejkaz
12/24/2024, 1:32 PM