From e04843fe963d80b443c4dbf798151ac54da37bff Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Sun, 5 Feb 2023 18:39:54 -0500 Subject: [PATCH] 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. --- .../amethyst/ui/screen/CardFeedViewModel.kt | 19 +++++++++++-------- .../amethyst/ui/screen/FeedViewModel.kt | 8 ++++++-- .../amethyst/ui/screen/UserFeedViewModel.kt | 19 +++++++++++-------- 3 files changed, 28 insertions(+), 18 deletions(-) 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)) } + } } }