Moves UserFeed to MutableLists

This commit is contained in:
Vitor Pamplona
2023-06-02 21:52:22 -04:00
parent 515159f18c
commit bbef06de59
2 changed files with 7 additions and 4 deletions
@@ -2,10 +2,11 @@ package com.vitorpamplona.amethyst.ui.screen
import androidx.compose.runtime.MutableState
import com.vitorpamplona.amethyst.model.User
import kotlinx.collections.immutable.ImmutableList
sealed class UserFeedState {
object Loading : UserFeedState()
class Loaded(val feed: MutableState<List<User>>) : UserFeedState()
class Loaded(val feed: MutableState<ImmutableList<User>>) : UserFeedState()
object Empty : UserFeedState()
class FeedError(val errorMessage: String) : UserFeedState()
}
@@ -12,6 +12,8 @@ import com.vitorpamplona.amethyst.ui.dal.FeedFilter
import com.vitorpamplona.amethyst.ui.dal.HiddenAccountsFeedFilter
import com.vitorpamplona.amethyst.ui.dal.UserProfileFollowersFeedFilter
import com.vitorpamplona.amethyst.ui.dal.UserProfileFollowsFeedFilter
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
@@ -56,12 +58,12 @@ open class UserFeedViewModel(val dataSource: FeedFilter<User>) : ViewModel() {
}
private fun refreshSuspended() {
val notes = dataSource.loadTop()
val notes = dataSource.loadTop().toImmutableList()
val oldNotesState = _feedContent.value
if (oldNotesState is UserFeedState.Loaded) {
// Using size as a proxy for has changed.
if (notes != oldNotesState.feed.value) {
if (!equalImmutableLists(notes, oldNotesState.feed.value)) {
updateFeed(notes)
}
} else {
@@ -69,7 +71,7 @@ open class UserFeedViewModel(val dataSource: FeedFilter<User>) : ViewModel() {
}
}
private fun updateFeed(notes: List<User>) {
private fun updateFeed(notes: ImmutableList<User>) {
val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch {
val currentState = _feedContent.value