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) -> items.forEach { (month, posts) ->
stickyHeader(contentType = "month-header") { MonthHeader(label = month) } stickyHeader(contentType = "month-header") { MonthHeader(label = month) }
items(items = posts, key = { it.shortId }, contentType = { "LobstersItem" }) { item -> items(items = posts, key = { it.shortId }, contentType = { "LobstersItem" }) { item ->
LobstersListItem(item = item, postActions = postActions) LobstersListItem(item = item, refresh = {}, postActions = postActions)
HorizontalDivider() HorizontalDivider()
} }
} }

View file

@ -20,7 +20,12 @@ import me.saket.swipe.SwipeAction
import me.saket.swipe.SwipeableActionsBox import me.saket.swipe.SwipeableActionsBox
@Composable @Composable
fun LobstersListItem(item: UIPost, postActions: PostActions, modifier: Modifier = Modifier) { fun LobstersListItem(
item: UIPost,
postActions: PostActions,
refresh: () -> Unit,
modifier: Modifier = Modifier,
) {
val commentsAction = val commentsAction =
SwipeAction( SwipeAction(
icon = rememberVectorPainter(Icons.AutoMirrored.Filled.Reply), icon = rememberVectorPainter(Icons.AutoMirrored.Filled.Reply),
@ -28,6 +33,6 @@ fun LobstersListItem(item: UIPost, postActions: PostActions, modifier: Modifier
onSwipe = { postActions.viewCommentsPage(item.commentsUrl) }, onSwipe = { postActions.viewCommentsPage(item.commentsUrl) },
) )
SwipeableActionsBox(endActions = listOf(commentsAction)) { 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 } ReportDrawnWhen { lazyPagingItems.itemCount > 0 }
val refreshLoadState = lazyPagingItems.loadState.refresh val refreshLoadState = lazyPagingItems.loadState.refresh
val isRefreshing = refreshLoadState == LoadState.Loading val isRefreshing = refreshLoadState == LoadState.Loading && lazyPagingItems.itemCount == 0
val pullRefreshState = rememberPullRefreshState(isRefreshing, lazyPagingItems::refresh) 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) {
@ -60,8 +60,11 @@ fun NetworkPosts(
) { index -> ) { index ->
val item = lazyPagingItems[index] val item = lazyPagingItems[index]
if (item != null) { if (item != null) {
LobstersListItem(item = item, postActions = postActions) LobstersListItem(
item = item,
postActions = postActions,
refresh = { lazyPagingItems.refresh() },
)
HorizontalDivider() HorizontalDivider()
} }
} }

View file

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

View file

@ -58,13 +58,23 @@ import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableList
@Composable @Composable
fun LobstersCard(post: UIPost, postActions: PostActions, modifier: Modifier = Modifier) { fun LobstersCard(
post: UIPost,
postActions: PostActions,
refresh: () -> Unit,
modifier: Modifier = Modifier,
) {
var localReadState by remember(post) { mutableStateOf(post.isRead) }
var localSavedState by remember(post) { mutableStateOf(post.isSaved) } var localSavedState by remember(post) { mutableStateOf(post.isSaved) }
Box( Box(
modifier = modifier =
modifier modifier
.fillMaxWidth() .fillMaxWidth()
.clickable { postActions.viewPost(post.shortId, post.url, post.commentsUrl) } .clickable {
postActions.viewPost(post.shortId, post.url, post.commentsUrl)
localReadState = true
refresh()
}
.background(MaterialTheme.colorScheme.background) .background(MaterialTheme.colorScheme.background)
.padding(start = 16.dp, top = 16.dp, end = 4.dp, bottom = 16.dp) .padding(start = 16.dp, top = 16.dp, end = 4.dp, bottom = 16.dp)
) { ) {
@ -72,7 +82,7 @@ fun LobstersCard(post: UIPost, postActions: PostActions, modifier: Modifier = Mo
horizontalArrangement = Arrangement.spacedBy(8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
) { ) {
PostDetails(modifier = Modifier.weight(1f), post = post) PostDetails(post = post, isRead = localReadState, modifier = Modifier.weight(1f))
Column( Column(
modifier = Modifier.wrapContentHeight(), modifier = Modifier.wrapContentHeight(),
verticalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp),
@ -84,6 +94,7 @@ fun LobstersCard(post: UIPost, postActions: PostActions, modifier: Modifier = Mo
Modifier.clickable(role = Role.Button) { Modifier.clickable(role = Role.Button) {
localSavedState = !localSavedState localSavedState = !localSavedState
postActions.toggleSave(post) postActions.toggleSave(post)
refresh()
}, },
) )
HorizontalDivider(modifier = Modifier.width(48.dp)) HorizontalDivider(modifier = Modifier.width(48.dp))
@ -92,7 +103,11 @@ fun LobstersCard(post: UIPost, postActions: PostActions, modifier: Modifier = Mo
modifier = modifier =
Modifier.clickable( Modifier.clickable(
role = Role.Button, role = Role.Button,
onClick = { postActions.viewComments(post.shortId) }, onClick = {
postActions.viewComments(post.shortId)
localReadState = true
refresh()
},
), ),
) )
} }
@ -101,9 +116,9 @@ fun LobstersCard(post: UIPost, postActions: PostActions, modifier: Modifier = Mo
} }
@Composable @Composable
fun PostDetails(post: UIPost, modifier: Modifier = Modifier) { fun PostDetails(post: UIPost, isRead: Boolean, modifier: Modifier = Modifier) {
Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(8.dp)) { Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(8.dp)) {
PostTitle(title = post.title, isRead = post.isRead) PostTitle(title = post.title, isRead = isRead)
TagRow(tags = post.tags.toImmutableList()) TagRow(tags = post.tags.toImmutableList())
Spacer(Modifier.height(4.dp)) Spacer(Modifier.height(4.dp))
Submitter( Submitter(
@ -269,6 +284,7 @@ private fun LobstersCardPreview() {
return LinkMetadata("", "") return LinkMetadata("", "")
} }
}, },
refresh = {},
) )
} }
} }