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

View File

@@ -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 {

View File

@@ -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,13 +26,8 @@ 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()
}
}
fun refreshSuspend() {
val notes = dataSource.loadTop() val notes = dataSource.loadTop()
val lastNotesCopy = lastNotes val lastNotesCopy = lastNotes
@@ -47,6 +45,7 @@ class CardFeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
updateFeed(cards) updateFeed(cards)
} }
} }
}
private fun convertToCard(notes: List<Note>): List<Card> { private fun convertToCard(notes: List<Note>): List<Card> {
val reactionsPerEvent = mutableMapOf<Note, MutableList<Note>>() val reactionsPerEvent = mutableMapOf<Note, MutableList<Note>>()

View File

@@ -24,11 +24,9 @@ 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
@@ -39,8 +37,6 @@ class FeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
} else { } else {
updateFeed(notes) updateFeed(notes)
} }
}.duration)
} }
} }

View File

@@ -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,13 +32,8 @@ 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()
}
}
fun refreshSuspend() {
val notes = dataSource.loadTop() val notes = dataSource.loadTop()
val oldNotesState = feedContent.value val oldNotesState = feedContent.value
@@ -47,6 +45,7 @@ open class UserFeedViewModel(val dataSource: NostrDataSource<User>): ViewModel()
updateFeed(notes) updateFeed(notes)
} }
} }
}
fun updateFeed(notes: List<User>) { fun updateFeed(notes: List<User>) {
if (notes.isEmpty()) { if (notes.isEmpty()) {