From 674896cea468abe1c9ba24a3b7646b5da70c75c5 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 4 Aug 2023 15:20:31 -0400 Subject: [PATCH] Moves coroutine creation to the viewModel --- .../amethyst/ui/actions/NewChannelView.kt | 11 +----- .../ui/actions/NewChannelViewModel.kt | 37 +++++++++++-------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewChannelView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewChannelView.kt index a86c91462..a5769d463 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewChannelView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewChannelView.kt @@ -16,7 +16,6 @@ import androidx.compose.material.OutlinedTextField import androidx.compose.material.Surface import androidx.compose.material.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource @@ -30,8 +29,6 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.PublicChatChannel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.theme.placeholderText -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch @Composable fun NewChannelView(onClose: () -> Unit, accountViewModel: AccountViewModel, channel: PublicChatChannel? = null) { @@ -59,14 +56,10 @@ fun NewChannelView(onClose: () -> Unit, accountViewModel: AccountViewModel, chan onClose() }) - val scope = rememberCoroutineScope() - PostButton( onPost = { - scope.launch(Dispatchers.IO) { - postViewModel.create() - onClose() - } + postViewModel.create() + onClose() }, postViewModel.channelName.value.text.isNotBlank() ) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewChannelViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewChannelViewModel.kt index 18aaf9035..7ab71eb1f 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewChannelViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewChannelViewModel.kt @@ -3,8 +3,11 @@ package com.vitorpamplona.amethyst.ui.actions import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.text.input.TextFieldValue import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.PublicChatChannel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch class NewChannelViewModel : ViewModel() { private var account: Account? = null @@ -25,24 +28,26 @@ class NewChannelViewModel : ViewModel() { } fun create() { - this.account?.let { account -> - if (originalChannel == null) { - account.sendCreateNewChannel( - channelName.value.text, - channelDescription.value.text, - channelPicture.value.text - ) - } else { - account.sendChangeChannel( - channelName.value.text, - channelDescription.value.text, - channelPicture.value.text, - originalChannel!! - ) + viewModelScope.launch(Dispatchers.IO) { + account?.let { account -> + if (originalChannel == null) { + account.sendCreateNewChannel( + channelName.value.text, + channelDescription.value.text, + channelPicture.value.text + ) + } else { + account.sendChangeChannel( + channelName.value.text, + channelDescription.value.text, + channelPicture.value.text, + originalChannel!! + ) + } } - } - clear() + clear() + } } fun clear() {