Add threading for the LazyColumn refresh.

This commit is contained in:
Vitor Pamplona
2023-01-18 15:55:35 -05:00
parent 3441f7987b
commit a4885e643e
4 changed files with 38 additions and 44 deletions
@@ -87,14 +87,14 @@ class User(val pubkey: ByteArray) {
updatedFollowsAt = updateAt updatedFollowsAt = updateAt
live.refresh() refreshObservers()
} }
fun updateUserInfo(newUserInfo: UserMetadata, updateAt: Long) { fun updateUserInfo(newUserInfo: UserMetadata, updateAt: Long) {
info = newUserInfo info = newUserInfo
updatedMetadataAt = updateAt updatedMetadataAt = updateAt
live.refresh() refreshObservers()
} }
fun isFollowing(user: User): Boolean { fun isFollowing(user: User): Boolean {
@@ -10,6 +10,9 @@ import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.NostrDataSource import com.vitorpamplona.amethyst.service.NostrDataSource
import com.vitorpamplona.amethyst.service.model.ReactionEvent import com.vitorpamplona.amethyst.service.model.ReactionEvent
import com.vitorpamplona.amethyst.service.model.RepostEvent import com.vitorpamplona.amethyst.service.model.RepostEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
@@ -23,28 +26,24 @@ class CardFeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
private var lastNotes: List<Note>? = null private var lastNotes: List<Note>? = null
fun refresh() { fun refresh() {
// For some reason, view Model Scope doesn't call val scope = CoroutineScope(Job() + Dispatchers.Main)
viewModelScope.launch { scope.launch {
refreshSuspend() val notes = dataSource.loadTop()
}
}
fun refreshSuspend() { val lastNotesCopy = lastNotes
val notes = dataSource.loadTop()
val lastNotesCopy = lastNotes val oldNotesState = feedContent.value
if (lastNotesCopy != null && oldNotesState is CardFeedState.Loaded) {
val oldNotesState = feedContent.value val newCards = convertToCard(notes.minus(lastNotesCopy))
if (lastNotesCopy != null && oldNotesState is CardFeedState.Loaded) { if (newCards.isNotEmpty()) {
val newCards = convertToCard(notes.minus(lastNotesCopy)) lastNotes = notes
if (newCards.isNotEmpty()) { updateFeed((oldNotesState.feed + newCards).sortedBy { it.createdAt() }.reversed())
}
} else {
val cards = convertToCard(notes)
lastNotes = notes lastNotes = notes
updateFeed((oldNotesState.feed + newCards).sortedBy { it.createdAt() }.reversed()) updateFeed(cards)
} }
} else {
val cards = convertToCard(notes)
lastNotes = notes
updateFeed(cards)
} }
} }
@@ -24,23 +24,19 @@ class FeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
private val _feedContent = MutableStateFlow<FeedState>(FeedState.Loading) private val _feedContent = MutableStateFlow<FeedState>(FeedState.Loading)
val feedContent = _feedContent.asStateFlow() val feedContent = _feedContent.asStateFlow()
@OptIn(ExperimentalTime::class)
fun refresh() { fun refresh() {
val scope = CoroutineScope(Job() + Dispatchers.Main) val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch { scope.launch {
println("AAA" + measureTimedValue { val notes = dataSource.loadTop()
val notes = dataSource.loadTop()
val oldNotesState = feedContent.value val oldNotesState = feedContent.value
if (oldNotesState is FeedState.Loaded) { if (oldNotesState is FeedState.Loaded) {
if (notes != oldNotesState.feed) { if (notes != oldNotesState.feed) {
updateFeed(notes)
}
} else {
updateFeed(notes) updateFeed(notes)
} }
}.duration) } else {
updateFeed(notes)
}
} }
} }
@@ -10,6 +10,9 @@ import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.NostrDataSource import com.vitorpamplona.amethyst.service.NostrDataSource
import com.vitorpamplona.amethyst.service.NostrUserProfileFollowersDataSource import com.vitorpamplona.amethyst.service.NostrUserProfileFollowersDataSource
import com.vitorpamplona.amethyst.service.NostrUserProfileFollowsDataSource import com.vitorpamplona.amethyst.service.NostrUserProfileFollowsDataSource
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
@@ -29,22 +32,18 @@ open class UserFeedViewModel(val dataSource: NostrDataSource<User>): ViewModel()
val feedContent = _feedContent.asStateFlow() val feedContent = _feedContent.asStateFlow()
fun refresh() { fun refresh() {
// For some reason, view Model Scope doesn't call val scope = CoroutineScope(Job() + Dispatchers.Main)
viewModelScope.launch { scope.launch {
refreshSuspend() val notes = dataSource.loadTop()
}
}
fun refreshSuspend() { val oldNotesState = feedContent.value
val notes = dataSource.loadTop() if (oldNotesState is UserFeedState.Loaded) {
if (notes != oldNotesState.feed) {
val oldNotesState = feedContent.value updateFeed(notes)
if (oldNotesState is UserFeedState.Loaded) { }
if (notes != oldNotesState.feed) { } else {
updateFeed(notes) updateFeed(notes)
} }
} else {
updateFeed(notes)
} }
} }