mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 23:27:04 +05:30
refactor: improve read posts search performance
This commit is contained in:
parent
89b821ebf1
commit
cbd7f2fca4
6 changed files with 31 additions and 44 deletions
|
@ -57,8 +57,6 @@ import kotlinx.coroutines.flow.first
|
|||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.shareIn
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@ContributesViewModel
|
||||
|
@ -84,7 +82,7 @@ constructor(
|
|||
pagingSourceFactory = { pagingSourceFactory.create(api::getHottestPosts) },
|
||||
)
|
||||
.flow
|
||||
.map(::mapUIPost)
|
||||
.map(::mapToUIPost)
|
||||
.cachedIn(viewModelScope)
|
||||
|
||||
val newestPosts =
|
||||
|
@ -94,7 +92,7 @@ constructor(
|
|||
pagingSourceFactory = { pagingSourceFactory.create(api::getNewestPosts) },
|
||||
)
|
||||
.flow
|
||||
.map(::mapUIPost)
|
||||
.map(::mapToUIPost)
|
||||
.cachedIn(viewModelScope)
|
||||
val searchResults =
|
||||
Pager(
|
||||
|
@ -103,38 +101,33 @@ constructor(
|
|||
pagingSourceFactory = { searchPagingSourceFactory.create { searchQuery } },
|
||||
)
|
||||
.flow
|
||||
.map(::mapUIPost)
|
||||
.map(::mapToUIPost)
|
||||
|
||||
val savedPosts
|
||||
get() =
|
||||
savedPostsRepository.savedPosts
|
||||
.map { it.map(UIPost.Companion::fromSavedPost) }
|
||||
.shareIn(viewModelScope, started = SharingStarted.Lazily, Int.MAX_VALUE)
|
||||
val savedPosts =
|
||||
savedPostsRepository.savedPosts
|
||||
.map { it.map(UIPost.Companion::fromSavedPost) }
|
||||
.shareIn(viewModelScope, started = SharingStarted.Lazily, Int.MAX_VALUE)
|
||||
|
||||
val savedPostsByMonth
|
||||
get() = savedPosts.map(::mapSavedPosts)
|
||||
val savedPostsByMonth = savedPosts.map(::groupSavedPosts)
|
||||
|
||||
var searchQuery by mutableStateOf("")
|
||||
|
||||
private val _savedPostsMutex = Mutex()
|
||||
private var _readPosts = emptyList<String>()
|
||||
private var _savedPosts = emptyList<String>()
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
savedPosts.collectLatest {
|
||||
_savedPostsMutex.withLock { _savedPosts = it.map(UIPost::shortId) }
|
||||
}
|
||||
}
|
||||
viewModelScope.launch { savedPosts.collectLatest { _savedPosts = it.map(UIPost::shortId) } }
|
||||
viewModelScope.launch { readPostsRepository.readPosts.collectLatest { _readPosts = it } }
|
||||
}
|
||||
|
||||
private fun mapUIPost(pagingData: PagingData<LobstersPost>): PagingData<UIPost> {
|
||||
private fun mapToUIPost(pagingData: PagingData<LobstersPost>): PagingData<UIPost> {
|
||||
return pagingData.map { post ->
|
||||
val uiPost = post.toUIPost()
|
||||
uiPost.copy(isSaved = isPostSaved(uiPost), isRead = isPostRead(uiPost))
|
||||
}
|
||||
}
|
||||
|
||||
private fun mapSavedPosts(items: List<UIPost>): ImmutableMap<String, List<UIPost>> {
|
||||
private fun groupSavedPosts(items: List<UIPost>): ImmutableMap<String, List<UIPost>> {
|
||||
val sorted =
|
||||
items.sortedWith { post1, post2 ->
|
||||
val post1Date = post1.createdAt.toLocalDateTime()
|
||||
|
@ -159,7 +152,9 @@ constructor(
|
|||
return _savedPosts.contains(post.shortId)
|
||||
}
|
||||
|
||||
private fun isPostRead(post: UIPost) = readPostsRepository.isRead(post.shortId)
|
||||
private fun isPostRead(post: UIPost): Boolean {
|
||||
return _readPosts.contains(post.shortId)
|
||||
}
|
||||
|
||||
fun toggleSave(post: UIPost) {
|
||||
viewModelScope.launch(ioDispatcher) {
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
*/
|
||||
package dev.msfjarvis.claw.android.viewmodel
|
||||
|
||||
import app.cash.sqldelight.coroutines.asFlow
|
||||
import app.cash.sqldelight.coroutines.mapToList
|
||||
import dev.msfjarvis.claw.core.injection.DatabaseDispatcher
|
||||
import dev.msfjarvis.claw.database.local.ReadPostsQueries
|
||||
import javax.inject.Inject
|
||||
|
@ -18,12 +20,9 @@ constructor(
|
|||
private val readPostsQueries: ReadPostsQueries,
|
||||
@DatabaseDispatcher private val dbDispatcher: CoroutineDispatcher,
|
||||
) {
|
||||
val readPosts = readPostsQueries.selectAllPosts().asFlow().mapToList(dbDispatcher)
|
||||
|
||||
suspend fun markRead(postId: String) {
|
||||
withContext(dbDispatcher) { readPostsQueries.markRead(postId) }
|
||||
}
|
||||
|
||||
fun isRead(postId: String): Boolean {
|
||||
return readPostsQueries.isRead(postId).executeAsOneOrNull() != null
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue