mirror of
https://github.com/readrops/Readrops.git
synced 2025-02-01 19:26:49 +01:00
Better react to account change in TimelineViewModel
This commit is contained in:
parent
b15eb9fa91
commit
3d6cbfe65f
@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
|
||||
import com.readrops.app.compose.repositories.BaseRepository
|
||||
import com.readrops.db.Database
|
||||
import com.readrops.db.entities.account.Account
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.core.component.KoinComponent
|
||||
@ -15,7 +16,7 @@ import org.koin.core.parameter.parametersOf
|
||||
* Custom ViewModel for Tab screens handling account change
|
||||
*/
|
||||
abstract class TabViewModel(
|
||||
private val database: Database,
|
||||
private val database: Database,
|
||||
) : ViewModel(), KoinComponent {
|
||||
|
||||
/**
|
||||
@ -25,24 +26,21 @@ abstract class TabViewModel(
|
||||
|
||||
protected var currentAccount: Account? = null
|
||||
|
||||
/**
|
||||
* This method is called when the repository has been rebuilt from the new current account
|
||||
*/
|
||||
abstract fun invalidate()
|
||||
protected val accountEvent = Channel<Account>()
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
database.newAccountDao()
|
||||
.selectCurrentAccount()
|
||||
.distinctUntilChanged()
|
||||
.collect { account ->
|
||||
if (account != null) {
|
||||
currentAccount = account
|
||||
repository = get(parameters = { parametersOf(account) })
|
||||
.selectCurrentAccount()
|
||||
.distinctUntilChanged()
|
||||
.collect { account ->
|
||||
if (account != null) {
|
||||
currentAccount = account
|
||||
repository = get(parameters = { parametersOf(account) })
|
||||
|
||||
invalidate()
|
||||
}
|
||||
accountEvent.send(account)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
package com.readrops.app.compose.feeds
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.readrops.app.compose.base.TabViewModel
|
||||
import com.readrops.app.compose.repositories.BaseRepository
|
||||
import com.readrops.db.Database
|
||||
import com.readrops.db.entities.Feed
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@ -32,10 +30,6 @@ class FeedViewModel(
|
||||
repository?.insertNewFeeds(listOf(url))
|
||||
}
|
||||
}
|
||||
|
||||
override fun invalidate() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
sealed class FeedsState {
|
||||
|
@ -6,14 +6,18 @@ import com.readrops.db.Database
|
||||
import com.readrops.db.pojo.ItemWithFeed
|
||||
import com.readrops.db.queries.ItemsQueryBuilder
|
||||
import com.readrops.db.queries.QueryFilters
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.consumeAsFlow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class TimelineViewModel(
|
||||
private val database: Database,
|
||||
private val database: Database,
|
||||
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
|
||||
) : TabViewModel(database) {
|
||||
|
||||
private val _timelineState = MutableStateFlow<TimelineState>(TimelineState.Loading)
|
||||
@ -23,20 +27,22 @@ class TimelineViewModel(
|
||||
val isRefreshing = _isRefreshing.asStateFlow()
|
||||
|
||||
init {
|
||||
viewModelScope.launch(context = Dispatchers.IO) {
|
||||
val query = ItemsQueryBuilder.buildItemsQuery(QueryFilters(accountId = 1))
|
||||
viewModelScope.launch(dispatcher) {
|
||||
accountEvent.consumeAsFlow().collectLatest { account ->
|
||||
val query = ItemsQueryBuilder.buildItemsQuery(QueryFilters(accountId = account.id))
|
||||
|
||||
database.newItemDao().selectAll(query)
|
||||
database.newItemDao().selectAll(query)
|
||||
.catch { _timelineState.value = TimelineState.Error(Exception(it)) }
|
||||
.collect {
|
||||
_timelineState.value = TimelineState.Loaded(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun refreshTimeline() {
|
||||
_isRefreshing.value = true
|
||||
viewModelScope.launch(context = Dispatchers.IO) {
|
||||
viewModelScope.launch(dispatcher) {
|
||||
repository?.synchronize(null) {
|
||||
|
||||
}
|
||||
@ -44,10 +50,6 @@ class TimelineViewModel(
|
||||
_isRefreshing.value = false
|
||||
}
|
||||
}
|
||||
|
||||
override fun invalidate() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
sealed class TimelineState {
|
||||
|
Loading…
x
Reference in New Issue
Block a user