refactor: simplify swipe to refresh

This commit is contained in:
Harsh Shandilya 2022-11-29 01:25:20 +05:30
parent 683cb0cfbb
commit 5443cc1486
No known key found for this signature in database
3 changed files with 3 additions and 20 deletions

View file

@ -174,7 +174,6 @@ fun LobstersApp(
lazyPagingItems = hottestPosts,
listState = hottestListState,
isPostSaved = viewModel::isPostSaved,
reloadPosts = viewModel::refreshHottestPosts,
postActions = postActions,
)
}
@ -186,7 +185,6 @@ fun LobstersApp(
lazyPagingItems = newestPosts,
listState = newestListState,
isPostSaved = viewModel::isPostSaved,
reloadPosts = viewModel::refreshNewestPosts,
postActions = postActions,
)
}

View file

@ -40,13 +40,12 @@ fun NetworkPosts(
lazyPagingItems: LazyPagingItems<LobstersPost>,
listState: LazyListState,
isPostSaved: suspend (SavedPost) -> Boolean,
reloadPosts: () -> Unit,
postActions: PostActions,
modifier: Modifier = Modifier,
) {
val refreshLoadState = lazyPagingItems.loadState.refresh
val isRefreshing = refreshLoadState == LoadState.Loading
val pullRefreshState = rememberPullRefreshState(isRefreshing, reloadPosts)
val pullRefreshState = rememberPullRefreshState(isRefreshing, lazyPagingItems::refresh)
Box(modifier = modifier.fillMaxSize().pullRefresh(pullRefreshState)) {
if (lazyPagingItems.itemCount == 0 && refreshLoadState is LoadState.Error) {
NetworkError(

View file

@ -37,16 +37,10 @@ constructor(
private val pagingSourceFactory: LobstersPagingSource.Factory,
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
) : ViewModel() {
private var hottestPostsPagingSource: LobstersPagingSource? = null
private var newestPostsPagingSource: LobstersPagingSource? = null
private val hottestPostsPager =
Pager(PagingConfig(20)) {
pagingSourceFactory.create(api::getHottestPosts).also { hottestPostsPagingSource = it }
}
Pager(PagingConfig(pageSize = 20)) { pagingSourceFactory.create(api::getHottestPosts) }
private val newestPostsPager =
Pager(PagingConfig(20)) {
pagingSourceFactory.create(api::getHottestPosts).also { newestPostsPagingSource = it }
}
Pager(PagingConfig(pageSize = 20)) { pagingSourceFactory.create(api::getHottestPosts) }
val hottestPosts
get() = hottestPostsPager.flow
@ -104,12 +98,4 @@ constructor(
is Failure.ApiFailure -> throw IOException("API returned an invalid response")
}
}
fun refreshHottestPosts() {
hottestPostsPagingSource?.invalidate()
}
fun refreshNewestPosts() {
newestPostsPagingSource?.invalidate()
}
}