mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-10 12:33:10 +02:00
Add threading for the LazyColumn refresh.
This commit is contained in:
@@ -87,14 +87,14 @@ class User(val pubkey: ByteArray) {
|
||||
|
||||
updatedFollowsAt = updateAt
|
||||
|
||||
live.refresh()
|
||||
refreshObservers()
|
||||
}
|
||||
|
||||
fun updateUserInfo(newUserInfo: UserMetadata, updateAt: Long) {
|
||||
info = newUserInfo
|
||||
updatedMetadataAt = updateAt
|
||||
|
||||
live.refresh()
|
||||
refreshObservers()
|
||||
}
|
||||
|
||||
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.model.ReactionEvent
|
||||
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.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
@@ -23,13 +26,8 @@ class CardFeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
|
||||
private var lastNotes: List<Note>? = null
|
||||
|
||||
fun refresh() {
|
||||
// For some reason, view Model Scope doesn't call
|
||||
viewModelScope.launch {
|
||||
refreshSuspend()
|
||||
}
|
||||
}
|
||||
|
||||
fun refreshSuspend() {
|
||||
val scope = CoroutineScope(Job() + Dispatchers.Main)
|
||||
scope.launch {
|
||||
val notes = dataSource.loadTop()
|
||||
|
||||
val lastNotesCopy = lastNotes
|
||||
@@ -47,6 +45,7 @@ class CardFeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
|
||||
updateFeed(cards)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun convertToCard(notes: List<Note>): List<Card> {
|
||||
val reactionsPerEvent = mutableMapOf<Note, MutableList<Note>>()
|
||||
|
@@ -24,11 +24,9 @@ class FeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
|
||||
private val _feedContent = MutableStateFlow<FeedState>(FeedState.Loading)
|
||||
val feedContent = _feedContent.asStateFlow()
|
||||
|
||||
@OptIn(ExperimentalTime::class)
|
||||
fun refresh() {
|
||||
val scope = CoroutineScope(Job() + Dispatchers.Main)
|
||||
scope.launch {
|
||||
println("AAA" + measureTimedValue {
|
||||
val notes = dataSource.loadTop()
|
||||
|
||||
val oldNotesState = feedContent.value
|
||||
@@ -39,8 +37,6 @@ class FeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
|
||||
} else {
|
||||
updateFeed(notes)
|
||||
}
|
||||
}.duration)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -10,6 +10,9 @@ import com.vitorpamplona.amethyst.model.User
|
||||
import com.vitorpamplona.amethyst.service.NostrDataSource
|
||||
import com.vitorpamplona.amethyst.service.NostrUserProfileFollowersDataSource
|
||||
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.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
@@ -29,13 +32,8 @@ open class UserFeedViewModel(val dataSource: NostrDataSource<User>): ViewModel()
|
||||
val feedContent = _feedContent.asStateFlow()
|
||||
|
||||
fun refresh() {
|
||||
// For some reason, view Model Scope doesn't call
|
||||
viewModelScope.launch {
|
||||
refreshSuspend()
|
||||
}
|
||||
}
|
||||
|
||||
fun refreshSuspend() {
|
||||
val scope = CoroutineScope(Job() + Dispatchers.Main)
|
||||
scope.launch {
|
||||
val notes = dataSource.loadTop()
|
||||
|
||||
val oldNotesState = feedContent.value
|
||||
@@ -47,6 +45,7 @@ open class UserFeedViewModel(val dataSource: NostrDataSource<User>): ViewModel()
|
||||
updateFeed(notes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateFeed(notes: List<User>) {
|
||||
if (notes.isEmpty()) {
|
||||
|
Reference in New Issue
Block a user