android: reduce transformation steps for saved state check

This commit is contained in:
Harsh Shandilya 2022-05-14 19:31:47 +05:30
parent 4124cdcd1b
commit 733cae4815
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80

View file

@ -12,14 +12,11 @@ import dev.msfjarvis.claw.database.local.SavedPost
import java.time.Month import java.time.Month
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@OptIn(ExperimentalCoroutinesApi::class)
@HiltViewModel @HiltViewModel
class ClawViewModel class ClawViewModel
@Inject @Inject
@ -56,10 +53,7 @@ constructor(
} }
suspend fun isPostSaved(post: SavedPost): Boolean { suspend fun isPostSaved(post: SavedPost): Boolean {
return savedPostsFlow return savedPostsFlow.first().any { savedPost -> savedPost.shortId == post.shortId }
.mapLatest { posts -> posts.map { it.shortId } }
.mapLatest { shortIds -> post.shortId in shortIds }
.first()
} }
fun toggleSave(post: SavedPost) { fun toggleSave(post: SavedPost) {