Using Main to update screen seems needed otherwise I get a 1 minute delay in the loading page before the screen updates. It looks like it waits for all the Pictures to load. Not sure why.

This commit is contained in:
Vitor Pamplona
2023-02-05 18:39:54 -05:00
parent bb50099021
commit e04843fe96
3 changed files with 28 additions and 18 deletions

View File

@@ -82,15 +82,18 @@ class CardFeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel() {
} }
private fun updateFeed(notes: List<Card>) { private fun updateFeed(notes: List<Card>) {
val currentState = feedContent.value val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch {
val currentState = feedContent.value
if (notes.isEmpty()) { if (notes.isEmpty()) {
_feedContent.update { CardFeedState.Empty } _feedContent.update { CardFeedState.Empty }
} else if (currentState is CardFeedState.Loaded) { } else if (currentState is CardFeedState.Loaded) {
// updates the current list // updates the current list
currentState.feed.value = notes currentState.feed.value = notes
} else { } else {
_feedContent.update { CardFeedState.Loaded(mutableStateOf(notes)) } _feedContent.update { CardFeedState.Loaded(mutableStateOf(notes)) }
}
} }
} }

View File

@@ -89,10 +89,14 @@ abstract class FeedViewModel(val dataSource: NostrDataSource<Note>): ViewModel()
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) withContext(Dispatchers.Main) {
updateFeed(notes)
}
} }
} else { } else {
updateFeed(notes) withContext(Dispatchers.Main) {
updateFeed(notes)
}
} }
} }
} }

View File

@@ -60,14 +60,17 @@ open class UserFeedViewModel(val dataSource: NostrDataSource<User>): ViewModel()
} }
private fun updateFeed(notes: List<User>) { private fun updateFeed(notes: List<User>) {
val currentState = feedContent.value val scope = CoroutineScope(Job() + Dispatchers.Main)
if (notes.isEmpty()) { scope.launch {
_feedContent.update { UserFeedState.Empty } val currentState = feedContent.value
} else if (currentState is UserFeedState.Loaded) { if (notes.isEmpty()) {
// updates the current list _feedContent.update { UserFeedState.Empty }
currentState.feed.value = notes } else if (currentState is UserFeedState.Loaded) {
} else { // updates the current list
_feedContent.update { UserFeedState.Loaded(mutableStateOf(notes)) } currentState.feed.value = notes
} else {
_feedContent.update { UserFeedState.Loaded(mutableStateOf(notes)) }
}
} }
} }