mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-15 09:57:04 +05:30
android: cleanup ClawViewModel
This commit is contained in:
parent
06ea10f3b3
commit
b6598436b2
1 changed files with 5 additions and 11 deletions
|
@ -8,13 +8,12 @@ import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import dev.msfjarvis.claw.android.paging.LobstersPagingSource
|
import dev.msfjarvis.claw.android.paging.LobstersPagingSource
|
||||||
import dev.msfjarvis.claw.api.LobstersApi
|
import dev.msfjarvis.claw.api.LobstersApi
|
||||||
import dev.msfjarvis.claw.database.local.SavedPost
|
import dev.msfjarvis.claw.database.local.SavedPost
|
||||||
import dev.msfjarvis.claw.model.LobstersPostDetails
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.flow.collect
|
import kotlinx.coroutines.flow.collect
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import kotlinx.coroutines.flow.last
|
|
||||||
import kotlinx.coroutines.flow.mapLatest
|
import kotlinx.coroutines.flow.mapLatest
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
@ -27,7 +26,7 @@ constructor(
|
||||||
private val api: LobstersApi,
|
private val api: LobstersApi,
|
||||||
private val repository: SavedPostsRepository,
|
private val repository: SavedPostsRepository,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
var lastPagingSource: LobstersPagingSource? = null
|
private var lastPagingSource: LobstersPagingSource? = null
|
||||||
private val savedPosts = flow { repository.savedPosts.collect { emit(it) } }
|
private val savedPosts = flow { repository.savedPosts.collect { emit(it) } }
|
||||||
private val pager =
|
private val pager =
|
||||||
Pager(PagingConfig(20)) {
|
Pager(PagingConfig(20)) {
|
||||||
|
@ -37,14 +36,11 @@ constructor(
|
||||||
val pagerFlow
|
val pagerFlow
|
||||||
get() = pager.flow
|
get() = pager.flow
|
||||||
|
|
||||||
suspend fun isPostSaved(post: SavedPost): Boolean {
|
suspend fun isPostSaved(post: SavedPost) = savedPosts.mapLatest { posts -> post in posts }.first()
|
||||||
return savedPosts.mapLatest { posts -> post in posts }.last()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun toggleSave(post: SavedPost) {
|
fun toggleSave(post: SavedPost) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val saved = isPostSaved(post)
|
val saved = isPostSaved(post)
|
||||||
println("saved=$saved")
|
|
||||||
if (saved) {
|
if (saved) {
|
||||||
repository.removePost(post)
|
repository.removePost(post)
|
||||||
} else {
|
} else {
|
||||||
|
@ -53,10 +49,8 @@ constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getPostComments(postId: String): LobstersPostDetails =
|
suspend fun getPostComments(postId: String) =
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) { api.getPostDetails(postId) }
|
||||||
return@withContext api.getPostDetails(postId)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun reloadPosts() {
|
fun reloadPosts() {
|
||||||
lastPagingSource?.invalidate()
|
lastPagingSource?.invalidate()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue