refactor: workaround poor read/saved state management

I need a fresh set of eyes on this absolute mess
This commit is contained in:
Harsh Shandilya 2024-01-30 01:36:00 +05:30
parent 6a9cd40eaf
commit b1148679e3
5 changed files with 36 additions and 15 deletions

View file

@ -54,7 +54,7 @@ fun DatabasePosts(
items.forEach { (month, posts) ->
stickyHeader(contentType = "month-header") { MonthHeader(label = month) }
items(items = posts, key = { it.shortId }, contentType = { "LobstersItem" }) { item ->
LobstersListItem(item = item, postActions = postActions)
LobstersListItem(item = item, refresh = {}, postActions = postActions)
HorizontalDivider()
}
}

View file

@ -20,7 +20,12 @@ import me.saket.swipe.SwipeAction
import me.saket.swipe.SwipeableActionsBox
@Composable
fun LobstersListItem(item: UIPost, postActions: PostActions, modifier: Modifier = Modifier) {
fun LobstersListItem(
item: UIPost,
postActions: PostActions,
refresh: () -> Unit,
modifier: Modifier = Modifier,
) {
val commentsAction =
SwipeAction(
icon = rememberVectorPainter(Icons.AutoMirrored.Filled.Reply),
@ -28,6 +33,6 @@ fun LobstersListItem(item: UIPost, postActions: PostActions, modifier: Modifier
onSwipe = { postActions.viewCommentsPage(item.commentsUrl) },
)
SwipeableActionsBox(endActions = listOf(commentsAction)) {
LobstersCard(post = item, postActions = postActions, modifier = modifier)
LobstersCard(post = item, postActions = postActions, refresh = refresh, modifier = modifier)
}
}

View file

@ -42,7 +42,7 @@ fun NetworkPosts(
) {
ReportDrawnWhen { lazyPagingItems.itemCount > 0 }
val refreshLoadState = lazyPagingItems.loadState.refresh
val isRefreshing = refreshLoadState == LoadState.Loading
val isRefreshing = refreshLoadState == LoadState.Loading && lazyPagingItems.itemCount == 0
val pullRefreshState = rememberPullRefreshState(isRefreshing, lazyPagingItems::refresh)
Box(modifier = modifier.fillMaxSize().pullRefresh(pullRefreshState)) {
if (lazyPagingItems.itemCount == 0 && refreshLoadState is LoadState.Error) {
@ -60,8 +60,11 @@ fun NetworkPosts(
) { index ->
val item = lazyPagingItems[index]
if (item != null) {
LobstersListItem(item = item, postActions = postActions)
LobstersListItem(
item = item,
postActions = postActions,
refresh = { lazyPagingItems.refresh() },
)
HorizontalDivider()
}
}

View file

@ -84,7 +84,6 @@ constructor(
.flow
.map(::mapToUIPost)
.cachedIn(viewModelScope)
val newestPosts =
Pager(
config = PagingConfig(pageSize = PAGE_SIZE),
@ -102,12 +101,10 @@ constructor(
)
.flow
.map(::mapToUIPost)
val savedPosts =
savedPostsRepository.savedPosts
.map { it.map(UIPost.Companion::fromSavedPost) }
.shareIn(viewModelScope, started = SharingStarted.Lazily, Int.MAX_VALUE)
val savedPostsByMonth = savedPosts.map(::groupSavedPosts)
var searchQuery by mutableStateOf("")