Нужна помощь в переходе с Box+PullToRefreshContainer на PullToRefreshBoxAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Нужна помощь в переходе с Box+PullToRefreshContainer на PullToRefreshBox

Сообщение Anonymous »

несколько месяцев назад я использовал Box+PullToRefreshContainer в одном из своих проектов в Jetpack Compose. Но теперь, поскольку он устарел и заменен на PullToRefreshBox. Мне нужна помощь, как перейти на него.
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ArticlesList(
modifier: Modifier = Modifier,
articles: LazyPagingItems,
onRefresh: () -> Unit,
onClick: (Article) -> Unit,
lazyListState: LazyListState = rememberLazyListState()
){
val pullToRefreshState = rememberPullToRefreshState()
Box (
modifier = modifier
.nestedScroll(pullToRefreshState.nestedScrollConnection)
){
val handlePagingResult = handlePagingResult(articles = articles)
if (handlePagingResult) {
LazyColumn(
modifier = modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(24.dp),
contentPadding = PaddingValues(all = 6.dp),
state = lazyListState
) {
items(articles.itemCount) { index ->
articles[index]?.let { article ->
ArticleCard(
article = article,
onClick = { onClick(article) },
)
}
}
}
}
if(pullToRefreshState.isRefreshing) {
LaunchedEffect(true) {
onRefresh()
}
}

LaunchedEffect(articles.loadState.refresh) {
if(articles.loadState.refresh is LoadState.Loading) {
pullToRefreshState.startRefresh()
} else {
pullToRefreshState.endRefresh()
}
}

if(articles.loadState.refresh is LoadState.Loading || (lazyListState.isScrollInProgress && !lazyListState.canScrollBackward)) {
PullToRefreshContainer(
state = pullToRefreshState,
modifier = Modifier
.align(Alignment.TopCenter),
containerColor = Color.Transparent
)
}
}
}

@Composable
fun handlePagingResult(
articles: LazyPagingItems,
): Boolean {
val loadState = articles.loadState
val error = when {
loadState.refresh is LoadState.Error -> {
loadState.refresh as LoadState.Error
}

loadState.append is LoadState.Error -> {
loadState.append as LoadState.Error
}

loadState.prepend is LoadState.Error -> {
loadState.prepend as LoadState.Error
}

else -> null
}
return when {
loadState.refresh is LoadState.Loading -> {
ShimmerEffect()
false
}

error != null -> {
EmptyScreen(error)
false
}

articles.itemCount == 0 -> {
EmptyScreen()
false
}
else -> {
true
}

}
}

@Composable
private fun ShimmerEffect() {
Column(verticalArrangement = Arrangement.spacedBy(24.dp)) {
repeat(10) {
ArticleCardShimmerEffect(modifier = Modifier.padding(horizontal = 24.dp))
}
}
}

Я прочитал документацию, но не понял, как интегрировать ее с PagingResult.
package com.example.newz.presentation.home

import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import com.example.newz.domain.usecases.news.NewsUseCases
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(
private val newsUseCases: NewsUseCases
): ViewModel() {
private val _state = mutableStateOf(HomeState())
val state: State = _state
// val news = newsUseCases.getNews(
// source = listOf("bbc-news", "cnn", "fox-news", "google-news", "the-hindu", "the-times-of-india", "zee-news" ),
// domain = listOf("ndtv.com", "hindustantimes.com", "indianexpress.com", "indiatoday.in", "indiatvnews.com", "moneycontrol.com", "screenrant.com", "india.com","abplive.com", "googleblog.in", "businessinsider.in", "in.ign.com")
// ).cachedIn(viewModelScope)
private fun updateScrollValue(value: Int) {
_state.value = _state.value.copy(scrollValue = value)
}
private fun updateMaxScrollValue(value: Int) {
_state.value = _state.value.copy(maxScrollValue = value)
}

private fun getNews() {
viewModelScope.launch {
val articles = newsUseCases.getNews(
source = listOf("bbc-news", "cnn", "fox-news", "google-news", "the-hindu", "the-times-of-india", "zee-news"),
domain = listOf("ndtv.com", "hindustantimes.com", "indianexpress.com", "indiatoday.in", "indiatvnews.com", "moneycontrol.com", "screenrant.com", "india.com","abplive.com", "googleblog.in", "businessinsider.in", "in.ign.com")
).cachedIn(viewModelScope)
_state.value = _state.value.copy(articles = articles)
}
}
init {
getNews()
}
fun onEvent(event: HomeEvent) {
when (event) {
is HomeEvent.UpdateScrollValue -> {
updateScrollValue(event.value)
}
is HomeEvent.UpdateMaxScrollValue -> {
updateMaxScrollValue(event.value)
}
is HomeEvent.LoadNews -> {
getNews()
}
}
}
}


package com.example.newz.presentation.home

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
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.statusBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.paging.compose.collectAsLazyPagingItems
import com.example.newz.R
import com.example.newz.domain.model.Article
import com.example.newz.presentation.common.ArticlesList
import com.example.newz.presentation.common.SearchBar
import kotlinx.coroutines.delay

@Composable
fun HomeScreen(
state: HomeState,
navigateToSearch: () -> Unit,
navigateToDetails:(Article)->Unit,
event:(HomeEvent)->Unit
){
val articles = state.articles!!.collectAsLazyPagingItems()
val titles by remember(state.articles) {
derivedStateOf {
if (articles.itemCount>10){
articles.itemSnapshotList.items
.slice(0..9)
.joinToString ( separator = "\uD83d\uDFE5" ) { it.title }
}else{
""
}
}
}
Column(
modifier = Modifier
.fillMaxSize()
.padding(top = 24.dp)
.statusBarsPadding()){
Image(
painter = painterResource(id = R.drawable.ic_logo),
contentDescription = null,
modifier = Modifier
.width(150.dp)
.height(50.dp)
.padding(horizontal = 24.dp)
)
Spacer(modifier =Modifier.height(24.dp))
SearchBar(
modifier = Modifier
.padding(horizontal = 24.dp),
text = "",
readOnly = true,
onValueChange = {},
onSearch = {},
onClick = {
navigateToSearch()
}
)
Spacer(modifier =Modifier.height(24.dp))
val scrollState = rememberScrollState()
LaunchedEffect(key1 = state.maxScrollValue){
delay(500)
if (state.maxScrollValue>0){
scrollState.animateScrollTo(
state.maxScrollValue,
animationSpec = infiniteRepeatable(
tween(
durationMillis = (state.maxScrollValue-state.scrollValue)*50000/state.maxScrollValue,
easing = LinearEasing,
delayMillis = 2500
)
)
)
}
}
LaunchedEffect(key1 = scrollState.maxValue){
event(HomeEvent.UpdateMaxScrollValue(scrollState.maxValue))
}
LaunchedEffect(key1 = scrollState.value){
event(HomeEvent.UpdateScrollValue(scrollState.value))
}
Text(
text =titles,
modifier = Modifier
.padding(horizontal = 24.dp)
.fillMaxWidth()
.horizontalScroll(scrollState, enabled = false),
fontSize = 12.sp,
color = colorResource(id = R.color.placeholder)
)
Spacer(modifier = Modifier.height(24.dp))
// ArticlesList(
// modifier = Modifier
// .padding(horizontal = 24.dp),
// articles = articles,
// onClick = {
// navigateToDetails(it)
// })
ArticlesList(
articles = articles,
onRefresh = {
event(HomeEvent.LoadNews)
},
onClick = {
navigateToDetails(it)
}
)
}
}

package com.example.newz.presentation.home

sealed class HomeEvent {
data class UpdateScrollValue(val value: Int): HomeEvent()
data class UpdateMaxScrollValue(val value: Int): HomeEvent()
data object LoadNews: HomeEvent()
}


package com.example.newz.presentation.home

import androidx.paging.PagingData
import com.example.newz.domain.model.Article
import kotlinx.coroutines.flow.Flow

data class HomeState(
val scrollValue: Int = 0,
val maxScrollValue: Int = 0,
val articles: Flow? = null,
)


Подробнее здесь: https://stackoverflow.com/questions/790 ... refreshbox
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»