diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/CardFeedViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/CardFeedViewModel.kt index 947c46cce..7836b1d52 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/CardFeedViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/CardFeedViewModel.kt @@ -82,15 +82,18 @@ class CardFeedViewModel(val dataSource: NostrDataSource): ViewModel() { } private fun updateFeed(notes: List) { - val currentState = feedContent.value + val scope = CoroutineScope(Job() + Dispatchers.Main) + scope.launch { + val currentState = feedContent.value - if (notes.isEmpty()) { - _feedContent.update { CardFeedState.Empty } - } else if (currentState is CardFeedState.Loaded) { - // updates the current list - currentState.feed.value = notes - } else { - _feedContent.update { CardFeedState.Loaded(mutableStateOf(notes)) } + if (notes.isEmpty()) { + _feedContent.update { CardFeedState.Empty } + } else if (currentState is CardFeedState.Loaded) { + // updates the current list + currentState.feed.value = notes + } else { + _feedContent.update { CardFeedState.Loaded(mutableStateOf(notes)) } + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/FeedViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/FeedViewModel.kt index d2332bb5d..7af88399a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/FeedViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/FeedViewModel.kt @@ -89,10 +89,14 @@ abstract class FeedViewModel(val dataSource: NostrDataSource): ViewModel() val oldNotesState = feedContent.value if (oldNotesState is FeedState.Loaded) { if (notes != oldNotesState.feed) { - updateFeed(notes) + withContext(Dispatchers.Main) { + updateFeed(notes) + } } } else { - updateFeed(notes) + withContext(Dispatchers.Main) { + updateFeed(notes) + } } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/UserFeedViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/UserFeedViewModel.kt index cbe938f54..9ba891cd6 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/UserFeedViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/UserFeedViewModel.kt @@ -60,14 +60,17 @@ open class UserFeedViewModel(val dataSource: NostrDataSource): ViewModel() } private fun updateFeed(notes: List) { - val currentState = feedContent.value - if (notes.isEmpty()) { - _feedContent.update { UserFeedState.Empty } - } else if (currentState is UserFeedState.Loaded) { - // updates the current list - currentState.feed.value = notes - } else { - _feedContent.update { UserFeedState.Loaded(mutableStateOf(notes)) } + val scope = CoroutineScope(Job() + Dispatchers.Main) + scope.launch { + val currentState = feedContent.value + if (notes.isEmpty()) { + _feedContent.update { UserFeedState.Empty } + } else if (currentState is UserFeedState.Loaded) { + // updates the current list + currentState.feed.value = notes + } else { + _feedContent.update { UserFeedState.Loaded(mutableStateOf(notes)) } + } } }