Moves coroutine creation to the viewModel

This commit is contained in:
Vitor Pamplona
2023-08-04 15:20:31 -04:00
parent ec514651fc
commit 674896cea4
2 changed files with 23 additions and 25 deletions

View File

@@ -16,7 +16,6 @@ import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Surface import androidx.compose.material.Surface
import androidx.compose.material.Text import androidx.compose.material.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource 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.model.PublicChatChannel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.placeholderText
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@Composable @Composable
fun NewChannelView(onClose: () -> Unit, accountViewModel: AccountViewModel, channel: PublicChatChannel? = null) { fun NewChannelView(onClose: () -> Unit, accountViewModel: AccountViewModel, channel: PublicChatChannel? = null) {
@@ -59,14 +56,10 @@ fun NewChannelView(onClose: () -> Unit, accountViewModel: AccountViewModel, chan
onClose() onClose()
}) })
val scope = rememberCoroutineScope()
PostButton( PostButton(
onPost = { onPost = {
scope.launch(Dispatchers.IO) { postViewModel.create()
postViewModel.create() onClose()
onClose()
}
}, },
postViewModel.channelName.value.text.isNotBlank() postViewModel.channelName.value.text.isNotBlank()
) )

View File

@@ -3,8 +3,11 @@ package com.vitorpamplona.amethyst.ui.actions
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.PublicChatChannel import com.vitorpamplona.amethyst.model.PublicChatChannel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class NewChannelViewModel : ViewModel() { class NewChannelViewModel : ViewModel() {
private var account: Account? = null private var account: Account? = null
@@ -25,24 +28,26 @@ class NewChannelViewModel : ViewModel() {
} }
fun create() { fun create() {
this.account?.let { account -> viewModelScope.launch(Dispatchers.IO) {
if (originalChannel == null) { account?.let { account ->
account.sendCreateNewChannel( if (originalChannel == null) {
channelName.value.text, account.sendCreateNewChannel(
channelDescription.value.text, channelName.value.text,
channelPicture.value.text channelDescription.value.text,
) channelPicture.value.text
} else { )
account.sendChangeChannel( } else {
channelName.value.text, account.sendChangeChannel(
channelDescription.value.text, channelName.value.text,
channelPicture.value.text, channelDescription.value.text,
originalChannel!! channelPicture.value.text,
) originalChannel!!
)
}
} }
}
clear() clear()
}
} }
fun clear() { fun clear() {