Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
Moved isRefreshing state to compose
Browse files Browse the repository at this point in the history
  • Loading branch information
sahilsk3333 committed Dec 11, 2023
1 parent 7fa3031 commit 8064a5b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import androidx.compose.material3.pulltorefresh.PullToRefreshContainer
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
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.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
Expand All @@ -43,6 +47,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.paging.LoadState
import androidx.paging.PagingData
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
Expand All @@ -66,30 +71,29 @@ fun GalleryScreen(
onPhotoClick = onPhotoClick,
onUpClick = onUpClick,
onPullToRefresh = viewModel::refreshData,
isRefreshing = viewModel.isRefreshing.value
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun GalleryScreen(
plantPictures: Flow<PagingData<UnsplashPhoto>>,
onPhotoClick: (UnsplashPhoto) -> Unit = {},
onUpClick: () -> Unit = {},
onPullToRefresh: () -> Unit,
isRefreshing: Boolean
) {
Scaffold(
topBar = {
GalleryTopBar(onUpClick = onUpClick)
},
) { padding ->

var isRefreshing by remember { mutableStateOf(false) }
val pullToRefreshState = rememberPullToRefreshState()

if (pullToRefreshState.isRefreshing){
LaunchedEffect(Unit){
onPullToRefresh()
isRefreshing = true
}
}
LaunchedEffect(isRefreshing){
Expand All @@ -98,14 +102,26 @@ private fun GalleryScreen(
}
}

val pagingItems: LazyPagingItems<UnsplashPhoto> =
plantPictures.collectAsLazyPagingItems()

LaunchedEffect(pagingItems.loadState) {
when (pagingItems.loadState.refresh) {
is LoadState.Loading -> Unit
is LoadState.Error,is LoadState.NotLoading -> {
isRefreshing = false
}
}
}



Box(
modifier = Modifier
.padding(padding)
.nestedScroll(pullToRefreshState.nestedScrollConnection)
) {
val pagingItems: LazyPagingItems<UnsplashPhoto> =
plantPictures.collectAsLazyPagingItems()

LazyVerticalGrid(
columns = GridCells.Fixed(2),
contentPadding = PaddingValues(all = dimensionResource(id = R.dimen.card_side_margin))
Expand Down Expand Up @@ -163,7 +179,7 @@ private fun GalleryTopBar(
private fun GalleryScreenPreview(
@PreviewParameter(GalleryScreenPreviewParamProvider::class) plantPictures: Flow<PagingData<UnsplashPhoto>>
) {
GalleryScreen(plantPictures = plantPictures, onPullToRefresh = {}, isRefreshing = false)
GalleryScreen(plantPictures = plantPictures, onPullToRefresh = {})
}

private class GalleryScreenPreviewParamProvider :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.google.samples.apps.sunflower.viewmodels

import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand All @@ -26,7 +24,6 @@ import androidx.paging.cachedIn
import com.google.samples.apps.sunflower.data.UnsplashPhoto
import com.google.samples.apps.sunflower.data.UnsplashRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.filterNotNull
Expand All @@ -46,25 +43,18 @@ class GalleryViewModel @Inject constructor(
private val _plantPictures = MutableStateFlow<PagingData<UnsplashPhoto>?>(null)
val plantPictures: Flow<PagingData<UnsplashPhoto>> get() = _plantPictures.filterNotNull()

private val _isRefreshing = mutableStateOf(false)
val isRefreshing: State<Boolean> get() = _isRefreshing

init {
refreshData()
}


fun refreshData() {
_isRefreshing.value = true

viewModelScope.launch {
delay(1000)
try {
_plantPictures.value = repository.getSearchResultStream(queryString ?: "").cachedIn(viewModelScope).first()
_plantPictures.value = repository.getSearchResultStream(queryString ?: "").cachedIn(viewModelScope).first()
} catch (e: Exception) {
e.printStackTrace()
} finally {
_isRefreshing.value = false
}
}
}
Expand Down

0 comments on commit 8064a5b

Please sign in to comment.