This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Feature pull to refresh gallery screen issue [866] #934
Merged
arriolac
merged 12 commits into
android:main
from
sahilsk3333:feature_pull_to_refresh_gallery_screen_issue_866
Dec 27, 2023
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
adaaa15
Replaced TopAppBar with CenterAlignedTopAppBar, Fix issue #857 App ti…
sahilsk3333 5dd259b
Merge branch 'android:main' into main
sahilsk3333 f7fb6de
Merge branch 'android:main' into main
sahilsk3333 7fa3031
Feature pull to refresh GalleryScreen issue[#866]
sahilsk3333 8064a5b
Moved isRefreshing state to compose
sahilsk3333 3c9b506
Merge branch 'android:main' into main
sahilsk3333 ec06499
Remove unnecessary isLoading state
sahilsk3333 f387658
Apply suggestions from code review
arriolac a0dde22
Merge branch 'main' into feature_pull_to_refresh_gallery_screen_issue…
arriolac 4fa74d3
Merge branch 'main' into feature_pull_to_refresh_gallery_screen_issue…
sahilsk3333 e915abc
Merge remote-tracking branch 'origin/feature_pull_to_refresh_gallery_…
sahilsk3333 ceb828d
Remove unnecessary imports
sahilsk3333 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,27 +16,38 @@ | |
|
||
package com.google.samples.apps.sunflower.compose.gallery | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.statusBarsPadding | ||
import androidx.compose.foundation.lazy.grid.GridCells | ||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.ArrowBack | ||
import androidx.compose.material.icons.automirrored.filled.ArrowBack | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBar | ||
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 | ||
import androidx.compose.ui.res.dimensionResource | ||
import androidx.compose.ui.res.stringResource | ||
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 | ||
|
@@ -59,41 +70,85 @@ fun GalleryScreen( | |
plantPictures = viewModel.plantPictures, | ||
onPhotoClick = onPhotoClick, | ||
onUpClick = onUpClick, | ||
onPullToRefresh = viewModel::refreshData, | ||
) | ||
} | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
private fun GalleryScreen( | ||
plantPictures: Flow<PagingData<UnsplashPhoto>>, | ||
onPhotoClick: (UnsplashPhoto) -> Unit = {}, | ||
onUpClick: () -> Unit = {}, | ||
onPullToRefresh: () -> Unit, | ||
) { | ||
Scaffold( | ||
topBar = { | ||
GalleryTopBar(onUpClick = onUpClick) | ||
}, | ||
) { padding -> | ||
val pagingItems: LazyPagingItems<UnsplashPhoto> = plantPictures.collectAsLazyPagingItems() | ||
LazyVerticalGrid( | ||
columns = GridCells.Fixed(2), | ||
modifier = Modifier.padding(padding), | ||
contentPadding = PaddingValues(all = dimensionResource(id = R.dimen.card_side_margin)) | ||
) { | ||
// TODO update this implementation once paging Compose supports LazyGridScope | ||
// See: https://issuetracker.google.com/issues/178087310 | ||
items( | ||
count = pagingItems.itemCount, | ||
key = { index -> | ||
val photo = pagingItems[index] | ||
"${ photo?.id ?: ""}${index}" | ||
|
||
var isRefreshing by remember { mutableStateOf(false) } | ||
val pullToRefreshState = rememberPullToRefreshState() | ||
|
||
if (pullToRefreshState.isRefreshing){ | ||
arriolac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LaunchedEffect(Unit){ | ||
onPullToRefresh() | ||
isRefreshing = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this per comment above |
||
} | ||
} | ||
LaunchedEffect(isRefreshing){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block of code is not needed |
||
if (!isRefreshing){ | ||
pullToRefreshState.endRefresh() | ||
} | ||
} | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can call |
||
} | ||
) { index -> | ||
val photo = pagingItems[index] ?: return@items | ||
PhotoListItem(photo = photo) { | ||
onPhotoClick(photo) | ||
} | ||
} | ||
|
||
|
||
|
||
Box( | ||
modifier = Modifier | ||
.padding(padding) | ||
.nestedScroll(pullToRefreshState.nestedScrollConnection) | ||
) { | ||
|
||
LazyVerticalGrid( | ||
columns = GridCells.Fixed(2), | ||
contentPadding = PaddingValues(all = dimensionResource(id = R.dimen.card_side_margin)) | ||
) { | ||
// TODO update this implementation once paging Compose supports LazyGridScope | ||
// See: https://issuetracker.google.com/issues/178087310 | ||
items( | ||
count = pagingItems.itemCount, | ||
key = { index -> | ||
val photo = pagingItems[index] | ||
"${photo?.id ?: ""}${index}" | ||
} | ||
) { index -> | ||
val photo = pagingItems[index] ?: return@items | ||
PhotoListItem(photo = photo) { | ||
onPhotoClick(photo) | ||
} | ||
} | ||
} | ||
|
||
PullToRefreshContainer( | ||
modifier = Modifier.align(Alignment.TopCenter), | ||
state = pullToRefreshState | ||
) | ||
} | ||
|
||
|
||
} | ||
} | ||
|
||
|
@@ -111,7 +166,7 @@ private fun GalleryTopBar( | |
navigationIcon = { | ||
IconButton(onClick = onUpClick) { | ||
Icon( | ||
Icons.Filled.ArrowBack, | ||
Icons.AutoMirrored.Filled.ArrowBack, | ||
contentDescription = null | ||
) | ||
} | ||
|
@@ -124,7 +179,7 @@ private fun GalleryTopBar( | |
private fun GalleryScreenPreview( | ||
@PreviewParameter(GalleryScreenPreviewParamProvider::class) plantPictures: Flow<PagingData<UnsplashPhoto>> | ||
) { | ||
GalleryScreen(plantPictures = plantPictures) | ||
GalleryScreen(plantPictures = plantPictures, onPullToRefresh = {}) | ||
} | ||
|
||
private class GalleryScreenPreviewParamProvider : | ||
|
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra
isRefreshing
state is not needed.pullToRefreshState
should be the source of truth instead of having 2 different "is refreshing" states.