From 03d1754498125dc6d76311d4c81057dd002d5dfd Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 7 Jun 2023 12:05:45 -0400 Subject: [PATCH] Moves collection events to an IO thread. --- .../vitorpamplona/amethyst/ui/actions/NewMediaView.kt | 10 ++++++++-- .../vitorpamplona/amethyst/ui/actions/NewPostView.kt | 9 +++++++-- .../amethyst/ui/actions/NewUserMetadataView.kt | 11 +++++++++-- .../amethyst/ui/screen/loggedIn/ChannelScreen.kt | 10 +++++++--- .../amethyst/ui/screen/loggedIn/ChatroomScreen.kt | 10 +++++++--- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaView.kt index 16a8e94ab..c68db2944 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewMediaView.kt @@ -36,6 +36,7 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.TextSpinner import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @Composable fun NewMediaView(uri: Uri, onClose: () -> Unit, postViewModel: NewMediaModel, accountViewModel: AccountViewModel, nav: (String) -> Unit) { @@ -48,8 +49,13 @@ fun NewMediaView(uri: Uri, onClose: () -> Unit, postViewModel: NewMediaModel, ac LaunchedEffect(uri) { val mediaType = resolver.getType(uri) ?: "" postViewModel.load(account, uri, mediaType) - postViewModel.imageUploadingError.collect { error -> - Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + + launch(Dispatchers.IO) { + postViewModel.imageUploadingError.collect { error -> + withContext(Dispatchers.Main) { + Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + } + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt index b263e038a..9a3837e3a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt @@ -76,6 +76,7 @@ import kotlinx.collections.immutable.toImmutableList import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @OptIn(ExperimentalComposeUiApi::class) @Composable @@ -99,8 +100,12 @@ fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n delay(100) focusRequester.requestFocus() - postViewModel.imageUploadingError.collect { error -> - Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + launch(Dispatchers.IO) { + postViewModel.imageUploadingError.collect { error -> + withContext(Dispatchers.Main) { + Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + } + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewUserMetadataView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewUserMetadataView.kt index 130c20f83..0d7e83254 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewUserMetadataView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewUserMetadataView.kt @@ -28,6 +28,9 @@ import androidx.compose.ui.window.DialogProperties import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.Account +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @Composable fun NewUserMetadataView(onClose: () -> Unit, account: Account) { @@ -37,8 +40,12 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) { LaunchedEffect(Unit) { postViewModel.load(account) - postViewModel.imageUploadingError.collect { error -> - Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + launch(Dispatchers.IO) { + postViewModel.imageUploadingError.collect { error -> + withContext(Dispatchers.Main) { + Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + } + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt index 5ca792f29..9394b7a71 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt @@ -148,10 +148,14 @@ fun ChannelScreen( LaunchedEffect(Unit) { NostrChannelDataSource.start() - feedViewModel.invalidateData() - newPostModel.imageUploadingError.collect { error -> - Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + + launch(Dispatchers.IO) { + newPostModel.imageUploadingError.collect { error -> + withContext(Dispatchers.Main) { + Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + } + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt index 1d818890a..8fa3046de 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt @@ -106,10 +106,14 @@ fun ChatroomScreen( LaunchedEffect(baseUser, accountViewModel) { NostrChatroomDataSource.start() - feedViewModel.invalidateData() - newPostModel.imageUploadingError.collect { error -> - Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + + launch(Dispatchers.IO) { + newPostModel.imageUploadingError.collect { error -> + withContext(Dispatchers.Main) { + Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + } + } } }