mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 03:37:05 +05:30
refactor: simplify swipe to refresh
This commit is contained in:
parent
683cb0cfbb
commit
5443cc1486
3 changed files with 3 additions and 20 deletions
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue