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,10 +49,15 @@ 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)
launch(Dispatchers.IO) {
postViewModel.imageUploadingError.collect { error -> postViewModel.imageUploadingError.collect { error ->
withContext(Dispatchers.Main) {
Toast.makeText(context, error, Toast.LENGTH_SHORT).show() Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
} }
} }
}
}
Dialog( Dialog(
onDismissRequest = { onClose() }, onDismissRequest = { onClose() },
@@ -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,10 +100,14 @@ fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
delay(100) delay(100)
focusRequester.requestFocus() focusRequester.requestFocus()
launch(Dispatchers.IO) {
postViewModel.imageUploadingError.collect { error -> postViewModel.imageUploadingError.collect { error ->
withContext(Dispatchers.Main) {
Toast.makeText(context, error, Toast.LENGTH_SHORT).show() Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
} }
} }
}
}
DisposableEffect(Unit) { DisposableEffect(Unit) {
onDispose { onDispose {
@@ -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,10 +40,14 @@ fun NewUserMetadataView(onClose: () -> Unit, account: Account) {
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
postViewModel.load(account) postViewModel.load(account)
launch(Dispatchers.IO) {
postViewModel.imageUploadingError.collect { error -> postViewModel.imageUploadingError.collect { error ->
withContext(Dispatchers.Main) {
Toast.makeText(context, error, Toast.LENGTH_SHORT).show() Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
} }
} }
}
}
Dialog( Dialog(
onDismissRequest = { onClose() }, onDismissRequest = { onClose() },
@@ -148,12 +148,16 @@ fun ChannelScreen(
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
NostrChannelDataSource.start() NostrChannelDataSource.start()
feedViewModel.invalidateData() feedViewModel.invalidateData()
launch(Dispatchers.IO) {
newPostModel.imageUploadingError.collect { error -> newPostModel.imageUploadingError.collect { error ->
withContext(Dispatchers.Main) {
Toast.makeText(context, error, Toast.LENGTH_SHORT).show() Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
} }
} }
}
}
DisposableEffect(accountViewModel) { DisposableEffect(accountViewModel) {
val observer = LifecycleEventObserver { _, event -> val observer = LifecycleEventObserver { _, event ->
@@ -106,12 +106,16 @@ fun ChatroomScreen(
LaunchedEffect(baseUser, accountViewModel) { LaunchedEffect(baseUser, accountViewModel) {
NostrChatroomDataSource.start() NostrChatroomDataSource.start()
feedViewModel.invalidateData() feedViewModel.invalidateData()
launch(Dispatchers.IO) {
newPostModel.imageUploadingError.collect { error -> newPostModel.imageUploadingError.collect { error ->
withContext(Dispatchers.Main) {
Toast.makeText(context, error, Toast.LENGTH_SHORT).show() Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
} }
} }
}
}
DisposableEffect(baseUser, accountViewModel) { DisposableEffect(baseUser, accountViewModel) {
val observer = LifecycleEventObserver { _, event -> val observer = LifecycleEventObserver { _, event ->