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 32b514a95..3a05707e0 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 @@ -16,11 +16,13 @@ import java.util.concurrent.atomic.AtomicBoolean import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job +import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext object NotificationViewModel: CardFeedViewModel(NotificationFeedFilter) @@ -123,8 +125,14 @@ open class CardFeedViewModel(val dataSource: FeedFilter): ViewModel() { handlerWaiting.set(true) val scope = CoroutineScope(Job() + Dispatchers.Default) scope.launch { - delay(50) - refresh() + try { + delay(50) + refresh() + } finally { + withContext(NonCancellable) { + handlerWaiting.set(false) + } + } handlerWaiting.set(false) } } 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 8a1627604..23b804089 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 @@ -19,11 +19,13 @@ import java.util.concurrent.atomic.AtomicBoolean import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job +import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext class NostrChannelFeedViewModel: FeedViewModel(ChannelFeedFilter) class NostrChatRoomFeedViewModel: FeedViewModel(ChatroomFeedFilter) @@ -90,9 +92,14 @@ abstract class FeedViewModel(val localFilter: FeedFilter): ViewModel() { handlerWaiting.set(true) val scope = CoroutineScope(Job() + Dispatchers.Default) scope.launch { - delay(50) - refresh() - handlerWaiting.set(false) + try { + delay(50) + refresh() + } finally { + withContext(NonCancellable) { + handlerWaiting.set(false) + } + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/LnZapFeedViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/LnZapFeedViewModel.kt index 4c7ada516..c2b0f9c2a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/LnZapFeedViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/LnZapFeedViewModel.kt @@ -17,6 +17,7 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.util.concurrent.atomic.AtomicBoolean +import kotlinx.coroutines.NonCancellable class NostrUserProfileZapsFeedViewModel: LnZapFeedViewModel(UserProfileZapsFeedFilter) @@ -69,8 +70,14 @@ open class LnZapFeedViewModel(val dataSource: FeedFilter>): Vie handlerWaiting.set(true) val scope = CoroutineScope(Job() + Dispatchers.Default) scope.launch { - delay(50) - refresh() + try { + delay(50) + refresh() + } finally { + withContext(NonCancellable) { + handlerWaiting.set(false) + } + } handlerWaiting.set(false) } } 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 e629f2915..7d0a89b87 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 @@ -18,6 +18,8 @@ import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import java.util.concurrent.atomic.AtomicBoolean +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.withContext class NostrUserProfileFollowsUserFeedViewModel: UserFeedViewModel(UserProfileFollowsFeedFilter) class NostrUserProfileFollowersUserFeedViewModel: UserFeedViewModel(UserProfileFollowersFeedFilter) @@ -72,8 +74,14 @@ open class UserFeedViewModel(val dataSource: FeedFilter): ViewModel() { handlerWaiting.set(true) val scope = CoroutineScope(Job() + Dispatchers.Default) scope.launch { - delay(50) - refresh() + try { + delay(50) + refresh() + } finally { + withContext(NonCancellable) { + handlerWaiting.set(false) + } + } handlerWaiting.set(false) } }