Moves collection events to an IO thread.

This commit is contained in:
Vitor Pamplona
2023-06-07 12:05:45 -04:00
parent e9374370b9
commit 03d1754498
5 changed files with 38 additions and 12 deletions
@@ -36,6 +36,7 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.TextSpinner
import kotlinx.collections.immutable.toImmutableList import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@Composable @Composable
fun NewMediaView(uri: Uri, onClose: () -> Unit, postViewModel: NewMediaModel, accountViewModel: AccountViewModel, nav: (String) -> Unit) { 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) { LaunchedEffect(uri) {
val mediaType = resolver.getType(uri) ?: "" val mediaType = resolver.getType(uri) ?: ""
postViewModel.load(account, uri, mediaType) 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()
}
}
} }
} }
@@ -76,6 +76,7 @@ import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
@Composable @Composable
@@ -99,8 +100,12 @@ fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
delay(100) delay(100)
focusRequester.requestFocus() focusRequester.requestFocus()
postViewModel.imageUploadingError.collect { error -> launch(Dispatchers.IO) {
Toast.makeText(context, error, Toast.LENGTH_SHORT).show() postViewModel.imageUploadingError.collect { error ->
withContext(Dispatchers.Main) {
Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
}
}
} }
} }
@@ -28,6 +28,9 @@ import androidx.compose.ui.window.DialogProperties
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@Composable @Composable
fun NewUserMetadataView(onClose: () -> Unit, account: Account) { fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
@@ -37,8 +40,12 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
postViewModel.load(account) postViewModel.load(account)
postViewModel.imageUploadingError.collect { error -> launch(Dispatchers.IO) {
Toast.makeText(context, error, Toast.LENGTH_SHORT).show() postViewModel.imageUploadingError.collect { error ->
withContext(Dispatchers.Main) {
Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
}
}
} }
} }
@@ -148,10 +148,14 @@ fun ChannelScreen(
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
NostrChannelDataSource.start() NostrChannelDataSource.start()
feedViewModel.invalidateData() 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()
}
}
} }
} }
@@ -106,10 +106,14 @@ fun ChatroomScreen(
LaunchedEffect(baseUser, accountViewModel) { LaunchedEffect(baseUser, accountViewModel) {
NostrChatroomDataSource.start() NostrChatroomDataSource.start()
feedViewModel.invalidateData() 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()
}
}
} }
} }