From 12c330f8f62255fa4c5defcc78128ac19ae96f05 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 6 Feb 2023 18:15:37 -0500 Subject: [PATCH] Correctly handling Atomic Boolean --- .../amethyst/ui/screen/CardFeedViewModel.kt | 22 +++++++++---------- .../amethyst/ui/screen/FeedViewModel.kt | 16 +++++++------- .../amethyst/ui/screen/UserFeedViewModel.kt | 2 +- 3 files changed, 20 insertions(+), 20 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 5d6b9fa72..2c5ed4370 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 @@ -13,6 +13,7 @@ import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent import com.vitorpamplona.amethyst.service.model.ReactionEvent import com.vitorpamplona.amethyst.service.model.RepostEvent +import java.util.concurrent.atomic.AtomicBoolean import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -99,19 +100,18 @@ class CardFeedViewModel(val dataSource: NostrDataSource): ViewModel() { } } + var handlerWaiting = AtomicBoolean() - var handlerWaiting = false - fun invalidateData() { - synchronized(handlerWaiting) { - if (handlerWaiting) return + @Synchronized + private fun invalidateData() { + if (handlerWaiting.getAndSet(true)) return - handlerWaiting = true - val scope = CoroutineScope(Job() + Dispatchers.Default) - scope.launch { - delay(100) - refresh() - handlerWaiting = false - } + handlerWaiting.set(true) + val scope = CoroutineScope(Job() + Dispatchers.Default) + scope.launch { + delay(100) + refresh() + 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 1a417869a..033ff7834 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 @@ -117,15 +117,15 @@ abstract class FeedViewModel(val dataSource: NostrDataSource): ViewModel() private var handlerWaiting = AtomicBoolean() @Synchronized private fun invalidateData() { - if (handlerWaiting.get()) return + if (handlerWaiting.getAndSet(true)) return - handlerWaiting.set(true) - val scope = CoroutineScope(Job() + Dispatchers.Default) - scope.launch { - delay(100) - refresh() - handlerWaiting.set(false) - } + handlerWaiting.set(true) + val scope = CoroutineScope(Job() + Dispatchers.Default) + scope.launch { + delay(100) + refresh() + handlerWaiting.set(false) + } } private val cacheListener: (LocalCacheState) -> Unit = { 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 d6229751e..495e635ab 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 @@ -81,7 +81,7 @@ open class UserFeedViewModel(val dataSource: NostrDataSource): ViewModel() @Synchronized private fun invalidateData() { - if (handlerWaiting.get()) return + if (handlerWaiting.getAndSet(true)) return handlerWaiting.set(true) val scope = CoroutineScope(Job() + Dispatchers.Default)