From 3ce5d925566708e31a5b0b6a817f47f44fcbb89d Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Sun, 1 Oct 2023 11:42:31 -0400 Subject: [PATCH] Making sure shared flows don't suspend. --- .../main/java/com/vitorpamplona/amethyst/model/LocalCache.kt | 3 ++- .../com/vitorpamplona/amethyst/ui/actions/NewMediaModel.kt | 3 ++- .../com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt | 3 ++- .../amethyst/ui/actions/NewUserMetadataViewModel.kt | 3 ++- .../amethyst/ui/screen/loggedIn/AccountViewModel.kt | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt index ca20bf549..97fd92232 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt @@ -22,6 +22,7 @@ import kotlinx.collections.immutable.persistentSetOf import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableSet import kotlinx.coroutines.* +import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.asSharedFlow import java.io.File @@ -1553,7 +1554,7 @@ object LocalCache { @Stable class LocalCacheLiveData { - private val _newEventBundles = MutableSharedFlow>() + private val _newEventBundles = MutableSharedFlow>(0, 10, BufferOverflow.DROP_OLDEST) val newEventBundles = _newEventBundles.asSharedFlow() // read-only public view // Refreshes observers in batches. diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaModel.kt index 580a13c35..47f1a6bbb 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaModel.kt @@ -14,6 +14,7 @@ import com.vitorpamplona.amethyst.service.FileHeader import com.vitorpamplona.amethyst.service.relays.Relay import com.vitorpamplona.amethyst.ui.components.MediaCompressor import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.launch @@ -22,7 +23,7 @@ open class NewMediaModel : ViewModel() { var account: Account? = null var isUploadingImage by mutableStateOf(false) - val imageUploadingError = MutableSharedFlow() + val imageUploadingError = MutableSharedFlow(0, 3, onBufferOverflow = BufferOverflow.DROP_OLDEST) var mediaType by mutableStateOf(null) var selectedServer by mutableStateOf(null) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt index 81f65d224..783cf824d 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt @@ -33,6 +33,7 @@ import com.vitorpamplona.quartz.events.PrivateDmEvent import com.vitorpamplona.quartz.events.TextNoteEvent import com.vitorpamplona.quartz.events.ZapSplitSetup import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.mapLatest @@ -58,7 +59,7 @@ open class NewPostViewModel() : ViewModel() { var message by mutableStateOf(TextFieldValue("")) var urlPreview by mutableStateOf(null) var isUploadingImage by mutableStateOf(false) - val imageUploadingError = MutableSharedFlow() + val imageUploadingError = MutableSharedFlow(0, 3, onBufferOverflow = BufferOverflow.DROP_OLDEST) var userSuggestions by mutableStateOf>(emptyList()) var userSuggestionAnchor: TextRange? = null diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewUserMetadataViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewUserMetadataViewModel.kt index 212ecbbe5..5736190e0 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewUserMetadataViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewUserMetadataViewModel.kt @@ -15,6 +15,7 @@ import com.vitorpamplona.quartz.events.GitHubIdentity import com.vitorpamplona.quartz.events.MastodonIdentity import com.vitorpamplona.quartz.events.TwitterIdentity import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.launch import java.io.ByteArrayInputStream @@ -41,7 +42,7 @@ class NewUserMetadataViewModel : ViewModel() { var isUploadingImageForPicture by mutableStateOf(false) var isUploadingImageForBanner by mutableStateOf(false) - val imageUploadingError = MutableSharedFlow() + val imageUploadingError = MutableSharedFlow(0, 3, onBufferOverflow = BufferOverflow.DROP_OLDEST) fun load(account: Account) { this.account = account diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index dcc033aaa..8a82a2567 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -60,6 +60,7 @@ import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableSet import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job +import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.launch @@ -85,7 +86,7 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao { val userFollows: LiveData = account.userProfile().live().follows.map { it } val userRelays: LiveData = account.userProfile().live().relays.map { it } - val toasts = MutableSharedFlow() + val toasts = MutableSharedFlow(0, 3, onBufferOverflow = BufferOverflow.DROP_OLDEST) val discoveryListLiveData = account.live.map { it.account.defaultDiscoveryFollowList