package com.example.swiperefresh
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.swiperefresh.ui.theme.SwipeRefreshTheme
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
SwipeRefreshTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
val items = remember {
(1..100).map { "Item $it" }
}
var isRefreshing by remember {
mutableStateOf(false)
}
val scope = rememberCoroutineScope()
Box(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.padding(top = 24.dp)
) {
PullToRefreshLazyColumn(
items = items,
content = {
Text(text = it)
},
isRefreshing = isRefreshing,
onRefresh = {
scope.launch {
isRefreshing = true
delay(2000)
isRefreshing = false
}
}
)
}
}
}
}
}
}
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun <T> PullToRefreshLazyColumn(
items: List<T>,
content: @Composable (T) -> Unit,
isRefreshing: Boolean,
onRefresh: () -> Unit,
modifier: Modifier = Modifier,
lazyListState: LazyListState = rememberLazyListState()
) {
val pullToRefreshState = rememberPullRefreshState(isRefreshing, onRefresh = onRefresh)
Box(
modifier = modifier
.fillMaxSize()
) {
LazyColumn(
state = lazyListState,
contentPadding = PaddingValues(8.dp),
modifier = Modifier
.fillMaxSize()
.pullRefresh(pullToRefreshState),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
items(items) {
content(it)
}
}
PullRefreshIndicator(
refreshing = isRefreshing,
state = pullToRefreshState,
modifier = Modifier.align(Alignment.TopCenter)
)
}
}