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, lazyPagingItems = hottestPosts,
listState = hottestListState, listState = hottestListState,
isPostSaved = viewModel::isPostSaved, isPostSaved = viewModel::isPostSaved,
reloadPosts = viewModel::refreshHottestPosts,
postActions = postActions, postActions = postActions,
) )
} }
@ -186,7 +185,6 @@ fun LobstersApp(
lazyPagingItems = newestPosts, lazyPagingItems = newestPosts,
listState = newestListState, listState = newestListState,
isPostSaved = viewModel::isPostSaved, isPostSaved = viewModel::isPostSaved,
reloadPosts = viewModel::refreshNewestPosts,
postActions = postActions, postActions = postActions,
) )
} }

View file

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

View file

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