mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 22:17:03 +05:30
refactor(android): make ClawViewModel#isPostSaved
synchronous
Fixes #403
This commit is contained in:
parent
46cc0910bf
commit
a5af23e330
4 changed files with 20 additions and 7 deletions
|
@ -23,12 +23,11 @@ import me.saket.swipe.SwipeableActionsBox
|
||||||
@Composable
|
@Composable
|
||||||
fun ListItem(
|
fun ListItem(
|
||||||
item: SavedPost,
|
item: SavedPost,
|
||||||
isSaved: suspend (SavedPost) -> Boolean,
|
isSaved: (SavedPost) -> Boolean,
|
||||||
isRead: suspend (String) -> Boolean,
|
isRead: suspend (String) -> Boolean,
|
||||||
postActions: PostActions,
|
postActions: PostActions,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
val saved by produceState(false, item) { value = isSaved(item) }
|
|
||||||
val read by produceState(false, item.shortId) { value = isRead(item.shortId) }
|
val read by produceState(false, item.shortId) { value = isRead(item.shortId) }
|
||||||
val commentsAction =
|
val commentsAction =
|
||||||
SwipeAction(
|
SwipeAction(
|
||||||
|
@ -41,7 +40,7 @@ fun ListItem(
|
||||||
) {
|
) {
|
||||||
LobstersCard(
|
LobstersCard(
|
||||||
post = item,
|
post = item,
|
||||||
isSaved = saved,
|
isSaved = isSaved(item),
|
||||||
isRead = read,
|
isRead = read,
|
||||||
postActions = postActions,
|
postActions = postActions,
|
||||||
modifier = modifier,
|
modifier = modifier,
|
||||||
|
|
|
@ -39,7 +39,7 @@ import eu.bambooapps.material3.pullrefresh.rememberPullRefreshState
|
||||||
fun NetworkPosts(
|
fun NetworkPosts(
|
||||||
lazyPagingItems: LazyPagingItems<LobstersPost>,
|
lazyPagingItems: LazyPagingItems<LobstersPost>,
|
||||||
listState: LazyListState,
|
listState: LazyListState,
|
||||||
isPostSaved: suspend (SavedPost) -> Boolean,
|
isPostSaved: (SavedPost) -> Boolean,
|
||||||
isPostRead: suspend (String) -> Boolean,
|
isPostRead: suspend (String) -> Boolean,
|
||||||
postActions: PostActions,
|
postActions: PostActions,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
|
|
|
@ -43,7 +43,7 @@ import kotlinx.coroutines.flow.Flow
|
||||||
fun SearchList(
|
fun SearchList(
|
||||||
items: Flow<PagingData<LobstersPost>>,
|
items: Flow<PagingData<LobstersPost>>,
|
||||||
listState: LazyListState,
|
listState: LazyListState,
|
||||||
isPostSaved: suspend (SavedPost) -> Boolean,
|
isPostSaved: (SavedPost) -> Boolean,
|
||||||
postActions: PostActions,
|
postActions: PostActions,
|
||||||
searchQuery: String,
|
searchQuery: String,
|
||||||
setSearchQuery: (String) -> Unit,
|
setSearchQuery: (String) -> Unit,
|
||||||
|
|
|
@ -44,9 +44,12 @@ import javax.inject.Inject
|
||||||
import kotlinx.collections.immutable.ImmutableMap
|
import kotlinx.collections.immutable.ImmutableMap
|
||||||
import kotlinx.collections.immutable.toImmutableMap
|
import kotlinx.collections.immutable.toImmutableMap
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
@ContributesViewModel
|
@ContributesViewModel
|
||||||
|
@ -99,6 +102,17 @@ constructor(
|
||||||
|
|
||||||
var searchQuery by mutableStateOf("")
|
var searchQuery by mutableStateOf("")
|
||||||
|
|
||||||
|
private val _savedPostsMutex = Mutex()
|
||||||
|
private var _savedPosts = emptyList<String>()
|
||||||
|
|
||||||
|
init {
|
||||||
|
viewModelScope.launch {
|
||||||
|
savedPosts.collectLatest {
|
||||||
|
_savedPostsMutex.withLock { _savedPosts = it.map(SavedPost::shortId) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun mapSavedPosts(items: List<SavedPost>): ImmutableMap<Month, List<SavedPost>> {
|
private fun mapSavedPosts(items: List<SavedPost>): ImmutableMap<Month, List<SavedPost>> {
|
||||||
val sorted =
|
val sorted =
|
||||||
items.sortedWith { post1, post2 ->
|
items.sortedWith { post1, post2 ->
|
||||||
|
@ -107,8 +121,8 @@ constructor(
|
||||||
return sorted.groupBy { post -> post.createdAt.toLocalDateTime().month }.toImmutableMap()
|
return sorted.groupBy { post -> post.createdAt.toLocalDateTime().month }.toImmutableMap()
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun isPostSaved(post: SavedPost): Boolean {
|
fun isPostSaved(post: SavedPost): Boolean {
|
||||||
return savedPosts.first().any { savedPost -> savedPost.shortId == post.shortId }
|
return _savedPosts.contains(post.shortId)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toggleSave(post: SavedPost) {
|
fun toggleSave(post: SavedPost) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue